-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
handleChange works unexpectedly when input is inside of a component being tested #713
Comments
Hi @nooruddin Look at here
|
@marcosvega91 |
@marcosvega91 Bumping default version to latest from npm (10.x), resulted in I tried installing It seems like if I am coming from Is there a way I can fix this, without doing any upgrades? |
Hi @nooruddin you can use
|
Created a sandbox to further debug the issue: |
That being said, I'm having mixed results in Codesandbox, which I think might be more them then the code. But you're also missing an |
Few other things:
|
@samtsai tried all these approaches. No luck still. also sandbox is behaving weird where it would pass some test and fail on the next run. Also, I also followed all the best practices listed here. Here is my resulting test so far. All tests will pass even the ones which is testing for
|
@nooruddin can you try adding
|
Also it doesn't look like you've added |
@samtsai yeah I had onChange prop but it was not working either and I guess it makes sense because input is controlled by component itself and is not expecting any props as such. I have also tried adding
I have also tried removing I have attached updated zip file in case if you want to try couple of things and see if you can get it working. Also, I have not installed any packages separately. Everything I have in package.json minus axios was installed as part of |
I'm trying to understand this one, if you're not using the |
@MatanBobi thanks for the input, I agree with what you're saying. 😄 |
@MatanBobi Yes that explanation totally makes sense. However, I am curious as to why I can test for the value of the text input to be that text but I can't test for the onChange being called for each character being typed. If an input that is controlled can be tested for the result (in this case the text being typed), why can't it be tested for it's actual onChange functionality. I might be completely off here and not really understand the internal workings of this library. So please forgive that. 😄 |
Hi @nooruddin, I'll try to answer you question. The onChange event and it's corresponding handler are contained inside of your component. Because of this, there is no way to access it from outside without a refactor to accept the onChange handler as a prop. However, as the others have said, this isn't something that needs to be tested as it is partially out of your control and only how the result is achieved rather than what was achieved. I hope that helps! 😄 |
@WretchedDade in that sense, can't we say that the I guess |
@nooruddin The value property is made available when the component is rendered to the dom/virtual dom. This is how you can do things like I haven't tried to get a reference to an event handler for my component before but I can't think of any good ways to do it at the moment. |
Just following what @WretchedDade wrote and emphasizing one important thing, when using RTL we should test a component the way the user uses it. |
Looks like the issue has been resolved. If this is still an issue please update the original issue description and ping me so that it's easier for maintainers to understand what exactly the issue is. |
I am trying to figure out why following line of code would always work no matter what number I put.
waitForElement(() => expect(onChange).toHaveBeenCalledTimes(3));
I am trying to test that input has been called x number of times however input is controlled within the component itself. No matter what number provide it will always pass the test.
complete-react-testing.zip
With fireEvent, I expect it to be
waitForElement(() => expect(onChange).toHaveBeenCalledTimes(1));
With userEvent, I expect it to be
waitForElement(() => expect(onChange).toHaveBeenCalledTimes(5));
Here is my Component:
Here are my tests:
The text was updated successfully, but these errors were encountered: