@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.
@@ -1,265 +0,0 @@
1
- import React from "react";
2
- import { cleanup, render, wait, 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.type", () => {
9
- it.each(["input", "textarea"])("should type text in <%s>", (type) => {
10
- const onChange = jest.fn();
11
- const { getByTestId } = render(
12
- React.createElement(type, {
13
- "data-testid": "input",
14
- onChange: onChange,
15
- })
16
- );
17
- const text = "Hello, world!";
18
- userEvent.type(getByTestId("input"), text);
19
- expect(onChange).toHaveBeenCalledTimes(text.length);
20
- expect(getByTestId("input")).toHaveProperty("value", text);
21
- });
22
-
23
- it("should append text one by one", () => {
24
- const onChange = jest.fn();
25
- const { getByTestId } = render(
26
- <input data-testid="input" onChange={onChange} />
27
- );
28
- userEvent.type(getByTestId("input"), "hello");
29
- userEvent.type(getByTestId("input"), " world");
30
- expect(onChange).toHaveBeenCalledTimes("hello world".length);
31
- expect(getByTestId("input")).toHaveProperty("value", "hello world");
32
- });
33
-
34
- it("should append text all at once", () => {
35
- const onChange = jest.fn();
36
- const { getByTestId } = render(
37
- <input data-testid="input" onChange={onChange} />
38
- );
39
- userEvent.type(getByTestId("input"), "hello", { allAtOnce: true });
40
- userEvent.type(getByTestId("input"), " world", { allAtOnce: true });
41
- expect(onChange).toHaveBeenCalledTimes(2);
42
- expect(getByTestId("input")).toHaveProperty("value", "hello world");
43
- });
44
-
45
- it("should not type when event.preventDefault() is called", () => {
46
- const onChange = jest.fn();
47
- const onKeydown = jest
48
- .fn()
49
- .mockImplementation((event) => event.preventDefault());
50
- const { getByTestId } = render(
51
- <input data-testid="input" onKeyDown={onKeydown} onChange={onChange} />
52
- );
53
- const text = "Hello, world!";
54
- userEvent.type(getByTestId("input"), text);
55
- expect(onKeydown).toHaveBeenCalledTimes(text.length);
56
- expect(onChange).toHaveBeenCalledTimes(0);
57
- expect(getByTestId("input")).not.toHaveProperty("value", text);
58
- });
59
-
60
- it.each(["input", "textarea"])(
61
- "should not type when <%s> is disabled",
62
- (type) => {
63
- const onChange = jest.fn();
64
- const { getByTestId } = render(
65
- React.createElement(type, {
66
- "data-testid": "input",
67
- onChange: onChange,
68
- disabled: true,
69
- })
70
- );
71
- const text = "Hello, world!";
72
- userEvent.type(getByTestId("input"), text);
73
- expect(onChange).not.toHaveBeenCalled();
74
- expect(getByTestId("input")).toHaveProperty("value", "");
75
- }
76
- );
77
-
78
- it.each(["input", "textarea"])(
79
- "should not type when <%s> is readOnly",
80
- (type) => {
81
- const onChange = jest.fn();
82
- const onKeyDown = jest.fn();
83
- const onKeyPress = jest.fn();
84
- const onKeyUp = jest.fn();
85
- const { getByTestId } = render(
86
- React.createElement(type, {
87
- "data-testid": "input",
88
- onChange,
89
- onKeyDown,
90
- onKeyPress,
91
- onKeyUp,
92
- readOnly: true,
93
- })
94
- );
95
- const text = "Hello, world!";
96
- userEvent.type(getByTestId("input"), text);
97
- expect(onKeyDown).toHaveBeenCalledTimes(text.length);
98
- expect(onKeyPress).toHaveBeenCalledTimes(text.length);
99
- expect(onKeyUp).toHaveBeenCalledTimes(text.length);
100
- expect(onChange).not.toHaveBeenCalled();
101
- expect(getByTestId("input")).toHaveProperty("value", "");
102
- }
103
- );
104
-
105
- it("should delay the typing when opts.delay is not 0", async () => {
106
- jest.useFakeTimers();
107
- const onChange = jest.fn();
108
- const onInput = jest.fn();
109
- const { getByTestId } = render(
110
- React.createElement("input", {
111
- "data-testid": "input",
112
- onInput,
113
- onChange,
114
- })
115
- );
116
- const text = "Hello, world!";
117
- const delay = 10;
118
- // Attach a native change listener because React cannot listen for text input change events
119
- userEvent.type(getByTestId("input"), text, {
120
- delay,
121
- });
122
- expect(onInput).not.toHaveBeenCalled();
123
- expect(getByTestId("input")).not.toHaveProperty("value", text);
124
-
125
- for (let i = 0; i < text.length; i++) {
126
- jest.advanceTimersByTime(delay);
127
- await wait(() => expect(onInput).toHaveBeenCalledTimes(i + 1));
128
- expect(onChange).toHaveBeenCalledTimes(i + 1);
129
- expect(getByTestId("input")).toHaveProperty(
130
- "value",
131
- text.slice(0, i + 1)
132
- );
133
- }
134
- // Blurring the input "commits" the value, React's onChange should not fire
135
- fireEvent.blur(getByTestId("input"));
136
- await wait(() => expect(onChange).toHaveBeenCalledTimes(text.length), {
137
- timeout: 300,
138
- });
139
- });
140
-
141
- it.each(["input", "textarea"])(
142
- "should type text in <%s> all at once",
143
- (type) => {
144
- const onChange = jest.fn();
145
- const { getByTestId } = render(
146
- React.createElement(type, {
147
- "data-testid": "input",
148
- onChange: onChange,
149
- })
150
- );
151
- const text = "Hello, world!";
152
- userEvent.type(getByTestId("input"), text, {
153
- allAtOnce: true,
154
- });
155
-
156
- expect(onChange).toHaveBeenCalledTimes(1);
157
- expect(getByTestId("input")).toHaveProperty("value", text);
158
- }
159
- );
160
-
161
- it.each(["input", "textarea"])(
162
- "should enter text in <%s> up to maxLength if provided",
163
- (type) => {
164
- const onChange = jest.fn();
165
- const onKeyDown = jest.fn();
166
- const onKeyPress = jest.fn();
167
- const onKeyUp = jest.fn();
168
- const maxLength = 10;
169
-
170
- const { getByTestId } = render(
171
- React.createElement(type, {
172
- "data-testid": "input",
173
- onChange,
174
- onKeyDown,
175
- onKeyPress,
176
- onKeyUp,
177
- maxLength,
178
- })
179
- );
180
-
181
- const text = "superlongtext";
182
- const slicedText = text.slice(0, maxLength);
183
-
184
- const inputEl = getByTestId("input");
185
-
186
- userEvent.type(inputEl, text);
187
-
188
- expect(inputEl).toHaveProperty("value", slicedText);
189
- expect(onChange).toHaveBeenCalledTimes(slicedText.length);
190
- expect(onKeyPress).toHaveBeenCalledTimes(text.length);
191
- expect(onKeyDown).toHaveBeenCalledTimes(text.length);
192
- expect(onKeyUp).toHaveBeenCalledTimes(text.length);
193
-
194
- inputEl.value = "";
195
- onChange.mockClear();
196
- onKeyPress.mockClear();
197
- onKeyDown.mockClear();
198
- onKeyUp.mockClear();
199
-
200
- userEvent.type(inputEl, text, {
201
- allAtOnce: true,
202
- });
203
-
204
- expect(inputEl).toHaveProperty("value", slicedText);
205
- expect(onChange).toHaveBeenCalledTimes(1);
206
- expect(onKeyPress).not.toHaveBeenCalled();
207
- expect(onKeyDown).not.toHaveBeenCalled();
208
- expect(onKeyUp).not.toHaveBeenCalled();
209
- }
210
- );
211
-
212
- it.each(["input", "textarea"])(
213
- "should append text in <%s> up to maxLength if provided",
214
- (type) => {
215
- const onChange = jest.fn();
216
- const onKeyDown = jest.fn();
217
- const onKeyPress = jest.fn();
218
- const onKeyUp = jest.fn();
219
- const maxLength = 10;
220
-
221
- const { getByTestId } = render(
222
- React.createElement(type, {
223
- "data-testid": "input",
224
- onChange,
225
- onKeyDown,
226
- onKeyPress,
227
- onKeyUp,
228
- maxLength,
229
- })
230
- );
231
-
232
- const text1 = "superlong";
233
- const text2 = "text";
234
- const text = text1 + text2;
235
- const slicedText = text.slice(0, maxLength);
236
-
237
- const inputEl = getByTestId("input");
238
-
239
- userEvent.type(inputEl, text1);
240
- userEvent.type(inputEl, text2);
241
-
242
- expect(inputEl).toHaveProperty("value", slicedText);
243
- expect(onChange).toHaveBeenCalledTimes(slicedText.length);
244
- expect(onKeyPress).toHaveBeenCalledTimes(text.length);
245
- expect(onKeyDown).toHaveBeenCalledTimes(text.length);
246
- expect(onKeyUp).toHaveBeenCalledTimes(text.length);
247
-
248
- inputEl.value = "";
249
- onChange.mockClear();
250
- onKeyPress.mockClear();
251
- onKeyDown.mockClear();
252
- onKeyUp.mockClear();
253
-
254
- userEvent.type(inputEl, text, {
255
- allAtOnce: true,
256
- });
257
-
258
- expect(inputEl).toHaveProperty("value", slicedText);
259
- expect(onChange).toHaveBeenCalledTimes(1);
260
- expect(onKeyPress).not.toHaveBeenCalled();
261
- expect(onKeyDown).not.toHaveBeenCalled();
262
- expect(onKeyUp).not.toHaveBeenCalled();
263
- }
264
- );
265
- });
@@ -1,124 +0,0 @@
1
- import React from "react";
2
- import { cleanup, render } 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.upload", () => {
9
- it("should fire the correct events for input", () => {
10
- const file = new File(["hello"], "hello.png", { type: "image/png" });
11
- const events = [];
12
- const eventsHandler = jest.fn((evt) => events.push(evt.type));
13
- const eventHandlers = {
14
- onMouseOver: eventsHandler,
15
- onMouseMove: eventsHandler,
16
- onMouseDown: eventsHandler,
17
- onFocus: eventsHandler,
18
- onMouseUp: eventsHandler,
19
- onClick: eventsHandler,
20
- };
21
-
22
- const { getByTestId } = render(
23
- <input type="file" data-testid="element" {...eventHandlers} />
24
- );
25
-
26
- userEvent.upload(getByTestId("element"), file);
27
-
28
- expect(events).toEqual([
29
- "mouseover",
30
- "mousemove",
31
- "mousedown",
32
- "focus",
33
- "mouseup",
34
- "click",
35
- ]);
36
- });
37
-
38
- it("should fire the correct events with label", () => {
39
- const file = new File(["hello"], "hello.png", { type: "image/png" });
40
-
41
- const inputEvents = [];
42
- const labelEvents = [];
43
- const eventsHandler = (events) => jest.fn((evt) => events.push(evt.type));
44
-
45
- const getEventHandlers = (events) => ({
46
- onMouseOver: eventsHandler(events),
47
- onMouseMove: eventsHandler(events),
48
- onMouseDown: eventsHandler(events),
49
- onFocus: eventsHandler(events),
50
- onMouseUp: eventsHandler(events),
51
- onClick: eventsHandler(events),
52
- });
53
-
54
- const { getByTestId } = render(
55
- <>
56
- <label
57
- htmlFor="element"
58
- data-testid="label"
59
- {...getEventHandlers(labelEvents)}
60
- >
61
- Element
62
- </label>
63
- <input type="file" id="element" {...getEventHandlers(inputEvents)} />
64
- </>
65
- );
66
-
67
- userEvent.upload(getByTestId("label"), file);
68
-
69
- expect(inputEvents).toEqual(["focus", "click"]);
70
- expect(labelEvents).toEqual([
71
- "mouseover",
72
- "mousemove",
73
- "mousedown",
74
- "mouseup",
75
- "click",
76
- ]);
77
- });
78
-
79
- it("should upload the file", () => {
80
- const file = new File(["hello"], "hello.png", { type: "image/png" });
81
- const { getByTestId } = render(<input type="file" data-testid="element" />);
82
- const input = getByTestId("element");
83
-
84
- userEvent.upload(input, file);
85
-
86
- expect(input.files[0]).toStrictEqual(file);
87
- expect(input.files.item(0)).toStrictEqual(file);
88
- expect(input.files).toHaveLength(1);
89
- });
90
-
91
- it("should upload multiple files", () => {
92
- const files = [
93
- new File(["hello"], "hello.png", { type: "image/png" }),
94
- new File(["there"], "there.png", { type: "image/png" }),
95
- ];
96
- const { getByTestId } = render(
97
- <input type="file" multiple data-testid="element" />
98
- );
99
- const input = getByTestId("element");
100
-
101
- userEvent.upload(input, files);
102
-
103
- expect(input.files[0]).toStrictEqual(files[0]);
104
- expect(input.files.item(0)).toStrictEqual(files[0]);
105
- expect(input.files[1]).toStrictEqual(files[1]);
106
- expect(input.files.item(1)).toStrictEqual(files[1]);
107
- expect(input.files).toHaveLength(2);
108
- });
109
-
110
- it("should not upload when is disabled", () => {
111
- const file = new File(["hello"], "hello.png", { type: "image/png" });
112
- const { getByTestId } = render(
113
- <input type="file" data-testid="element" disabled />
114
- );
115
-
116
- const input = getByTestId("element");
117
-
118
- userEvent.upload(input, file);
119
-
120
- expect(input.files[0]).toBeUndefined();
121
- expect(input.files.item(0)).toBeNull();
122
- expect(input.files).toHaveLength(0);
123
- });
124
- });