Skip to content

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

Closed
@exaucae

Description

@exaucae
  • @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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions