-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Simulate.click on a submit button #54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I agree we should add a note in the docs under Simulate. And the suggested solution should be to use |
@kentcdodds I was about to talk about this sometime back... We have I was just thinking like does |
Yes, |
Got it, thanks! |
Closing this w.r.t #57 |
* Add within API and document it * improve the `within` API example * Update README.md
Correct me if I'm wrong, but JSDOM does not support submitting the form element: jsdom/jsdom#1937 I get the same error as described in the link above when trying to do so. Am I missing something? |
This is a pretty old issue. Could you open a new one with an example of what you're trying to do? |
Sorry for not being clear enough: I think this is not an issue related to I tried to trigger a form submit using I asked in this thread as I thought other people might end up here when searching for the same issue. Sidenote: We're in the process of moving from enzyme to react-testing-library, love the new approach! Thanks! |
Glad you're liking react-testing-library @j13l! Hmmm, this actually works today: import React from 'react'
import {render, fireEvent} from 'react-testing-library'
test('works', () => {
const handler = jest.fn(e => e.preventDefault())
const {getByTestId} = render(
<form onSubmit={handler} data-testid="form">
<button type="submit">Submit</button>
</form>,
)
fireEvent.submit(getByTestId('form'))
expect(handler).toHaveBeenCalledTimes(1)
}) Though I would actually recommend not firing a submit event on a form, but instead do what the user would do and click on the submit button: import React from 'react'
import {render, fireEvent} from 'react-testing-library'
test('works', () => {
const handler = jest.fn(e => e.preventDefault())
const {getByText} = render(
<form onSubmit={handler}>
<button type="submit">Submit</button>
</form>,
)
fireEvent.click(getByText(/submit/i))
expect(handler).toHaveBeenCalledTimes(1)
})
I hope that's helpful! |
Yes, it works!! I thought submitting via click is breaking due to the JSDOM error. Turns out it was something completely different: Thanks a lot for your help @kentcdodds 👏 |
Actually, I think it's ok to put a span inside a button, but firing a click event on the span should bubble up to the button anyway, so it should still work. |
Actually it works if you use this aproach: test('works', () => {
const handler = jest.fn(e => e.preventDefault())
const {container} = render(
<form onSubmit={handler}>
<button type="submit" data-testid="submit"><span>Submit</span></button>
</form>,
)
fireEvent.click(getByTestId(container, 'submit'))
expect(handler).toHaveBeenCalledTimes(1)
}) But using the example with
|
Ok, so this should work: test('works', () => {
const handler = jest.fn(e => e.preventDefault())
const {getByText} = render(
<form onSubmit={handler}>
<button type="submit">
<span>Submit</span>
</button>
</form>,
)
fireEvent.click(getByText(/submit/i))
expect(handler).toHaveBeenCalledTimes(1)
}) If it's not working it's probably because your version of jsdom is old. I believe this is a bug that was fixed in jsdom@14. If you cannot upgrade, then I recommend this approach: test('works', () => {
const handler = jest.fn(e => e.preventDefault())
const {getByText} = render(
<form onSubmit={handler}>
<button type="submit">
<span>Submit</span>
</button>
</form>,
)
fireEvent.click(getByText(/submit/i).closest('button'))
expect(handler).toHaveBeenCalledTimes(1)
}) |
You're right. Just set up a CodeSandbox example, but bubbling does work as intended. However, in our case if I render the label with a span, it does not bubble up. Running on latest Jest, JSDOM & co. Ran out of ideas why this could happen, might look into it again later. Thanks! |
The problem is that If you use https://www.npmjs.com/package/jest-environment-jsdom-fourteen you'll be set 👍 Maybe this should be documented, or maybe we can help jest upgrade jsdom... |
Looks like the reason jest is using an old version of jsdom is for Node 4 support. Node 4 will reach End-of-life at the end of April. So hopefully in the next few months we should get an updated version out of the box. |
I wonder if this would explain having a similar issue with event bubbling in codesandbox's jest environment. |
I see now even jsdom is on 15 .... the old version in jest is sad :( |
You can use the const {container, fireEvent} = render(<App/>)
const form = container.querySelector('form');
fireEvent.submit(form); |
react-testing-library
version: 2.1.1node
version: 8.9.3npm
(oryarn
) version:yarn
1.3.2Relevant code or config
What you did:
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 callingcan 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...The text was updated successfully, but these errors were encountered: