Simulating click on checkbox label is not triggering onChange event on inside input
See original GitHub issueI have an Checkbox component that is an <input type=checkbox>
inside a <label>
.
When I try to simulate an event of click in the label it does not trigger my component onChange
function.
Here is my component code:
const Checkbox = ({ disabled, onChange, checked, label }: Props) => (
<label
className={styles.label}
disabled={disabled}
>
<input
type='checkbox'
className={styles.checkbox}
checked={checked}
disabled={disabled}
onChange={onChange}
/>
{label}
</label>
);
My test:
it('calls onChange when the checkbox label is clicked', () => {
const wrapper = mount(<Checkbox label='Click me' onChange={onChange} />);
const label = wrapper.find('label');
label.simulate('click');
expect(onChange).toBeCalled();
});
To my understanding the simulate click should trigger the component onChange
function. As it works when a user click it and when we trigger an click event by javascript.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:7
- Comments:12 (3 by maintainers)
Top Results From Across the Web
Click event on label not firing (class doesn't change)
There's a quick fix: you could replace your click event with change event. This way: the label triggers checkbox; the change event on ......
Read more ><input type="checkbox"> - HTML: HyperText Markup Language
A Boolean attribute indicating whether this checkbox is checked by default (when the page loads). It does not indicate whether this checkbox is ......
Read more >Simulate React On-Change On Controlled Components
We can set the value on input directly without triggering onChange . But in situations where most of our logic relies on change...
Read more >How to trigger a click() event on LWC element
It appears that there's no way in LWC to send events to a component's internal components, which is why this code isn't working....
Read more >react input onchange doesn't work - You.com | The AI Search ...
There's the issue. When attaching Inputmask to event inside of react component there is no way to know when value has been changed....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Simulating click only calls
onClick
- the purpose isn’t to test React event wiring, or browser event handling.There’s no need to test that React will trigger a “change” event when a checkbox is clicked - all you need is to shallow-render
Checkbox
, and assert that theinput
’sonChange
attribute matches the prop you passed.True, but fwiw it’s also really nice to have tests that closely match the mental-model/flow of the user, i.e. react-testing-libraries principle of “The more your tests resemble the way your software is used, the more confidence they can give you.”.
So, agreed that technically yes, as an implementation detail I know that “click on this radio” will really do a change prop, but it’d be nice if this still “just worked”, especially for
mount
which AFAIU renders down to DOM elements anyway.Admittedly, it’s an engineer/text UX issue and not a core functionality bug, but we’re trying to use an approach of “props are internal impl details, the test should only interact with / touch / click DOM elements” (at least for full
mount
-based tests), and so hit this bump.Thanks @dmmulroy for the work around, we’ll try that!