Skip to content

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

Description

@albertnev

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions