-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Not firing onChange event #359
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
Hello, I confirm that onChange() is triggered in CodeSandbox but never with JSDOM (v14.1.0 and 15.0.0). I've tried with:
Neither works, with controlled or uncontrolled inputs.
|
Workaround with |
I'm not sure what the problem is exactly, but when I try it with jest it works properly: https://github.com/kentcdodds/react-test-issue-with-jest So my guess is you have JSDOM misconfigured. The good news is there's hope and you just need to figure out how to configure JSDOM correctly. Good luck! |
Hi @kentcdodds, Really thank you for your example I was able to compare configurations. Explanation why Jest team prefers use their JSDOM fork without node 6 compatibility: |
My problem is yet to be solved. Unfortunately I never I had to move a project into jest and create-react-app. Sorry for the silly question but how did you actually run the test? My first thoughts were I will come back later when I manage the time to deal with jest. I really think this project has a better approach than enzyme. I tried to follow the links but I started to get some abstraction layers on how to configure JSDOM. It just will need more time to reverse engineer Thanks for trying the material-ui code since it is not related to this project. I hope for future versions to become less Jest dependent. I would even like to promote my template for issues since it is the bare minimum. It does not require a testing library (run the file directly) and exposes clearly the critical dependency (JSDOM) which tends to have a different configuration among projects. well, JSDOM has been a source of pain and magic hacks since I deal with it, but it is the necessary evil |
I'd suggest that 90% of people using react-testing-library are using it with jest and they're using jest's default JSDOM functionality, so we're not going to change the template. I suggest you use Jest with it's default configuration for jsdom and you'll be fine. Thanks though! |
I'm also having the same issue. This doesn't work. It returns a different color to what is returned when it's used in production:
This works fine. It returns the same value that's returned when used in production:
|
Maybe @oliviertassinari can help with this |
@TidyIQ You can find a working Input change test in https://codesandbox.io/s/nn3o075794. Let's see for the OutlinedInput. Could you provide a failing test case? |
@oliviertassinari Sure. Here's the onChange function for the OutlinedInput component. It changes the field's
This is the entire
Simplified:
And here's the test that's failing:
Here's the failed message:
And if I remove the waitForDomChange and just get the
And if I run
|
@fgarcia @TidyIQ I ran into this same issue testing a MaterialUI Select. MaterialUI attaches the event handler to a div that wraps the input, not the input itself, so you can't update the component by triggering the input's onChange. You could replicate MaterialUI's own test -- click the element, and then click an option. Or you could add |
@mpopv The input is Use the native version of the Select component (uses the |
Why is this closed? Many seem to be still seeing this issue. |
Hi @jamesBennett. Could you please open a new issue with a reproduction of your problem? |
@jamesBennett if you create an issue, can you come back and add a link to it here? I'd like to keep an eye out on this. |
@jamesBennett I was having this same issue and it turned out that it was the package |
I don't know if this helps anyone, but I noticed the fireChange event will only work if your input has a type assigned to it. eg.
|
I'm not using jest-styled-components |
THANK YOU @mentierd The input type has to be a valid html5 input type as well. If you have |
Just as an FYI in case this helps others. I wasn't able to get fireEvent to working, but ReactTestUtils.Simulate was working. It turns out it was because I was setting the document body in my jest setup
when I updated that to append the items I needed in the body it worked as expected.
So, double check you're not doing something to the document body :) |
I'm seeing this issue as well using Karma on an actual browser. The input has a valid type attribute as well. |
This issue is pretty old and general. I'm pretty confident that most of the people "experiencing issue" are in fact experiencing different issues entirely. Please open new issues (and please follow the issue template). Thanks! |
I had similar problem, I was testing some forms in Formik, and onChange event was not triggering when I change text input value using fireEvent from @testing-library/react. In the end I managed to do that just by calling jest.useFakeTimers() before everything and waiting for assertion.
|
issue still exists in
hard to say if it's just version mismatching or what. there seems to be a persistent issue over the last couple of years of onChange—or onClick—just not firing when testing input elements. the only pattern i've seen is folks talking about issues with jsdom not firing the right set of events at the right time, which would go with the whole version mismatch issue. |
react-testing-library
version: 6.1.2react
version: 16.8.6node
version: 10.13.0npm
(oryarn
) version: 6.9Relevant code or config:
What you did:
I've seen other issues with problems where Material UI, they seem to have solved the problem but I cannot get this to work.
I tried two ways to change the right DOM element. If I search wit wrong test-id (to get the layout) I get this:
Reproduction:
git clone https://github.com/fgarcia/react-test-issue
Problem description:
I have a controlled input element and I cannot capture the onChange event.
I would expect the
let onChange
function of the example to be calledThe text was updated successfully, but these errors were encountered: