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

fix: 🐛 click() handles checkboxes correctly #17

Merged
merged 1 commit into from
Sep 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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