Skip to content

Commit

Permalink
fix: πŸ› click() handles checkboxes correctly
Browse files Browse the repository at this point in the history
Issues: #14
  • Loading branch information
Gpx committed Sep 25, 2018
1 parent 57f860a commit fc89964
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
28 changes: 28 additions & 0 deletions __tests__/click.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,32 @@ describe("fireEvent.click", () => {
expect(getByTestId("input")).toHaveFocus();
}
);

it('checks <input type="checkbox"> when clicking a <label> with htmlFor', () => {
const { getByTestId } = render(
<React.Fragment>
<label htmlFor="input" data-testid="label">
Label
</label>
<input id="input" data-testid="input" type="checkbox" />
</React.Fragment>
);
expect(getByTestId("input")).toHaveProperty("checked", false);
userEvent.click(getByTestId("label"));
expect(getByTestId("input")).toHaveProperty("checked", true);
});

it('checks <input type="checkbox"> when clicking a <label> without htmlFor', () => {
const { getByTestId } = render(
<React.Fragment>
<label data-testid="label">
Label
<input id="input" data-testid="input" type="checkbox" />
</label>
</React.Fragment>
);
expect(getByTestId("input")).toHaveProperty("checked", false);
userEvent.click(getByTestId("label"));
expect(getByTestId("input")).toHaveProperty("checked", true);
});
});
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
"name": "user-event",
"version": "0.0.0-development",
"description": "Simulate user events for react-testing-library",
"keywords": ["react-testing-library", "dom-testing-library", "react", "testing"],
"keywords": [
"react-testing-library",
"dom-testing-library",
"react",
"testing"
],
"main": "dist/index.js",
"scripts": {
"test": "jest",
Expand Down
2 changes: 0 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ function clickLabel(label) {
fireEvent.mouseMove(label);
fireEvent.mouseDown(label);
fireEvent.mouseUp(label);
fireEvent.click(label);

if (label.htmlFor) {
const input = document.getElementById(label.htmlFor);
Expand All @@ -21,7 +20,6 @@ function clickLabel(label) {
const input = label.querySelector("input,textarea");
input.focus();
label.focus();
fireEvent.click(input);
fireEvent.click(label);
}
}
Expand Down

0 comments on commit fc89964

Please sign in to comment.