userEvent.click doesn't trigger onSubmit callback from the form
See original GitHub issue@testing-library/user-event
version: @testing-library/user-event@12.8.3- Testing Framework and version: jest@26.6.0
- DOM Environment: jsdom@16.5.1
Relevant code or config
// component at its most reduced schema
return (
<form name='registrationForm' onSubmit={ () => window.alert('saved') }>
<Button>Save</Button>
</form>
);
What you did: test the componenent like so
it('should save something', async () => {
render(<Component/>);
userEvent.click(screen.getByRole('button', { name: 'save' }));
expect(await screen.findByText('saved')).toBeInTheDocument();
});
What happened:test fails with stacktrace
TestingLibraryElementError: Unable to find an element with the text: saved. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible.
The alert is not rendered - the onSubmit
method was not triggered. Obviously, the button has no callback for onClick
, but one one in the form should have been triggered, shouldn’t it?
Reproduction repository: None
Suggested solution:
When I change the component to this, the alert renders, and test passes :
return (
<form name='registrationForm'>
<Button onClick={ () => window.alert('saved') }>Save</Button>
</form>
);
That’s odd.
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
userEvent in React Testing Library Doesn't Cause onClick To ...
I had the same problem, fireEvent works but the recommended userEvent doesn't trigger click handler. Turned out that userEvent is async.
Read more >Testing Forms using @testing-library/user-event
This component will contain the entire form: providing an API for consuming onSubmit and onInvalid callbacks and for providing defaultValues ...
Read more >12 recipes for testing React applications using Testing Library
Invokes given callback. We're testing that after some interaction the component calls a given callback. We give a mock function to the component ......
Read more >Jest testing — mocking child components to make your unit ...
<form onSubmit={handleSubmit}> ... userEvent.click(deleteUserButton); ... If your mock component doesn't accurately capture the ...
Read more >Untitled
Note that click will trigger hover events before clicking. user-event ... be a way to handle the form submission so that Enter key...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I was debugging a similar error for an hour or two… My
onSubmit
wasn’t triggered because the form wasn’t valid. I had somerequired
fields that were empty. The version I upgraded from (maybe it was the old version of JSDOM) didn’t validate forms.I don’t know if this library could implement something that would prevent other users from wasting time on something like this. Maybe a verbose mode that gives the user a more detailed log:
@chrys-exaucet The submit handler is called.
window.alert
does not insert any element into the document. https://codesandbox.io/s/click-on-button-submits-form-forked-o9u99?file=/src/App.js