happy-dom
happy-dom copied to clipboard
Checkbox onchange does not work
Steps to repo:
- create a checkbox that fires an onchange handler
- validate the onchange handler in the UI
Result:
- the onchange handler does not fire
Environment:
- @happy-dom/jest-environment: 6.0.2
Notes:
- It works with
jsdom
!
// sample DOM
function OnChangeCheckbox() {
const [checked, setChecked] = useState(false)
return (
<div>
<label>
<input type="checkbox" name="stability" onChange={() => setChecked(true)}/>
Stability
</label>
{checked && <div>Checked</div>}
</div>
);
}
- run
yarn jest yarn jest src/checkbox-on-change.test.tsx
Here's another example of it failing and it also failing with userEvent.type, https://codesandbox.io/s/happy-dom-onchange-791uvl?file=/src/App.test.tsx and is also a reproduction of #467
Note a workaround is to use fireEvent.input instead of userEvent.type
On Jul 7, 2022 7:50 PM, GW @.***> wrote:
Here's another example of it failing and it also failing with userEvent.type, https://codesandbox.io/s/happy-dom-onchange-791uvl?file=/src/App.test.tsx
— Reply to this email directly, view it on GitHubhttps://github.com/capricorn86/happy-dom/issues/531#issuecomment-1178414827, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AAGAGML443LSOFNGAUQH3UDVS53OTANCNFSM52XTP5SQ. You are receiving this because you authored the thread.Message ID: @.***>
Note a workaround is to use fireEvent.input instead of userEvent.type
This isn't per se a workaround. This requires changing the event handler to onInput from onChange, which aren't the same event or behavior https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/input_event.
I even updated the codesandbox to show you cannot just swap to fireEvent.input and have everything just work.
No i meant that another bug is that userEvent.type in general does not work and a workaround is to use fireevent.input
On Jul 8, 2022 7:50 AM, GW @.***> wrote:
Note a workaround is to use fireEvent.input instead of userEvent.type
This isn't per se a workaround. This requires changing the event handler to onInput from onChange, which aren't the same event or behavior https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/input_event.
I even updated the codesandbox to show you cannot just swap to fireEvent.input and have everything just work.
— Reply to this email directly, view it on GitHubhttps://github.com/capricorn86/happy-dom/issues/531#issuecomment-1178953370, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AAGAGMIMYH7M76GBAMV5UJDVTAP2FANCNFSM52XTP5SQ. You are receiving this because you authored the thread.Message ID: @.***>
Just ran into this issue. Clicking on the label doesn't seem to work either.
Ran into the same issue. onChange is not fired but onClick is fired
Ran into a similar issue...
This test works with Jest in a Next project without Vite, but with Vite, vitest and using happy-dom, it doesn't work:
describe('<Checkbox />', () => {
it('selects the checkbox', async () => {
render(
<input id="checkbox" data-testid="my-checkbox" type="checkbox" />
);
const checkbox: HTMLInputElement = screen.getByTestId("my-checkbox");
await userEvent.click(checkbox);
expect(checkbox.checked).toEqual(true);
});
});
I'm in a Typescript project using (React) MUI checkboxes where we're migrating from Jest with jsdom
to Vitest with happy-dom
, and we run into this same issue. Changing the onChange
handlers to onClick
solved it, but we had to do some extraneous type castings. Also, we had similar tests to @abacchi00 example above which we had to refactor.
This has finally been fixed.
You can read more about the release here: https://github.com/capricorn86/happy-dom/releases/tag/v9.0.0
..hm, with version 9.1.7, onChange not called
We have the same problem. With checkbox elements and radio buttons.
It worked in jsdom
, but it doesn't work in happy-dom
.
Why is this issue closed if the problem is not solved?