@testing-library/user-event 10.3.2 → 10.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +5 -0
- package/LICENSE +2 -3
- package/README.md +201 -107
- package/dist/index.js +326 -252
- package/package.json +42 -43
- package/typings/index.d.ts +24 -20
- package/.all-contributorsrc +0 -311
- package/.babelrc +0 -13
- package/.gitattributes +0 -1
- package/.nvmrc +0 -1
- package/.prettierrc.js +0 -3
- package/.travis.yml +0 -30
- package/__tests__/react/clear.js +0 -94
- package/__tests__/react/click.js +0 -437
- package/__tests__/react/dblclick.js +0 -211
- package/__tests__/react/selectoptions.js +0 -282
- package/__tests__/react/tab.js +0 -277
- package/__tests__/react/type.js +0 -265
- package/__tests__/react/upload.js +0 -124
- package/__tests__/vue/click.js +0 -342
- package/__tests__/vue/dblclick.js +0 -126
- package/__tests__/vue/selectoptions.js +0 -254
- package/__tests__/vue/type.js +0 -240
- package/lint-staged.config.js +0 -21
- package/other/dog.png +0 -0
- package/src/index.js +0 -373
|
@@ -1,282 +0,0 @@
|
|
|
1
|
-
import React from "react";
|
|
2
|
-
import { render, cleanup, fireEvent } from "@testing-library/react";
|
|
3
|
-
import "@testing-library/jest-dom/extend-expect";
|
|
4
|
-
import userEvent from "../../src";
|
|
5
|
-
|
|
6
|
-
afterEach(cleanup);
|
|
7
|
-
|
|
8
|
-
describe("userEvent.selectOptions", () => {
|
|
9
|
-
it.each(["select", "select multiple"])(
|
|
10
|
-
"should fire the correct events for <%s>",
|
|
11
|
-
(type) => {
|
|
12
|
-
const events = [];
|
|
13
|
-
const eventsHandler = jest.fn((evt) => events.push(evt.type));
|
|
14
|
-
const multiple = type === "select multiple";
|
|
15
|
-
const eventHandlers = {
|
|
16
|
-
onMouseOver: eventsHandler,
|
|
17
|
-
onMouseMove: eventsHandler,
|
|
18
|
-
onMouseDown: eventsHandler,
|
|
19
|
-
onFocus: eventsHandler,
|
|
20
|
-
onMouseUp: eventsHandler,
|
|
21
|
-
onClick: eventsHandler,
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
const { getByTestId } = render(
|
|
25
|
-
<select {...{ ...eventHandlers, multiple }} data-testid="element">
|
|
26
|
-
<option value="1">1</option>
|
|
27
|
-
<option value="2">2</option>
|
|
28
|
-
<option value="3">3</option>
|
|
29
|
-
</select>
|
|
30
|
-
);
|
|
31
|
-
|
|
32
|
-
userEvent.selectOptions(getByTestId("element"), "1");
|
|
33
|
-
|
|
34
|
-
expect(events).toEqual([
|
|
35
|
-
"mouseover",
|
|
36
|
-
"mousemove",
|
|
37
|
-
"mousedown",
|
|
38
|
-
"focus",
|
|
39
|
-
"mouseup",
|
|
40
|
-
"click",
|
|
41
|
-
"mouseover", // The events repeat because we click on the child OPTION too
|
|
42
|
-
"mousemove", // But these specifically are the events bubbling up to the <select>
|
|
43
|
-
"mousedown",
|
|
44
|
-
"focus",
|
|
45
|
-
"mouseup",
|
|
46
|
-
"click",
|
|
47
|
-
]);
|
|
48
|
-
}
|
|
49
|
-
);
|
|
50
|
-
|
|
51
|
-
it("should fire the correct events on selected OPTION child with <select>", () => {
|
|
52
|
-
function handleEvent(evt) {
|
|
53
|
-
const optValue = parseInt(evt.target.value);
|
|
54
|
-
events[optValue] = [...(events[optValue] || []), evt.type];
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
const events = [];
|
|
58
|
-
const eventsHandler = jest.fn(handleEvent);
|
|
59
|
-
const eventHandlers = {
|
|
60
|
-
onMouseOver: eventsHandler,
|
|
61
|
-
onMouseMove: eventsHandler,
|
|
62
|
-
onMouseDown: eventsHandler,
|
|
63
|
-
onFocus: eventsHandler,
|
|
64
|
-
onMouseUp: eventsHandler,
|
|
65
|
-
onClick: eventsHandler,
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
const { getByTestId } = render(
|
|
69
|
-
<select data-testid="element">
|
|
70
|
-
<option {...eventHandlers} value="1">
|
|
71
|
-
1
|
|
72
|
-
</option>
|
|
73
|
-
<option {...eventHandlers} value="2">
|
|
74
|
-
2
|
|
75
|
-
</option>
|
|
76
|
-
<option {...eventHandlers} value="3">
|
|
77
|
-
3
|
|
78
|
-
</option>
|
|
79
|
-
</select>
|
|
80
|
-
);
|
|
81
|
-
|
|
82
|
-
userEvent.selectOptions(getByTestId("element"), ["2"]);
|
|
83
|
-
|
|
84
|
-
expect(events[1]).toBe(undefined);
|
|
85
|
-
expect(events[3]).toBe(undefined);
|
|
86
|
-
expect(events[2]).toEqual([
|
|
87
|
-
"mouseover",
|
|
88
|
-
"mousemove",
|
|
89
|
-
"mousedown",
|
|
90
|
-
"focus",
|
|
91
|
-
"mouseup",
|
|
92
|
-
"click",
|
|
93
|
-
]);
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
it("should fire the correct events on selected OPTION children with <select multiple>", () => {
|
|
97
|
-
function handleEvent(evt) {
|
|
98
|
-
const optValue = parseInt(evt.target.value);
|
|
99
|
-
events[optValue] = [...(events[optValue] || []), evt.type];
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
const events = [];
|
|
103
|
-
const eventsHandler = jest.fn(handleEvent);
|
|
104
|
-
const eventHandlers = {
|
|
105
|
-
onMouseOver: eventsHandler,
|
|
106
|
-
onMouseMove: eventsHandler,
|
|
107
|
-
onMouseDown: eventsHandler,
|
|
108
|
-
onFocus: eventsHandler,
|
|
109
|
-
onMouseUp: eventsHandler,
|
|
110
|
-
onClick: eventsHandler,
|
|
111
|
-
};
|
|
112
|
-
|
|
113
|
-
const { getByTestId } = render(
|
|
114
|
-
<select multiple data-testid="element">
|
|
115
|
-
<option {...eventHandlers} value="1">
|
|
116
|
-
1
|
|
117
|
-
</option>
|
|
118
|
-
<option {...eventHandlers} value="2">
|
|
119
|
-
2
|
|
120
|
-
</option>
|
|
121
|
-
<option {...eventHandlers} value="3">
|
|
122
|
-
3
|
|
123
|
-
</option>
|
|
124
|
-
</select>
|
|
125
|
-
);
|
|
126
|
-
|
|
127
|
-
userEvent.selectOptions(getByTestId("element"), ["1", "3"]);
|
|
128
|
-
|
|
129
|
-
expect(events[2]).toBe(undefined);
|
|
130
|
-
expect(events[1]).toEqual([
|
|
131
|
-
"mouseover",
|
|
132
|
-
"mousemove",
|
|
133
|
-
"mousedown",
|
|
134
|
-
"focus",
|
|
135
|
-
"mouseup",
|
|
136
|
-
"click",
|
|
137
|
-
]);
|
|
138
|
-
|
|
139
|
-
expect(events[3]).toEqual([
|
|
140
|
-
"mouseover",
|
|
141
|
-
"mousemove",
|
|
142
|
-
"mousedown",
|
|
143
|
-
"focus",
|
|
144
|
-
"mouseup",
|
|
145
|
-
"click",
|
|
146
|
-
]);
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
it("sets the selected prop on the selected OPTION", () => {
|
|
150
|
-
const onSubmit = jest.fn();
|
|
151
|
-
|
|
152
|
-
const { getByTestId } = render(
|
|
153
|
-
<form onSubmit={onSubmit}>
|
|
154
|
-
<select multiple data-testid="element">
|
|
155
|
-
<option data-testid="val1" value="1">
|
|
156
|
-
1
|
|
157
|
-
</option>
|
|
158
|
-
<option data-testid="val2" value="2">
|
|
159
|
-
2
|
|
160
|
-
</option>
|
|
161
|
-
<option data-testid="val3" value="3">
|
|
162
|
-
3
|
|
163
|
-
</option>
|
|
164
|
-
</select>
|
|
165
|
-
</form>
|
|
166
|
-
);
|
|
167
|
-
|
|
168
|
-
userEvent.selectOptions(getByTestId("element"), ["1", "3"]);
|
|
169
|
-
|
|
170
|
-
expect(getByTestId("val1").selected).toBe(true);
|
|
171
|
-
expect(getByTestId("val2").selected).toBe(false);
|
|
172
|
-
expect(getByTestId("val3").selected).toBe(true);
|
|
173
|
-
});
|
|
174
|
-
|
|
175
|
-
it("sets the selected prop on the selected OPTION using htmlFor", () => {
|
|
176
|
-
const onSubmit = jest.fn();
|
|
177
|
-
|
|
178
|
-
const { getByTestId } = render(
|
|
179
|
-
<form onSubmit={onSubmit}>
|
|
180
|
-
<label htmlFor="select">Example Select</label>
|
|
181
|
-
<select id="select" data-testid="element">
|
|
182
|
-
<option data-testid="val1" value="1">
|
|
183
|
-
1
|
|
184
|
-
</option>
|
|
185
|
-
<option data-testid="val2" value="2">
|
|
186
|
-
2
|
|
187
|
-
</option>
|
|
188
|
-
<option data-testid="val3" value="3">
|
|
189
|
-
3
|
|
190
|
-
</option>
|
|
191
|
-
</select>
|
|
192
|
-
</form>
|
|
193
|
-
);
|
|
194
|
-
|
|
195
|
-
userEvent.selectOptions(getByTestId("element"), "2");
|
|
196
|
-
|
|
197
|
-
expect(getByTestId("val1").selected).toBe(false);
|
|
198
|
-
expect(getByTestId("val2").selected).toBe(true);
|
|
199
|
-
expect(getByTestId("val3").selected).toBe(false);
|
|
200
|
-
});
|
|
201
|
-
|
|
202
|
-
it("should fire onChange event on a SELECT element", () => {
|
|
203
|
-
const onChangeHandler = jest.fn();
|
|
204
|
-
|
|
205
|
-
const { getByTestId } = render(
|
|
206
|
-
<select data-testid="element" onChange={onChangeHandler}>
|
|
207
|
-
<option data-testid="val1" value="1">
|
|
208
|
-
1
|
|
209
|
-
</option>
|
|
210
|
-
<option data-testid="val2" value="2">
|
|
211
|
-
2
|
|
212
|
-
</option>
|
|
213
|
-
<option data-testid="val3" value="3">
|
|
214
|
-
3
|
|
215
|
-
</option>
|
|
216
|
-
</select>
|
|
217
|
-
);
|
|
218
|
-
|
|
219
|
-
userEvent.selectOptions(getByTestId("element"), "2");
|
|
220
|
-
|
|
221
|
-
expect(onChangeHandler).toBeCalled();
|
|
222
|
-
});
|
|
223
|
-
|
|
224
|
-
it("sets the selected prop on the selected OPTION using nested SELECT", () => {
|
|
225
|
-
const onSubmit = jest.fn();
|
|
226
|
-
|
|
227
|
-
const { getByTestId } = render(
|
|
228
|
-
<form onSubmit={onSubmit}>
|
|
229
|
-
<label>
|
|
230
|
-
Example Select
|
|
231
|
-
<select data-testid="element">
|
|
232
|
-
<option data-testid="val1" value="1">
|
|
233
|
-
1
|
|
234
|
-
</option>
|
|
235
|
-
<option data-testid="val2" value="2">
|
|
236
|
-
2
|
|
237
|
-
</option>
|
|
238
|
-
<option data-testid="val3" value="3">
|
|
239
|
-
3
|
|
240
|
-
</option>
|
|
241
|
-
</select>
|
|
242
|
-
</label>
|
|
243
|
-
</form>
|
|
244
|
-
);
|
|
245
|
-
|
|
246
|
-
userEvent.selectOptions(getByTestId("element"), "2");
|
|
247
|
-
|
|
248
|
-
expect(getByTestId("val1").selected).toBe(false);
|
|
249
|
-
expect(getByTestId("val2").selected).toBe(true);
|
|
250
|
-
expect(getByTestId("val3").selected).toBe(false);
|
|
251
|
-
});
|
|
252
|
-
|
|
253
|
-
it("sets the selected prop on the selected OPTION using OPTGROUPS", () => {
|
|
254
|
-
const { getByTestId } = render(
|
|
255
|
-
<form>
|
|
256
|
-
<select multiple data-testid="element">
|
|
257
|
-
<optgroup label="test optgroup 1">
|
|
258
|
-
<option data-testid="val1" value="1">
|
|
259
|
-
1
|
|
260
|
-
</option>
|
|
261
|
-
</optgroup>
|
|
262
|
-
<optgroup label="test optgroup 2">
|
|
263
|
-
<option data-testid="val2" value="2">
|
|
264
|
-
2
|
|
265
|
-
</option>
|
|
266
|
-
</optgroup>
|
|
267
|
-
<optgroup label="test optgroup 1">
|
|
268
|
-
<option data-testid="val3" value="3">
|
|
269
|
-
3
|
|
270
|
-
</option>
|
|
271
|
-
</optgroup>
|
|
272
|
-
</select>
|
|
273
|
-
</form>
|
|
274
|
-
);
|
|
275
|
-
|
|
276
|
-
userEvent.selectOptions(getByTestId("element"), ["1", "3"]);
|
|
277
|
-
|
|
278
|
-
expect(getByTestId("val1").selected).toBe(true);
|
|
279
|
-
expect(getByTestId("val2").selected).toBe(false);
|
|
280
|
-
expect(getByTestId("val3").selected).toBe(true);
|
|
281
|
-
});
|
|
282
|
-
});
|
package/__tests__/react/tab.js
DELETED
|
@@ -1,277 +0,0 @@
|
|
|
1
|
-
import React from "react";
|
|
2
|
-
import { render } from "@testing-library/react";
|
|
3
|
-
import "@testing-library/jest-dom/extend-expect";
|
|
4
|
-
import userEvent from "../../src";
|
|
5
|
-
|
|
6
|
-
describe("userEvent.tab", () => {
|
|
7
|
-
it("should cycle elements in document tab order", () => {
|
|
8
|
-
const { getAllByTestId } = render(
|
|
9
|
-
<div>
|
|
10
|
-
<input data-testid="element" type="checkbox" />
|
|
11
|
-
<input data-testid="element" type="radio" />
|
|
12
|
-
<input data-testid="element" type="number" />
|
|
13
|
-
</div>
|
|
14
|
-
);
|
|
15
|
-
|
|
16
|
-
const [checkbox, radio, number] = getAllByTestId("element");
|
|
17
|
-
|
|
18
|
-
expect(document.body).toHaveFocus();
|
|
19
|
-
|
|
20
|
-
userEvent.tab();
|
|
21
|
-
|
|
22
|
-
expect(checkbox).toHaveFocus();
|
|
23
|
-
|
|
24
|
-
userEvent.tab();
|
|
25
|
-
|
|
26
|
-
expect(radio).toHaveFocus();
|
|
27
|
-
|
|
28
|
-
userEvent.tab();
|
|
29
|
-
|
|
30
|
-
expect(number).toHaveFocus();
|
|
31
|
-
|
|
32
|
-
userEvent.tab();
|
|
33
|
-
|
|
34
|
-
// cycle goes back to first element
|
|
35
|
-
expect(checkbox).toHaveFocus();
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
it("should go backwards when shift = true", () => {
|
|
39
|
-
const { getAllByTestId } = render(
|
|
40
|
-
<div>
|
|
41
|
-
<input data-testid="element" type="checkbox" />
|
|
42
|
-
<input data-testid="element" type="radio" />
|
|
43
|
-
<input data-testid="element" type="number" />
|
|
44
|
-
</div>
|
|
45
|
-
);
|
|
46
|
-
|
|
47
|
-
const [checkbox, radio, number] = getAllByTestId("element");
|
|
48
|
-
|
|
49
|
-
radio.focus();
|
|
50
|
-
|
|
51
|
-
userEvent.tab({ shift: true });
|
|
52
|
-
|
|
53
|
-
expect(checkbox).toHaveFocus();
|
|
54
|
-
|
|
55
|
-
userEvent.tab({ shift: true });
|
|
56
|
-
|
|
57
|
-
expect(number).toHaveFocus();
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
it("should respect tabindex, regardless of dom position", () => {
|
|
61
|
-
const { getAllByTestId } = render(
|
|
62
|
-
<div>
|
|
63
|
-
<input data-testid="element" tabIndex={2} type="checkbox" />
|
|
64
|
-
<input data-testid="element" tabIndex={1} type="radio" />
|
|
65
|
-
<input data-testid="element" tabIndex={3} type="number" />
|
|
66
|
-
</div>
|
|
67
|
-
);
|
|
68
|
-
|
|
69
|
-
const [checkbox, radio, number] = getAllByTestId("element");
|
|
70
|
-
|
|
71
|
-
userEvent.tab();
|
|
72
|
-
|
|
73
|
-
expect(radio).toHaveFocus();
|
|
74
|
-
|
|
75
|
-
userEvent.tab();
|
|
76
|
-
|
|
77
|
-
expect(checkbox).toHaveFocus();
|
|
78
|
-
|
|
79
|
-
userEvent.tab();
|
|
80
|
-
|
|
81
|
-
expect(number).toHaveFocus();
|
|
82
|
-
|
|
83
|
-
userEvent.tab();
|
|
84
|
-
|
|
85
|
-
expect(radio).toHaveFocus();
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
it("should respect dom order when tabindex are all the same", () => {
|
|
89
|
-
const { getAllByTestId } = render(
|
|
90
|
-
<div>
|
|
91
|
-
<input data-testid="element" tabIndex={0} type="checkbox" />
|
|
92
|
-
<input data-testid="element" tabIndex={1} type="radio" />
|
|
93
|
-
<input data-testid="element" tabIndex={0} type="number" />
|
|
94
|
-
</div>
|
|
95
|
-
);
|
|
96
|
-
|
|
97
|
-
const [checkbox, radio, number] = getAllByTestId("element");
|
|
98
|
-
|
|
99
|
-
userEvent.tab();
|
|
100
|
-
|
|
101
|
-
expect(checkbox).toHaveFocus();
|
|
102
|
-
|
|
103
|
-
userEvent.tab();
|
|
104
|
-
|
|
105
|
-
expect(number).toHaveFocus();
|
|
106
|
-
|
|
107
|
-
userEvent.tab();
|
|
108
|
-
|
|
109
|
-
expect(radio).toHaveFocus();
|
|
110
|
-
|
|
111
|
-
userEvent.tab();
|
|
112
|
-
|
|
113
|
-
expect(checkbox).toHaveFocus();
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
it("should suport a mix of elements with/without tab index", () => {
|
|
117
|
-
const { getAllByTestId } = render(
|
|
118
|
-
<div>
|
|
119
|
-
<input data-testid="element" tabIndex={0} type="checkbox" />
|
|
120
|
-
<input data-testid="element" tabIndex={1} type="radio" />
|
|
121
|
-
<input data-testid="element" type="number" />
|
|
122
|
-
</div>
|
|
123
|
-
);
|
|
124
|
-
|
|
125
|
-
const [checkbox, radio, number] = getAllByTestId("element");
|
|
126
|
-
|
|
127
|
-
userEvent.tab();
|
|
128
|
-
|
|
129
|
-
expect(checkbox).toHaveFocus();
|
|
130
|
-
userEvent.tab();
|
|
131
|
-
|
|
132
|
-
expect(number).toHaveFocus();
|
|
133
|
-
userEvent.tab();
|
|
134
|
-
|
|
135
|
-
expect(radio).toHaveFocus();
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
it("should not tab to <a> with no href", () => {
|
|
139
|
-
const { getAllByTestId } = render(
|
|
140
|
-
<div>
|
|
141
|
-
<input data-testid="element" tabIndex={0} type="checkbox" />
|
|
142
|
-
<a>ignore this</a>
|
|
143
|
-
<a data-testid="element" href="http://www.testingjavascript.com">
|
|
144
|
-
a link
|
|
145
|
-
</a>
|
|
146
|
-
</div>
|
|
147
|
-
);
|
|
148
|
-
|
|
149
|
-
const [checkbox, link] = getAllByTestId("element");
|
|
150
|
-
|
|
151
|
-
userEvent.tab();
|
|
152
|
-
|
|
153
|
-
expect(checkbox).toHaveFocus();
|
|
154
|
-
|
|
155
|
-
userEvent.tab();
|
|
156
|
-
|
|
157
|
-
expect(link).toHaveFocus();
|
|
158
|
-
});
|
|
159
|
-
|
|
160
|
-
it("should stay within a focus trap", () => {
|
|
161
|
-
const { getAllByTestId, getByTestId } = render(
|
|
162
|
-
<>
|
|
163
|
-
<div data-testid="div1">
|
|
164
|
-
<input data-testid="element" type="checkbox" />
|
|
165
|
-
<input data-testid="element" type="radio" />
|
|
166
|
-
<input data-testid="element" type="number" />
|
|
167
|
-
</div>
|
|
168
|
-
<div data-testid="div2">
|
|
169
|
-
<input data-testid="element" foo="bar" type="checkbox" />
|
|
170
|
-
<input data-testid="element" foo="bar" type="radio" />
|
|
171
|
-
<input data-testid="element" foo="bar" type="number" />
|
|
172
|
-
</div>
|
|
173
|
-
</>
|
|
174
|
-
);
|
|
175
|
-
|
|
176
|
-
const [div1, div2] = [getByTestId("div1"), getByTestId("div2")];
|
|
177
|
-
const [
|
|
178
|
-
checkbox1,
|
|
179
|
-
radio1,
|
|
180
|
-
number1,
|
|
181
|
-
checkbox2,
|
|
182
|
-
radio2,
|
|
183
|
-
number2,
|
|
184
|
-
] = getAllByTestId("element");
|
|
185
|
-
|
|
186
|
-
expect(document.body).toHaveFocus();
|
|
187
|
-
|
|
188
|
-
userEvent.tab({ focusTrap: div1 });
|
|
189
|
-
|
|
190
|
-
expect(checkbox1).toHaveFocus();
|
|
191
|
-
|
|
192
|
-
userEvent.tab({ focusTrap: div1 });
|
|
193
|
-
|
|
194
|
-
expect(radio1).toHaveFocus();
|
|
195
|
-
|
|
196
|
-
userEvent.tab({ focusTrap: div1 });
|
|
197
|
-
|
|
198
|
-
expect(number1).toHaveFocus();
|
|
199
|
-
|
|
200
|
-
userEvent.tab({ focusTrap: div1 });
|
|
201
|
-
|
|
202
|
-
// cycle goes back to first element
|
|
203
|
-
expect(checkbox1).toHaveFocus();
|
|
204
|
-
|
|
205
|
-
userEvent.tab({ focusTrap: div2 });
|
|
206
|
-
|
|
207
|
-
expect(checkbox2).toHaveFocus();
|
|
208
|
-
|
|
209
|
-
userEvent.tab({ focusTrap: div2 });
|
|
210
|
-
|
|
211
|
-
expect(radio2).toHaveFocus();
|
|
212
|
-
|
|
213
|
-
userEvent.tab({ focusTrap: div2 });
|
|
214
|
-
|
|
215
|
-
expect(number2).toHaveFocus();
|
|
216
|
-
|
|
217
|
-
userEvent.tab({ focusTrap: div2 });
|
|
218
|
-
|
|
219
|
-
// cycle goes back to first element
|
|
220
|
-
expect(checkbox2).toHaveFocus();
|
|
221
|
-
});
|
|
222
|
-
|
|
223
|
-
// prior to node 11, Array.sort was unstable for arrays w/ length > 10.
|
|
224
|
-
// https://twitter.com/mathias/status/1036626116654637057
|
|
225
|
-
// for this reason, the tab() function needs to account for this in it's sorting.
|
|
226
|
-
// for example under node 10 in this test:
|
|
227
|
-
// > 'abcdefghijklmnopqrstuvwxyz'.split('').sort(() => 0).join('')
|
|
228
|
-
// will give you 'nacdefghijklmbopqrstuvwxyz'
|
|
229
|
-
it("should support unstable sorting environments like node 10", () => {
|
|
230
|
-
const letters = "abcdefghijklmnopqrstuvwxyz";
|
|
231
|
-
|
|
232
|
-
const { getByTestId } = render(
|
|
233
|
-
<>
|
|
234
|
-
{letters.split("").map((letter) => (
|
|
235
|
-
<input key={letter} type="text" data-testid={letter} />
|
|
236
|
-
))}
|
|
237
|
-
</>
|
|
238
|
-
);
|
|
239
|
-
|
|
240
|
-
expect.assertions(26);
|
|
241
|
-
|
|
242
|
-
letters.split("").forEach((letter) => {
|
|
243
|
-
userEvent.tab();
|
|
244
|
-
expect(getByTestId(letter)).toHaveFocus();
|
|
245
|
-
});
|
|
246
|
-
});
|
|
247
|
-
});
|
|
248
|
-
|
|
249
|
-
it("should not focus disabled elements", () => {
|
|
250
|
-
const { getByTestId } = render(
|
|
251
|
-
<div>
|
|
252
|
-
<input data-testid="one" />
|
|
253
|
-
<input tabIndex={-1} />
|
|
254
|
-
<button disabled>click</button>
|
|
255
|
-
<input disabled />
|
|
256
|
-
<input data-testid="five" />
|
|
257
|
-
</div>
|
|
258
|
-
);
|
|
259
|
-
|
|
260
|
-
const [one, five] = [getByTestId("one"), getByTestId("five")];
|
|
261
|
-
|
|
262
|
-
userEvent.tab();
|
|
263
|
-
expect(one).toHaveFocus();
|
|
264
|
-
|
|
265
|
-
userEvent.tab();
|
|
266
|
-
expect(five).toHaveFocus();
|
|
267
|
-
});
|
|
268
|
-
|
|
269
|
-
it("should keep focus on the document if there are no enabled, focusable elements", () => {
|
|
270
|
-
render(<button disabled>no clicky</button>);
|
|
271
|
-
|
|
272
|
-
userEvent.tab();
|
|
273
|
-
expect(document.body).toHaveFocus();
|
|
274
|
-
|
|
275
|
-
userEvent.tab({ shift: true });
|
|
276
|
-
expect(document.body).toHaveFocus();
|
|
277
|
-
});
|