Reproduction example
https://codesandbox.io/p/sandbox/zealous-feynman-rxvrlm?file=%2Fsrc%2FApp.test.js
Prerequisites
- Render a component with an
<input type="checkbox" onChange={yourFunc} disabled /> inside a <label>.
- Click any element inside the
<label> (or the label itself).
Expected behavior
The onChange method of the checkbox should not be called, as the checkbox is disabled.
Actual behavior
The onChange method of the checkbox is being called.
User-event version
14.5.2
Environment
If the Codesandbox is not available, it is easily reproducible by a single test file:
import "@testing-library/jest-dom";
import { render } from "@testing-library/react";
import { screen } from "@testing-library/dom";
import { userEvent } from "@testing-library/user-event";
describe("Checkbox", () => {
it("does not execute onChange if checkbox is disabled", async () => {
const onChange = vi.fn(); // also reproducible with jest.fn();
render(
<label>
<input type="checkbox" onChange={onChange} data-testid="input" disabled />
<span>My label</span>
</label>
);
await userEvent.click(screen.getByText("My label"));
expect(onChange).not.toHaveBeenCalled(); // this fails
});
});
Additional context
If the same test is performed but using the onClick event, the test is successful and the event is not called. However, onChange should not be called either. When trying this same scenario in a real browser, onChange is correctly ignored.
Reproduction example
https://codesandbox.io/p/sandbox/zealous-feynman-rxvrlm?file=%2Fsrc%2FApp.test.js
Prerequisites
<input type="checkbox" onChange={yourFunc} disabled />inside a<label>.<label>(or the label itself).Expected behavior
The
onChangemethod of the checkbox should not be called, as the checkbox is disabled.Actual behavior
The
onChangemethod of the checkbox is being called.User-event version
14.5.2
Environment
If the Codesandbox is not available, it is easily reproducible by a single test file:
Additional context
If the same test is performed but using the
onClickevent, the test is successful and the event is not called. However,onChangeshould not be called either. When trying this same scenario in a real browser,onChangeis correctly ignored.