question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Simulating click on checkbox label is not triggering onChange event on inside input

See original GitHub issue

I 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:closed
  • Created 6 years ago
  • Reactions:7
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

34reactions
ljharbcommented, May 22, 2017

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 the input’s onChange attribute matches the prop you passed.

10reactions
stephenhcommented, Sep 19, 2019

the purpose isn’t to test React event wiring, or browser event handling.

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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found