I'm trying to paste text that's already in my clipboard into a textbox, but I dont understand how to use "eventInit" to do this. I've read the documentation on how to paste text into a textbox, but it isn't clear on how to use eventInit.
How do i paste text that's in my clipboard into a textbox using userEvent?
This is my code:
test('Copy id button copies correct id', async () => {
const { getAllByLabelText, debug, getByText } = render(
<MockedProvider mocks={mocks} addTypename={false}>
<History />
</MockedProvider>
);
const textbox = <input type="text" />;
await waitFor(() => new Promise((resolve) => setTimeout(resolve, 0)));
const button = getAllByLabelText('click to copy id')[0];
fireEvent.click(button);
// userEvent.paste(textbox,_,) unsure what to do here...
});
Documentation:
One approach would be to first build a copy to clipboard function and then call it using the onClick event handler. Show activity on this post. You can use event clipboardData collection method e. clipboardData.
Select the text or graphics you want to copy, and press Ctrl+C. Each selection appears in the Clipboard, with the latest at the top.
The Clipboard API provides the ability to respond to clipboard commands (cut, copy, and paste) as well as to asynchronously read from and write to the system clipboard. Note: This API is not available in Web Workers (not exposed via WorkerNavigator ).
React Testing Library encourages you to test the behavior of your application instead of implementation details. By testing your application the way a user would use it, you can be confident that your application will behave as expected when all test cases have passed.
userEvent.paste
won't help you: it is meant for cases where you test what happens when a user pastes some text into an input. React testing library doesn't actually have a clipboard that would hold the value that was copied.
What I would do:
If you actually want to test that copying to clipboard works, you need to write an end to end test that runs an actual browser. At least Cypress offers apis to read the contents of the clipboard.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With