Skip to content
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

onChange events do not work on checkbox and radio button elements #880

Closed
pkolt opened this issue Apr 30, 2023 · 2 comments · Fixed by #884
Closed

onChange events do not work on checkbox and radio button elements #880

pkolt opened this issue Apr 30, 2023 · 2 comments · Fixed by #884
Assignees
Labels
bug Something isn't working

Comments

@pkolt
Copy link

pkolt commented Apr 30, 2023

Describe the bug
The onChange event is not generated when clicking on the checkbox and radio button elements. This breaks tests that used to work on jsdom.

 const { container } = render(<App />);
 const checkbox = screen.getByTestId('checkbox');
 await userEvent.click(checkbox);

This was already discussed in issue, but for some reason this ticket was closed. The problem is not solved and new happy-dom users face it again and again. I suggest fixing this problem or documenting this feature in the happy-dom.

To Reproduce
https://github.com/pkolt/bug_checkbox_happy_dom

Expected behavior
When clicking on the checkbox and radio button elements, onChange events are triggered.

@pkolt pkolt added the bug Something isn't working label Apr 30, 2023
@pkolt
Copy link
Author

pkolt commented Apr 30, 2023

It is interesting that if you use native events - change, without the participation of React.js then the test passes successfully.

it('event change', async () => {
    const handle = vi.fn();
    render(<App />);
    const checkbox = screen.getByTestId('tea') as HTMLInputElement;
    checkbox.addEventListener('change', handle); // ❗️
    await userEvent.click(checkbox);
    expect(handle).toHaveBeenCalled(); // ✅
  });

But if the subscription to the onChange event comes from React.js, then the event handler is not executed.

it('event onChange', async () => {
    const { container } = render(<App />);
    const checkbox = screen.getByTestId('tea') as HTMLInputElement;
    await userEvent.click(checkbox);
    expect(container.innerHTML).toMatch('Selected Tea'); // ❌
  });

It seems that the problem is in the interaction between React.js and happy-dom, in the place of subscription to the onChange event.

@capricorn86 capricorn86 self-assigned this May 2, 2023
capricorn86 added a commit that referenced this issue May 2, 2023
capricorn86 added a commit that referenced this issue May 2, 2023
…not-work-on-checkbox-and-radio-button-elements

#880@patch: Fixes issue where react "onChange" listener never got tri…
@capricorn86
Copy link
Owner

Thank you for reporting @pkolt! 🙂

The issue has now been fixed.

React is apparently overriding the getter and setter for the HTMLInputElement.checked property with a wrapper for storing its state. As the internal logic of Happy DOM was setting the "checked" value using the setter, the React wrapper was called to store the checked state. React then failed to compare the old state with the new state as they where always the same value.

You can read more about the release here:
https://github.com/capricorn86/happy-dom/releases/tag/v9.10.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants