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
How to change a checkbox value using simulate api? #216
Comments
@marlonbernardes I need to make it work for change event.. Well, will give a try for 'click'. |
@marlonbernardes Still not working... |
You should bind the value of onChange={this.handleChange.bind(this)} Try this and let me know if it works for you. I also have some tips on how to make your component's code more concise, but lets focus on one thing at a time. |
@marlonbernardes Sorry for the late reply. Still it is not working... Can you post me a sample of how to test a checkbox with jsdom and enzyme ? That would be a great help. |
(of course, you shouldn't create functions and pass them as props in the render path - so you'd want |
@ljharb @marlonbernardes This is the full code https://gist.github.com/bboysathish/8c78d6c295c0613d08b6. Edit: That's not the best code though.. But it should work. The following is the error, 1) checkbox Renders and Simulates Click Event :
AssertionError: expected 'True' to equal 'False'
+ expected - actual
-True
+False
at Context.<anonymous> (checkbox_component_test.js:25:47)
at callFn (C:\Users\sathishkumar_so\Downloads\SSBT\Admin-UI\Feb24\Code\node_modules\mocha\lib\runnable.js:315:21)
at Test.Runnable.run (C:\Users\sathishkumar_so\Downloads\SSBT\Admin-UI\Feb24\Code\node_modules\mocha\lib\runnable.js:308:7)
at Runner.runTest (C:\Users\sathishkumar_so\Downloads\SSBT\Admin-UI\Feb24\Code\node_modules\mocha\lib\runner.js:422:10)
at C:\Users\sathishkumar_so\Downloads\SSBT\Admin-UI\Feb24\Code\node_modules\mocha\lib\runner.js:533:12
at next (C:\Users\sathishkumar_so\Downloads\SSBT\Admin-UI\Feb24\Code\node_modules\mocha\lib\runner.js:342:14)
at C:\Users\sathishkumar_so\Downloads\SSBT\Admin-UI\Feb24\Code\node_modules\mocha\lib\runner.js:352:7
at next (C:\Users\sathishkumar_so\Downloads\SSBT\Admin-UI\Feb24\Code\node_modules\mocha\lib\runner.js:284:14)
at Immediate._onImmediate (C:\Users\sathishkumar_so\Downloads\SSBT\Admin-UI\Feb24\Code\node_modules\mocha\lib\runner.js:320:5) |
@bboysathish calling |
@ljharb Worked like a charm, Thanks |
@bboysathish See item 2 on http://airbnb.io/enzyme/docs/api/ReactWrapper/simulate.html#arguments |
@ljharb R u referring this |
@bboysathish it's any object you like. whatever you pass there, will be passed as arguments to the event handler. there's no magic. |
@ljharb So, for |
Looks right to me - try it and see! :-) |
Great!! Will try.. Thanks @ljharb ... |
Now, this isn't working for me. |
I hit some problems when I was using |
In case someone else hits this. If you're using typescript, |
As a variation on @Burkazoid's suggestion, a workaround that doesn't require changing .prop('onChange')({currentTarget: { checked: false }}) |
Is there a way to simulate checking a checkbox that is within in a Edit: My use case is that I have a select with a bunch of checkboxes in them; instead of passing the change event directly to the The current test setup that I have has two steps: 1. Test passing a change event to select 2. Getting all of the checkboxes and testing the The problem that I have is that the rendered DOM of I am using Material-UI components, but that shouldn't change the scenario. |
@HunderlineK no, you can't simulate anything. You can explicitly invoke the |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
@ljharb this shows an error like Method “simulate” is only meant to be run on a single node. 0 found instead. |
@surya-art sounds like there's no checkboxes then. Remember to (Please file a new issue with your comment and your response, if that doesn't solve it for you) |
change currentTarget to target if you're using .prop
|
Hi,
I am using jsdom with enzyme.
When the component mounts first time it is set to "checked":"checked", the input element has "onChange" event which will change the state and re-renders the check box as uncheked.
Now to test this with enzyme, I tried doing this.
Even this also not working,
P.S : How to do simulation for all the events like input type text,radio,textarea etc.. is there any resource available.
The text was updated successfully, but these errors were encountered: