@khanacademy/wonder-blocks-form 4.4.4 → 4.4.6

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,6 +1,6 @@
1
1
  import * as React from "react";
2
2
  import {fireEvent, render, screen} from "@testing-library/react";
3
- import userEvent from "@testing-library/user-event";
3
+ import {userEvent} from "@testing-library/user-event";
4
4
 
5
5
  import {View} from "@khanacademy/wonder-blocks-core";
6
6
  import Button from "@khanacademy/wonder-blocks-button";
@@ -8,18 +8,18 @@ import Button from "@khanacademy/wonder-blocks-button";
8
8
  import TextField from "../text-field";
9
9
 
10
10
  describe("TextField", () => {
11
- it("textfield is focused", () => {
11
+ it("textfield is focused", async () => {
12
12
  // Arrange
13
13
  render(<TextField id="tf-1" value="" onChange={() => {}} />);
14
14
 
15
15
  // Act
16
- userEvent.tab();
16
+ await userEvent.tab();
17
17
 
18
18
  // Assert
19
- expect(screen.getByRole("textbox")).toHaveFocus();
19
+ expect(await screen.findByRole("textbox")).toHaveFocus();
20
20
  });
21
21
 
22
- it("onFocus is called after textfield is focused", () => {
22
+ it("onFocus is called after textfield is focused", async () => {
23
23
  // Arrange
24
24
  const handleOnFocus = jest.fn(() => {});
25
25
 
@@ -33,7 +33,7 @@ describe("TextField", () => {
33
33
  );
34
34
 
35
35
  // Act
36
- userEvent.tab();
36
+ await userEvent.tab();
37
37
 
38
38
  // Assert
39
39
  expect(handleOnFocus).toHaveBeenCalled();
@@ -44,14 +44,14 @@ describe("TextField", () => {
44
44
  render(<TextField id="tf-1" value="" onChange={() => {}} />);
45
45
 
46
46
  // focus
47
- userEvent.tab();
47
+ await userEvent.tab();
48
48
 
49
49
  // Act
50
50
  // blur
51
- userEvent.tab();
51
+ await userEvent.tab();
52
52
 
53
53
  // Assert
54
- expect(screen.getByRole("textbox")).not.toHaveFocus();
54
+ expect(await screen.findByRole("textbox")).not.toHaveFocus();
55
55
  });
56
56
 
57
57
  it("onBlur is called after textfield is blurred", async () => {
@@ -68,17 +68,17 @@ describe("TextField", () => {
68
68
  );
69
69
 
70
70
  // focus
71
- userEvent.tab();
71
+ await userEvent.tab();
72
72
 
73
73
  // Act
74
74
  // blur
75
- userEvent.tab();
75
+ await userEvent.tab();
76
76
 
77
77
  // Assert
78
78
  expect(handleOnBlur).toHaveBeenCalled();
79
79
  });
80
80
 
81
- it("id prop is passed to the input element", () => {
81
+ it("id prop is passed to the input element", async () => {
82
82
  // Arrange
83
83
  const id = "tf-1";
84
84
 
@@ -86,11 +86,11 @@ describe("TextField", () => {
86
86
  render(<TextField id={id} value="" onChange={() => {}} />);
87
87
 
88
88
  // Assert
89
- const input = screen.getByRole("textbox");
89
+ const input = await screen.findByRole("textbox");
90
90
  expect(input).toHaveAttribute("id", id);
91
91
  });
92
92
 
93
- it("type prop is passed to the input element", () => {
93
+ it("type prop is passed to the input element", async () => {
94
94
  // Arrange
95
95
  const type = "number";
96
96
 
@@ -101,11 +101,11 @@ describe("TextField", () => {
101
101
 
102
102
  // Assert
103
103
  // NOTE: The implicit role for input[type=number] is "spinbutton".
104
- const input = screen.getByRole("spinbutton");
104
+ const input = await screen.findByRole("spinbutton");
105
105
  expect(input).toHaveAttribute("type", type);
106
106
  });
107
107
 
108
- it("name prop is passed to the input element", () => {
108
+ it("name prop is passed to the input element", async () => {
109
109
  // Arrange
110
110
  const name = "some-name";
111
111
 
@@ -115,11 +115,11 @@ describe("TextField", () => {
115
115
  );
116
116
 
117
117
  // Assert
118
- const input = screen.getByRole("textbox");
118
+ const input = await screen.findByRole("textbox");
119
119
  expect(input).toHaveAttribute("name", name);
120
120
  });
121
121
 
122
- it("value prop is passed to the input element", () => {
122
+ it("value prop is passed to the input element", async () => {
123
123
  // Arrange
124
124
  const value = "Text";
125
125
 
@@ -127,11 +127,11 @@ describe("TextField", () => {
127
127
  render(<TextField id={"tf-1"} value={value} onChange={() => {}} />);
128
128
 
129
129
  // Assert
130
- const input = screen.getByDisplayValue(value);
130
+ const input = await screen.findByDisplayValue(value);
131
131
  expect(input).toBeInTheDocument();
132
132
  });
133
133
 
134
- it("disabled prop disables the input element", () => {
134
+ it("disabled prop disables the input element", async () => {
135
135
  // Arrange
136
136
  render(
137
137
  <TextField
@@ -141,7 +141,7 @@ describe("TextField", () => {
141
141
  disabled={true}
142
142
  />,
143
143
  );
144
- const input = screen.getByRole("textbox");
144
+ const input = await screen.findByRole("textbox");
145
145
 
146
146
  // Act
147
147
 
@@ -149,7 +149,7 @@ describe("TextField", () => {
149
149
  expect(input).toBeDisabled();
150
150
  });
151
151
 
152
- it("onChange is called when value changes", () => {
152
+ it("onChange is called when value changes", async () => {
153
153
  // Arrange
154
154
  const handleOnChange = jest.fn();
155
155
 
@@ -159,7 +159,7 @@ describe("TextField", () => {
159
159
 
160
160
  // Act
161
161
  const newValue = "Test2";
162
- const input = screen.getByRole("textbox");
162
+ const input = await screen.findByRole("textbox");
163
163
  // @see https://testing-library.com/docs/react-testing-library/faq
164
164
  // How do I test input onChange handlers?
165
165
  // eslint-disable-next-line testing-library/prefer-user-event
@@ -169,7 +169,7 @@ describe("TextField", () => {
169
169
  expect(handleOnChange).toHaveBeenCalledWith(newValue);
170
170
  });
171
171
 
172
- it("validate is called when value changes", () => {
172
+ it("validate is called when value changes", async () => {
173
173
  // Arrange
174
174
  const handleValidate = jest.fn((value: string) => {});
175
175
 
@@ -185,13 +185,16 @@ describe("TextField", () => {
185
185
  // Act
186
186
  const newValue = "Text2";
187
187
  // Select all text and replace it with the new value.
188
- userEvent.type(screen.getByRole("textbox"), `{selectall}${newValue}`);
188
+ await userEvent.type(
189
+ await screen.findByRole("textbox"),
190
+ `{selectall}${newValue}`,
191
+ );
189
192
 
190
193
  // Assert
191
194
  expect(handleValidate).toHaveBeenCalledWith(newValue);
192
195
  });
193
196
 
194
- it("validate is given a valid input", () => {
197
+ it("validate is given a valid input", async () => {
195
198
  // Arrange
196
199
  const handleValidate = jest.fn(
197
200
  (value: string): string | null | undefined => {
@@ -213,13 +216,16 @@ describe("TextField", () => {
213
216
  // Act
214
217
  const newValue = "TextIsLongerThan8";
215
218
  // Select all text and replace it with the new value.
216
- userEvent.type(screen.getByRole("textbox"), `{selectall}${newValue}`);
219
+ await userEvent.type(
220
+ await screen.findByRole("textbox"),
221
+ `{selectall}${newValue}`,
222
+ );
217
223
 
218
224
  // Assert
219
225
  expect(handleValidate).toHaveReturnedWith(undefined);
220
226
  });
221
227
 
222
- it("validate is given an invalid input", () => {
228
+ it("validate is given an invalid input", async () => {
223
229
  // Arrange
224
230
  const errorMessage = "Value is too short";
225
231
  const handleValidate = jest.fn(
@@ -242,13 +248,16 @@ describe("TextField", () => {
242
248
  // Act
243
249
  const newValue = "Text";
244
250
  // Select all text and replace it with the new value.
245
- userEvent.type(screen.getByRole("textbox"), `{selectall}${newValue}`);
251
+ const textbox = await screen.findByRole("textbox");
252
+ await userEvent.click(textbox);
253
+ await userEvent.clear(textbox);
254
+ await userEvent.paste(newValue);
246
255
 
247
256
  // Assert
248
257
  expect(handleValidate).toHaveReturnedWith(errorMessage);
249
258
  });
250
259
 
251
- it("onValidate is called after input validate", () => {
260
+ it("onValidate is called after input validate", async () => {
252
261
  // Arrange
253
262
  const errorMessage = "Value is too short";
254
263
  const handleValidate = jest.fn((errorMessage?: string | null) => {});
@@ -271,13 +280,16 @@ describe("TextField", () => {
271
280
  // Act
272
281
  const newValue = "Text";
273
282
  // Select all text and replace it with the new value.
274
- userEvent.type(screen.getByRole("textbox"), `{selectall}${newValue}`);
283
+ const textbox = await screen.findByRole("textbox");
284
+ await userEvent.click(textbox);
285
+ await userEvent.clear(textbox);
286
+ await userEvent.paste(newValue);
275
287
 
276
288
  // Assert
277
289
  expect(handleValidate).toHaveBeenCalledWith(errorMessage);
278
290
  });
279
291
 
280
- it("onValidate is called on input's initial value", () => {
292
+ it("onValidate is called on input's initial value", async () => {
281
293
  // Arrange
282
294
  const errorMessage = "Value is too short";
283
295
  const handleValidate = jest.fn((errorMessage?: string | null) => {});
@@ -302,7 +314,7 @@ describe("TextField", () => {
302
314
  expect(handleValidate).toHaveBeenCalledWith(errorMessage);
303
315
  });
304
316
 
305
- it("onKeyDown is called after keyboard key press", () => {
317
+ it("onKeyDown is called after keyboard key press", async () => {
306
318
  // Arrange
307
319
  const handleOnKeyDown = jest.fn(
308
320
  (event: React.KeyboardEvent<HTMLInputElement>) => {
@@ -320,13 +332,13 @@ describe("TextField", () => {
320
332
  );
321
333
 
322
334
  // Act
323
- userEvent.type(screen.getByRole("textbox"), "{enter}");
335
+ await userEvent.type(await screen.findByRole("textbox"), "{enter}");
324
336
 
325
337
  // Assert
326
338
  expect(handleOnKeyDown).toHaveReturnedWith("Enter");
327
339
  });
328
340
 
329
- it("placeholder prop is passed to the input element", () => {
341
+ it("placeholder prop is passed to the input element", async () => {
330
342
  // Arrange
331
343
  const placeholder = "Placeholder";
332
344
 
@@ -341,11 +353,11 @@ describe("TextField", () => {
341
353
  );
342
354
 
343
355
  // Assert
344
- const input = screen.getByPlaceholderText(placeholder);
356
+ const input = await screen.findByPlaceholderText(placeholder);
345
357
  expect(input).toBeInTheDocument();
346
358
  });
347
359
 
348
- it("testId is passed to the input element", () => {
360
+ it("testId is passed to the input element", async () => {
349
361
  // Arrange
350
362
  const testId = "some-test-id";
351
363
  render(
@@ -360,11 +372,11 @@ describe("TextField", () => {
360
372
  // Act
361
373
 
362
374
  // Assert
363
- const input = screen.getByRole("textbox");
375
+ const input = await screen.findByRole("textbox");
364
376
  expect(input).toHaveAttribute("data-test-id", testId);
365
377
  });
366
378
 
367
- it("aria props are passed to the input element", () => {
379
+ it("aria props are passed to the input element", async () => {
368
380
  // Arrange
369
381
  const ariaLabel = "example-text-field";
370
382
  render(
@@ -379,7 +391,7 @@ describe("TextField", () => {
379
391
  // Act
380
392
 
381
393
  // Assert
382
- const input = screen.getByRole("textbox");
394
+ const input = await screen.findByRole("textbox");
383
395
  expect(input).toHaveAttribute("aria-label", ariaLabel);
384
396
  });
385
397
 
@@ -397,7 +409,7 @@ describe("TextField", () => {
397
409
  );
398
410
 
399
411
  // Assert
400
- const input = screen.getByRole("textbox");
412
+ const input = await screen.findByRole("textbox");
401
413
  expect(input).toHaveAttribute("readOnly");
402
414
  });
403
415
 
@@ -416,11 +428,11 @@ describe("TextField", () => {
416
428
  );
417
429
 
418
430
  // Assert
419
- const input = screen.getByRole("textbox");
431
+ const input = await screen.findByRole("textbox");
420
432
  expect(input).toHaveAttribute("autoComplete", autoComplete);
421
433
  });
422
434
 
423
- test("has focus if autoFocus is true", () => {
435
+ test("has focus if autoFocus is true", async () => {
424
436
  // Arrange
425
437
  render(
426
438
  <View>
@@ -439,13 +451,13 @@ describe("TextField", () => {
439
451
  );
440
452
 
441
453
  // Act
442
- const searchField = screen.getByTestId("search-field-test");
454
+ const searchField = await screen.findByTestId("search-field-test");
443
455
 
444
456
  // Assert
445
457
  expect(searchField).toHaveFocus();
446
458
  });
447
459
 
448
- test("does not have focus if autoFocus is undefined", () => {
460
+ test("does not have focus if autoFocus is undefined", async () => {
449
461
  // Arrange
450
462
  render(
451
463
  <View>
@@ -463,7 +475,7 @@ describe("TextField", () => {
463
475
  );
464
476
 
465
477
  // Act
466
- const searchField = screen.getByTestId("search-field-test");
478
+ const searchField = await screen.findByTestId("search-field-test");
467
479
 
468
480
  // Assert
469
481
  expect(searchField).not.toHaveFocus();