Skip to content

Commit d686e55

Browse files
authored
Merge pull request #7 from Gpx/checkbox
Checkbox
2 parents a7f3503 + c00e865 commit d686e55

File tree

2 files changed

+117
-47
lines changed

2 files changed

+117
-47
lines changed

__tests__/events.js

+96-42
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,64 @@ import userEvent from "../src";
66
afterEach(cleanup);
77

88
describe("fireEvent.click", () => {
9-
it("should fire the correct events for <input>", () => {
9+
it.each(["input", "textarea"])(
10+
"should fire the correct events for <%s>",
11+
type => {
12+
const onMouseOver = jest.fn();
13+
const onMouseMove = jest.fn();
14+
const onMouseDown = jest.fn();
15+
const onFocus = jest.fn();
16+
const onMouseUp = jest.fn();
17+
const onClick = jest.fn();
18+
const { getByTestId } = render(
19+
React.createElement(type, {
20+
"data-testid": "element",
21+
onMouseOver: onMouseOver,
22+
onMouseMove: onMouseMove,
23+
onMouseDown: onMouseDown,
24+
onFocus: onFocus,
25+
onMouseUp: onMouseUp,
26+
onClick: onClick
27+
})
28+
);
29+
30+
expect(onMouseOver).not.toHaveBeenCalled();
31+
expect(onMouseMove).not.toHaveBeenCalled();
32+
expect(onMouseDown).not.toHaveBeenCalled();
33+
expect(onFocus).not.toHaveBeenCalled();
34+
expect(onMouseUp).not.toHaveBeenCalled();
35+
expect(onClick).not.toHaveBeenCalled();
36+
37+
userEvent.click(getByTestId("element"));
38+
39+
expect(onMouseOver).toHaveBeenCalledTimes(1);
40+
expect(onMouseMove).toHaveBeenCalledTimes(1);
41+
expect(onMouseDown).toHaveBeenCalledTimes(1);
42+
expect(onFocus).toHaveBeenCalledTimes(1);
43+
expect(onMouseUp).toHaveBeenCalledTimes(1);
44+
expect(onClick).toHaveBeenCalledTimes(1);
45+
}
46+
);
47+
48+
it('should fire the correct events for <input type="checkbox">', () => {
1049
const onMouseOver = jest.fn();
1150
const onMouseMove = jest.fn();
1251
const onMouseDown = jest.fn();
1352
const onFocus = jest.fn();
1453
const onMouseUp = jest.fn();
1554
const onClick = jest.fn();
55+
const onChange = jest.fn();
1656
const { getByTestId } = render(
1757
<input
18-
data-testid="input"
58+
data-testid="element"
59+
type="checkbox"
1960
onMouseOver={onMouseOver}
2061
onMouseMove={onMouseMove}
2162
onMouseDown={onMouseDown}
2263
onFocus={onFocus}
2364
onMouseUp={onMouseUp}
2465
onClick={onClick}
66+
onChange={onChange}
2567
/>
2668
);
2769

@@ -31,15 +73,18 @@ describe("fireEvent.click", () => {
3173
expect(onFocus).not.toHaveBeenCalled();
3274
expect(onMouseUp).not.toHaveBeenCalled();
3375
expect(onClick).not.toHaveBeenCalled();
76+
expect(onChange).not.toHaveBeenCalled();
3477

35-
userEvent.click(getByTestId("input"));
78+
userEvent.click(getByTestId("element"));
3679

3780
expect(onMouseOver).toHaveBeenCalledTimes(1);
3881
expect(onMouseMove).toHaveBeenCalledTimes(1);
3982
expect(onMouseDown).toHaveBeenCalledTimes(1);
40-
expect(onFocus).toHaveBeenCalledTimes(1);
83+
expect(onFocus).not.toHaveBeenCalledTimes(1);
4184
expect(onMouseUp).toHaveBeenCalledTimes(1);
4285
expect(onClick).toHaveBeenCalledTimes(1);
86+
expect(onChange).toHaveBeenCalledTimes(1);
87+
expect(getByTestId("element")).toHaveProperty("checked", true);
4388
});
4489

4590
it("should fire the correct events for <div>", () => {
@@ -101,42 +146,51 @@ describe("fireEvent.click", () => {
101146
expect(a).not.toHaveFocus();
102147
});
103148

104-
it("gives focus when clicking a <label> with htmlFor", () => {
105-
const { getByTestId } = render(
106-
<React.Fragment>
107-
<label htmlFor="input" data-testid="label">
108-
Label
109-
</label>
110-
<input id="input" data-testid="input" />
111-
</React.Fragment>
112-
);
113-
userEvent.click(getByTestId("label"));
114-
expect(getByTestId("input")).toHaveFocus();
115-
});
116-
117-
it("gives focus when clicking a <label> without htmlFor", () => {
118-
const { getByTestId } = render(
119-
<React.Fragment>
120-
<label data-testid="label">
121-
Label
122-
<input data-testid="input" />
123-
</label>
124-
</React.Fragment>
125-
);
126-
userEvent.click(getByTestId("label"));
127-
expect(getByTestId("input")).toHaveFocus();
128-
});
129-
130-
it("gives focus when clicking on an element contained within a <label>", () => {
131-
const { getByText, getByTestId } = render(
132-
<React.Fragment>
133-
<label htmlFor="input" data-testid="label">
134-
<span>Label</span>
135-
</label>
136-
<input id="input" data-testid="input" />
137-
</React.Fragment>
138-
);
139-
userEvent.click(getByText("Label"));
140-
//expect(getByTestId('input')).toHaveFocus()
141-
});
149+
it.each(["input", "textarea"])(
150+
"gives focus to <%s> when clicking a <label> with htmlFor",
151+
type => {
152+
const { getByTestId } = render(
153+
<React.Fragment>
154+
<label htmlFor="input" data-testid="label">
155+
Label
156+
</label>
157+
{React.createElement(type, { id: "input", "data-testid": "input" })}
158+
</React.Fragment>
159+
);
160+
userEvent.click(getByTestId("label"));
161+
expect(getByTestId("input")).toHaveFocus();
162+
}
163+
);
164+
165+
it.each(["input", "textarea"])(
166+
"gives focus to <%s> when clicking a <label> without htmlFor",
167+
type => {
168+
const { getByTestId } = render(
169+
<React.Fragment>
170+
<label data-testid="label">
171+
Label
172+
{React.createElement(type, { "data-testid": "input" })}
173+
</label>
174+
</React.Fragment>
175+
);
176+
userEvent.click(getByTestId("label"));
177+
expect(getByTestId("input")).toHaveFocus();
178+
}
179+
);
180+
181+
it.each(["input", "textarea"])(
182+
"gives focus to <%s> when clicking on an element contained within a <label>",
183+
type => {
184+
const { getByText, getByTestId } = render(
185+
<React.Fragment>
186+
<label htmlFor="input" data-testid="label">
187+
<span>Label</span>
188+
</label>
189+
{React.createElement(type, { id: "input", "data-testid": "input" })}
190+
</React.Fragment>
191+
);
192+
userEvent.click(getByText("Label"));
193+
expect(getByTestId("input")).toHaveFocus();
194+
}
195+
);
142196
});

src/index.js

+21-5
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,23 @@ function clickLabel(label) {
1818
input.focus();
1919
fireEvent.click(label);
2020
} else {
21-
const input = label.querySelector("input");
21+
const input = label.querySelector("input,textarea");
2222
input.focus();
2323
label.focus();
2424
fireEvent.click(input);
2525
fireEvent.click(label);
2626
}
2727
}
2828

29+
function clickCheckbox(checkbox) {
30+
fireEvent.mouseOver(checkbox);
31+
fireEvent.mouseMove(checkbox);
32+
fireEvent.mouseDown(checkbox);
33+
fireEvent.mouseUp(checkbox);
34+
fireEvent.click(checkbox);
35+
fireEvent.change(checkbox);
36+
}
37+
2938
function clickElement(element) {
3039
fireEvent.mouseOver(element);
3140
fireEvent.mouseMove(element);
@@ -48,10 +57,17 @@ const userEvent = {
4857
fireEvent.mouseLeave(focusedElement);
4958
}
5059

51-
if (element.tagName === "LABEL") {
52-
clickLabel(element);
53-
} else {
54-
clickElement(element);
60+
switch (element.tagName) {
61+
case "LABEL":
62+
clickLabel(element);
63+
break;
64+
case "INPUT":
65+
if (element.type === "checkbox") {
66+
clickCheckbox(element);
67+
break;
68+
}
69+
default:
70+
clickElement(element);
5571
}
5672

5773
wasAnotherElementFocused && focusedElement.blur();

0 commit comments

Comments
 (0)