Skip to content

Commit 3d1ad49

Browse files
authored
Merge pull request #17 from Gpx/checkbox-bug
fix: πŸ› click() handles checkboxes correctly
2 parents 57f860a + fc89964 commit 3d1ad49

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

β€Ž__tests__/click.js

+28
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,32 @@ describe("fireEvent.click", () => {
193193
expect(getByTestId("input")).toHaveFocus();
194194
}
195195
);
196+
197+
it('checks <input type="checkbox"> when clicking a <label> with htmlFor', () => {
198+
const { getByTestId } = render(
199+
<React.Fragment>
200+
<label htmlFor="input" data-testid="label">
201+
Label
202+
</label>
203+
<input id="input" data-testid="input" type="checkbox" />
204+
</React.Fragment>
205+
);
206+
expect(getByTestId("input")).toHaveProperty("checked", false);
207+
userEvent.click(getByTestId("label"));
208+
expect(getByTestId("input")).toHaveProperty("checked", true);
209+
});
210+
211+
it('checks <input type="checkbox"> when clicking a <label> without htmlFor', () => {
212+
const { getByTestId } = render(
213+
<React.Fragment>
214+
<label data-testid="label">
215+
Label
216+
<input id="input" data-testid="input" type="checkbox" />
217+
</label>
218+
</React.Fragment>
219+
);
220+
expect(getByTestId("input")).toHaveProperty("checked", false);
221+
userEvent.click(getByTestId("label"));
222+
expect(getByTestId("input")).toHaveProperty("checked", true);
223+
});
196224
});

β€Žpackage.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22
"name": "user-event",
33
"version": "0.0.0-development",
44
"description": "Simulate user events for react-testing-library",
5-
"keywords": ["react-testing-library", "dom-testing-library", "react", "testing"],
5+
"keywords": [
6+
"react-testing-library",
7+
"dom-testing-library",
8+
"react",
9+
"testing"
10+
],
611
"main": "dist/index.js",
712
"scripts": {
813
"test": "jest",

β€Žsrc/index.js

-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ function clickLabel(label) {
1111
fireEvent.mouseMove(label);
1212
fireEvent.mouseDown(label);
1313
fireEvent.mouseUp(label);
14-
fireEvent.click(label);
1514

1615
if (label.htmlFor) {
1716
const input = document.getElementById(label.htmlFor);
@@ -21,7 +20,6 @@ function clickLabel(label) {
2120
const input = label.querySelector("input,textarea");
2221
input.focus();
2322
label.focus();
24-
fireEvent.click(input);
2523
fireEvent.click(label);
2624
}
2725
}

0 commit comments

Comments
Β (0)