Skip to content

Commit

Permalink
fix: πŸ› don't fire events when clicking disabled checkbox
Browse files Browse the repository at this point in the history
disabled checkboxes don't fire any event

BREAKING CHANGE: 🧨 clicking disabled checkbox no longer fires events

βœ… Closes: #96
  • Loading branch information
Gpx committed Apr 18, 2019
1 parent 5a5288f commit c20dce0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
27 changes: 26 additions & 1 deletion __tests__/click.js
@@ -1,5 +1,5 @@
import React from "react";
import { render, cleanup } from "react-testing-library";
import { render, cleanup, fireEvent } from "react-testing-library";
import "jest-dom/extend-expect";
import userEvent from "../src";

Expand Down Expand Up @@ -67,6 +67,31 @@ describe("userEvent.click", () => {
expect(getByTestId("element")).toHaveProperty("checked", true);
});

it('should fire the correct events for <input type="checkbox" disabled>', () => {
const events = [];
const eventsHandler = jest.fn(evt => events.push(evt.type));
const { getByTestId } = render(
<input
data-testid="element"
type="checkbox"
onMouseOver={eventsHandler}
onMouseMove={eventsHandler}
onMouseDown={eventsHandler}
onFocus={eventsHandler}
onMouseUp={eventsHandler}
onClick={eventsHandler}
onChange={eventsHandler}
disabled
/>
);

userEvent.click(getByTestId("element"));

expect(events).toEqual([]);

expect(getByTestId("element")).toHaveProperty("checked", false);
});

it("should fire the correct events for <div>", () => {
const events = [];
const eventsHandler = jest.fn(evt => events.push(evt.type));
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Expand Up @@ -31,6 +31,8 @@ function clickLabel(label) {
}

function clickCheckbox(checkbox) {
if (checkbox.disabled) return;

fireEvent.mouseOver(checkbox);
fireEvent.mouseMove(checkbox);
fireEvent.mouseDown(checkbox);
Expand Down

0 comments on commit c20dce0

Please sign in to comment.