Description
react-testing-library
version: 2.1.1node
version: 8.9.3npm
(oryarn
) version:yarn
1.3.2
Relevant code or config
const Component = () => {
return <form onSubmit={handleSubmit}>
<button type="submit">Submit</button>
</form>
}
What you did:
const submitButton = getByText('Submit')
// does not work
Simulate.click(submitButton)
// works
Simulate.submit(submitButton)
What happened:
Submit buttons cannot be clicked using Simulate.click
.
Problem description:
This is not a bug as far as Simulate
is concerned, but I think that in the spirit of the Guiding Principles, resorting to calling .submit
should be discouraged. Users click submit buttons, they do not invoke the submit event.
Certain libraries like react-final-form rely on the submit event to display validation errors. If for example, you have logic to disable a submit button from being clickable, calling Simulate.submit
will ignore this and call the submit event anyway. This can result in incorrect assertions.
Suggested solution:
Add a quick caveat in the docs under Simulate
. Note that calling
submitButton.dispatchEvent(new MouseEvent('click'))
can solve this issue. I am good to submit a PR if this is deemed worth it.
EDIT: I guess this is technically covered in the fireEvent
use case...