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

Disabled checkbox inside a label still triggers onChange method when clicked #1234

Open
albertnev opened this issue Sep 23, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@albertnev
Copy link

Reproduction example

https://codesandbox.io/p/sandbox/zealous-feynman-rxvrlm?file=%2Fsrc%2FApp.test.js

Prerequisites

  1. Render a component with an <input type="checkbox" onChange={yourFunc} disabled /> inside a <label>.
  2. 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.

@albertnev albertnev added bug Something isn't working needs assessment This needs to be looked at by a team member labels Sep 23, 2024
@ph-fritsche ph-fritsche removed the needs assessment This needs to be looked at by a team member label Oct 25, 2024
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

No branches or pull requests

2 participants