question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

userEvent.click doesn't trigger onSubmit callback from the form

See original GitHub issue

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:closed
  • Created 2 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
MidnightDesigncommented, Aug 23, 2022

I was debugging a similar error for an hour or two… My onSubmit wasn’t triggered because the form wasn’t valid. I had some required 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:

Hovering over the element
Clicking the element
Firing "onClick"
Firing "onSubmit" on the element's form
Warning: Could not submit form because it is invalid
1reaction
ph-fritschecommented, Mar 24, 2021

@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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found