@khanacademy/wonder-blocks-clickable 4.1.2 → 4.2.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 +20 -0
- package/dist/es/index.js +2 -2
- package/dist/index.js +2 -2
- package/package.json +3 -3
- package/src/components/__tests__/clickable-behavior.test.tsx +111 -105
- package/src/components/__tests__/clickable.test.tsx +56 -52
- package/src/components/clickable.tsx +2 -2
- package/tsconfig-build.tsbuildinfo +1 -1
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
import * as React from "react";
|
|
4
4
|
import {render, screen, fireEvent, waitFor} from "@testing-library/react";
|
|
5
5
|
import {MemoryRouter, Switch, Route} from "react-router-dom";
|
|
6
|
-
import userEvent from "@testing-library/user-event";
|
|
6
|
+
import {userEvent} from "@testing-library/user-event";
|
|
7
7
|
|
|
8
8
|
import getClickableBehavior from "../../util/get-clickable-behavior";
|
|
9
9
|
import ClickableBehavior from "../clickable-behavior";
|
|
@@ -47,7 +47,7 @@ describe("ClickableBehavior", () => {
|
|
|
47
47
|
window.open.mockClear();
|
|
48
48
|
});
|
|
49
49
|
|
|
50
|
-
it("renders a label", () => {
|
|
50
|
+
it("renders a label", async () => {
|
|
51
51
|
const onClick = jest.fn();
|
|
52
52
|
render(
|
|
53
53
|
<ClickableBehavior
|
|
@@ -60,11 +60,11 @@ describe("ClickableBehavior", () => {
|
|
|
60
60
|
</ClickableBehavior>,
|
|
61
61
|
);
|
|
62
62
|
expect(onClick).not.toHaveBeenCalled();
|
|
63
|
-
userEvent.click(screen.
|
|
63
|
+
await userEvent.click(await screen.findByRole("button"));
|
|
64
64
|
expect(onClick).toHaveBeenCalled();
|
|
65
65
|
});
|
|
66
66
|
|
|
67
|
-
it("changes hovered state on mouse enter/leave", () => {
|
|
67
|
+
it("changes hovered state on mouse enter/leave", async () => {
|
|
68
68
|
const onClick = jest.fn();
|
|
69
69
|
render(
|
|
70
70
|
<ClickableBehavior
|
|
@@ -77,15 +77,15 @@ describe("ClickableBehavior", () => {
|
|
|
77
77
|
}}
|
|
78
78
|
</ClickableBehavior>,
|
|
79
79
|
);
|
|
80
|
-
const button = screen.
|
|
80
|
+
const button = await screen.findByRole("button");
|
|
81
81
|
expect(button).not.toHaveTextContent("hovered");
|
|
82
|
-
userEvent.hover(button);
|
|
82
|
+
await userEvent.hover(button);
|
|
83
83
|
expect(button).toHaveTextContent("hovered");
|
|
84
|
-
userEvent.unhover(button);
|
|
84
|
+
await userEvent.unhover(button);
|
|
85
85
|
expect(button).not.toHaveTextContent("hovered");
|
|
86
86
|
});
|
|
87
87
|
|
|
88
|
-
it("changes hovered state on mouse enter while dragging", () => {
|
|
88
|
+
it("changes hovered state on mouse enter while dragging", async () => {
|
|
89
89
|
const onClick = jest.fn();
|
|
90
90
|
render(
|
|
91
91
|
<ClickableBehavior
|
|
@@ -98,7 +98,7 @@ describe("ClickableBehavior", () => {
|
|
|
98
98
|
}}
|
|
99
99
|
</ClickableBehavior>,
|
|
100
100
|
);
|
|
101
|
-
const button = screen.
|
|
101
|
+
const button = await screen.findByRole("button");
|
|
102
102
|
expect(button).not.toHaveTextContent("hovered");
|
|
103
103
|
expect(button).not.toHaveTextContent("pressed");
|
|
104
104
|
|
|
@@ -107,7 +107,7 @@ describe("ClickableBehavior", () => {
|
|
|
107
107
|
expect(button).toHaveTextContent("hovered");
|
|
108
108
|
});
|
|
109
109
|
|
|
110
|
-
it("changes pressed and hover states on mouse leave while dragging", () => {
|
|
110
|
+
it("changes pressed and hover states on mouse leave while dragging", async () => {
|
|
111
111
|
const onClick = jest.fn();
|
|
112
112
|
render(
|
|
113
113
|
<ClickableBehavior
|
|
@@ -120,7 +120,7 @@ describe("ClickableBehavior", () => {
|
|
|
120
120
|
}}
|
|
121
121
|
</ClickableBehavior>,
|
|
122
122
|
);
|
|
123
|
-
const button = screen.
|
|
123
|
+
const button = await screen.findByRole("button");
|
|
124
124
|
expect(button).not.toHaveTextContent("hovered");
|
|
125
125
|
expect(button).not.toHaveTextContent("pressed");
|
|
126
126
|
|
|
@@ -132,7 +132,7 @@ describe("ClickableBehavior", () => {
|
|
|
132
132
|
expect(button).not.toHaveTextContent("pressed");
|
|
133
133
|
});
|
|
134
134
|
|
|
135
|
-
it("changes pressed state on mouse down/up", () => {
|
|
135
|
+
it("changes pressed state on mouse down/up", async () => {
|
|
136
136
|
const onClick = jest.fn();
|
|
137
137
|
render(
|
|
138
138
|
<ClickableBehavior
|
|
@@ -145,7 +145,7 @@ describe("ClickableBehavior", () => {
|
|
|
145
145
|
}}
|
|
146
146
|
</ClickableBehavior>,
|
|
147
147
|
);
|
|
148
|
-
const button = screen.
|
|
148
|
+
const button = await screen.findByRole("button");
|
|
149
149
|
expect(button).not.toHaveTextContent("pressed");
|
|
150
150
|
fireEvent.mouseDown(button);
|
|
151
151
|
expect(button).toHaveTextContent("pressed");
|
|
@@ -153,7 +153,7 @@ describe("ClickableBehavior", () => {
|
|
|
153
153
|
expect(button).not.toHaveTextContent("pressed");
|
|
154
154
|
});
|
|
155
155
|
|
|
156
|
-
it("changes pressed state on touch start/end/cancel", () => {
|
|
156
|
+
it("changes pressed state on touch start/end/cancel", async () => {
|
|
157
157
|
const onClick = jest.fn();
|
|
158
158
|
render(
|
|
159
159
|
<ClickableBehavior
|
|
@@ -166,7 +166,7 @@ describe("ClickableBehavior", () => {
|
|
|
166
166
|
}}
|
|
167
167
|
</ClickableBehavior>,
|
|
168
168
|
);
|
|
169
|
-
const button = screen.
|
|
169
|
+
const button = await screen.findByRole("button");
|
|
170
170
|
expect(button).not.toHaveTextContent("pressed");
|
|
171
171
|
fireEvent.touchStart(button);
|
|
172
172
|
expect(button).toHaveTextContent("pressed");
|
|
@@ -180,7 +180,7 @@ describe("ClickableBehavior", () => {
|
|
|
180
180
|
expect(button).not.toHaveTextContent("pressed");
|
|
181
181
|
});
|
|
182
182
|
|
|
183
|
-
it("enters focused state on key press after click", () => {
|
|
183
|
+
it("enters focused state on key press after click", async () => {
|
|
184
184
|
const onClick = jest.fn();
|
|
185
185
|
render(
|
|
186
186
|
<ClickableBehavior
|
|
@@ -193,17 +193,17 @@ describe("ClickableBehavior", () => {
|
|
|
193
193
|
}}
|
|
194
194
|
</ClickableBehavior>,
|
|
195
195
|
);
|
|
196
|
-
const button = screen.
|
|
196
|
+
const button = await screen.findByRole("button");
|
|
197
197
|
expect(button).not.toHaveTextContent("focused");
|
|
198
198
|
fireEvent.keyDown(button, {keyCode: keyCodes.space});
|
|
199
199
|
fireEvent.keyUp(button, {keyCode: keyCodes.space});
|
|
200
|
-
// NOTE(kevinb): userEvent.click() fires other events that we don't want
|
|
200
|
+
// NOTE(kevinb): await userEvent.click() fires other events that we don't want
|
|
201
201
|
// affecting this test case.
|
|
202
202
|
fireEvent.click(button);
|
|
203
203
|
expect(button).toHaveTextContent("focused");
|
|
204
204
|
});
|
|
205
205
|
|
|
206
|
-
it("exits focused state on click after key press", () => {
|
|
206
|
+
it("exits focused state on click after key press", async () => {
|
|
207
207
|
const onClick = jest.fn();
|
|
208
208
|
|
|
209
209
|
render(
|
|
@@ -217,19 +217,19 @@ describe("ClickableBehavior", () => {
|
|
|
217
217
|
}}
|
|
218
218
|
</ClickableBehavior>,
|
|
219
219
|
);
|
|
220
|
-
const button = screen.
|
|
220
|
+
const button = await screen.findByRole("button");
|
|
221
221
|
expect(button).not.toHaveTextContent("focused");
|
|
222
222
|
fireEvent.keyDown(button, {keyCode: keyCodes.space});
|
|
223
223
|
fireEvent.keyUp(button, {keyCode: keyCodes.space});
|
|
224
|
-
// NOTE(kevinb): userEvent.click() fires other events that we don't want
|
|
224
|
+
// NOTE(kevinb): await userEvent.click() fires other events that we don't want
|
|
225
225
|
// affecting this test case.
|
|
226
226
|
fireEvent.click(button);
|
|
227
227
|
expect(button).toHaveTextContent("focused");
|
|
228
|
-
userEvent.click(button);
|
|
228
|
+
await userEvent.click(button);
|
|
229
229
|
expect(button).not.toHaveTextContent("focused");
|
|
230
230
|
});
|
|
231
231
|
|
|
232
|
-
it("changes pressed state on space/enter key down/up if <button>", () => {
|
|
232
|
+
it("changes pressed state on space/enter key down/up if <button>", async () => {
|
|
233
233
|
const onClick = jest.fn();
|
|
234
234
|
render(
|
|
235
235
|
<ClickableBehavior
|
|
@@ -242,7 +242,7 @@ describe("ClickableBehavior", () => {
|
|
|
242
242
|
}}
|
|
243
243
|
</ClickableBehavior>,
|
|
244
244
|
);
|
|
245
|
-
const button = screen.
|
|
245
|
+
const button = await screen.findByRole("button");
|
|
246
246
|
expect(button).not.toHaveTextContent("pressed");
|
|
247
247
|
fireEvent.keyDown(button, {keyCode: keyCodes.space});
|
|
248
248
|
expect(button).toHaveTextContent("pressed");
|
|
@@ -255,7 +255,7 @@ describe("ClickableBehavior", () => {
|
|
|
255
255
|
expect(button).not.toHaveTextContent("pressed");
|
|
256
256
|
});
|
|
257
257
|
|
|
258
|
-
it("changes pressed state on only enter key down/up for a link", () => {
|
|
258
|
+
it("changes pressed state on only enter key down/up for a link", async () => {
|
|
259
259
|
const onClick = jest.fn();
|
|
260
260
|
// Use mount instead of a shallow render to trigger event defaults
|
|
261
261
|
render(
|
|
@@ -278,7 +278,7 @@ describe("ClickableBehavior", () => {
|
|
|
278
278
|
}}
|
|
279
279
|
</ClickableBehavior>,
|
|
280
280
|
);
|
|
281
|
-
const link = screen.
|
|
281
|
+
const link = await screen.findByRole("link");
|
|
282
282
|
expect(link).not.toHaveTextContent("pressed");
|
|
283
283
|
fireEvent.keyDown(link, {keyCode: keyCodes.enter});
|
|
284
284
|
expect(link).toHaveTextContent("pressed");
|
|
@@ -291,7 +291,7 @@ describe("ClickableBehavior", () => {
|
|
|
291
291
|
expect(link).not.toHaveTextContent("pressed");
|
|
292
292
|
});
|
|
293
293
|
|
|
294
|
-
it("gains focused state on focus event", () => {
|
|
294
|
+
it("gains focused state on focus event", async () => {
|
|
295
295
|
const onClick = jest.fn();
|
|
296
296
|
render(
|
|
297
297
|
<ClickableBehavior
|
|
@@ -304,12 +304,12 @@ describe("ClickableBehavior", () => {
|
|
|
304
304
|
}}
|
|
305
305
|
</ClickableBehavior>,
|
|
306
306
|
);
|
|
307
|
-
const button = screen.
|
|
307
|
+
const button = await screen.findByRole("button");
|
|
308
308
|
fireEvent.focus(button);
|
|
309
309
|
expect(button).toHaveTextContent("focused");
|
|
310
310
|
});
|
|
311
311
|
|
|
312
|
-
it("changes focused state on blur", () => {
|
|
312
|
+
it("changes focused state on blur", async () => {
|
|
313
313
|
const onClick = jest.fn();
|
|
314
314
|
render(
|
|
315
315
|
<ClickableBehavior
|
|
@@ -322,20 +322,20 @@ describe("ClickableBehavior", () => {
|
|
|
322
322
|
}}
|
|
323
323
|
</ClickableBehavior>,
|
|
324
324
|
);
|
|
325
|
-
const button = screen.
|
|
325
|
+
const button = await screen.findByRole("button");
|
|
326
326
|
fireEvent.focus(button);
|
|
327
327
|
fireEvent.blur(button);
|
|
328
328
|
expect(button).not.toHaveTextContent("focused");
|
|
329
329
|
});
|
|
330
330
|
|
|
331
|
-
test("should not have a tabIndex if one is not passed in", () => {
|
|
331
|
+
test("should not have a tabIndex if one is not passed in", async () => {
|
|
332
332
|
// Arrange
|
|
333
333
|
// Act
|
|
334
334
|
render(
|
|
335
335
|
<ClickableBehavior disabled={false} onClick={(e: any) => {}}>
|
|
336
336
|
{(state: any, childrenProps: any) => {
|
|
337
337
|
return (
|
|
338
|
-
<button data-
|
|
338
|
+
<button data-testid="test-button-1" {...childrenProps}>
|
|
339
339
|
Label
|
|
340
340
|
</button>
|
|
341
341
|
);
|
|
@@ -344,11 +344,11 @@ describe("ClickableBehavior", () => {
|
|
|
344
344
|
);
|
|
345
345
|
|
|
346
346
|
// Assert
|
|
347
|
-
const button = screen.
|
|
347
|
+
const button = await screen.findByTestId("test-button-1");
|
|
348
348
|
expect(button).not.toHaveAttribute("tabIndex");
|
|
349
349
|
});
|
|
350
350
|
|
|
351
|
-
test("should have the tabIndex that is passed in", () => {
|
|
351
|
+
test("should have the tabIndex that is passed in", async () => {
|
|
352
352
|
// Arrange
|
|
353
353
|
// Act
|
|
354
354
|
render(
|
|
@@ -359,7 +359,7 @@ describe("ClickableBehavior", () => {
|
|
|
359
359
|
>
|
|
360
360
|
{(state: any, childrenProps: any) => {
|
|
361
361
|
return (
|
|
362
|
-
<button data-
|
|
362
|
+
<button data-testid="test-button-2" {...childrenProps}>
|
|
363
363
|
Label
|
|
364
364
|
</button>
|
|
365
365
|
);
|
|
@@ -368,11 +368,11 @@ describe("ClickableBehavior", () => {
|
|
|
368
368
|
);
|
|
369
369
|
|
|
370
370
|
// Assert
|
|
371
|
-
const button = screen.
|
|
371
|
+
const button = await screen.findByTestId("test-button-2");
|
|
372
372
|
expect(button).toHaveAttribute("tabIndex", "1");
|
|
373
373
|
});
|
|
374
374
|
|
|
375
|
-
test("should have the tabIndex that is passed in even if disabled", () => {
|
|
375
|
+
test("should have the tabIndex that is passed in even if disabled", async () => {
|
|
376
376
|
// Arrange
|
|
377
377
|
// Act
|
|
378
378
|
render(
|
|
@@ -383,7 +383,7 @@ describe("ClickableBehavior", () => {
|
|
|
383
383
|
>
|
|
384
384
|
{(state: any, childrenProps: any) => {
|
|
385
385
|
return (
|
|
386
|
-
<button data-
|
|
386
|
+
<button data-testid="test-button-3" {...childrenProps}>
|
|
387
387
|
Label
|
|
388
388
|
</button>
|
|
389
389
|
);
|
|
@@ -392,11 +392,11 @@ describe("ClickableBehavior", () => {
|
|
|
392
392
|
);
|
|
393
393
|
|
|
394
394
|
// Assert
|
|
395
|
-
const button = screen.
|
|
395
|
+
const button = await screen.findByTestId("test-button-3");
|
|
396
396
|
expect(button).toHaveAttribute("tabIndex", "1");
|
|
397
397
|
});
|
|
398
398
|
|
|
399
|
-
test("should make non-interactive children keyboard focusable if tabIndex 0 is passed", () => {
|
|
399
|
+
test("should make non-interactive children keyboard focusable if tabIndex 0 is passed", async () => {
|
|
400
400
|
// Arrange
|
|
401
401
|
render(
|
|
402
402
|
<ClickableBehavior
|
|
@@ -406,7 +406,7 @@ describe("ClickableBehavior", () => {
|
|
|
406
406
|
>
|
|
407
407
|
{(state: any, childrenProps: any) => {
|
|
408
408
|
return (
|
|
409
|
-
<div data-
|
|
409
|
+
<div data-testid="test-div-1" {...childrenProps}>
|
|
410
410
|
Label
|
|
411
411
|
</div>
|
|
412
412
|
);
|
|
@@ -415,14 +415,14 @@ describe("ClickableBehavior", () => {
|
|
|
415
415
|
);
|
|
416
416
|
|
|
417
417
|
// Act
|
|
418
|
-
const button = screen.
|
|
419
|
-
userEvent.tab();
|
|
418
|
+
const button = await screen.findByTestId("test-div-1");
|
|
419
|
+
await userEvent.tab();
|
|
420
420
|
|
|
421
421
|
// Assert
|
|
422
422
|
expect(button).toHaveFocus();
|
|
423
423
|
});
|
|
424
424
|
|
|
425
|
-
it("does not change state if disabled", () => {
|
|
425
|
+
it("does not change state if disabled", async () => {
|
|
426
426
|
const onClick = jest.fn();
|
|
427
427
|
render(
|
|
428
428
|
<ClickableBehavior disabled={true} onClick={(e: any) => onClick(e)}>
|
|
@@ -433,7 +433,7 @@ describe("ClickableBehavior", () => {
|
|
|
433
433
|
</ClickableBehavior>,
|
|
434
434
|
);
|
|
435
435
|
|
|
436
|
-
const button = screen.
|
|
436
|
+
const button = await screen.findByRole("button");
|
|
437
437
|
expect(onClick).not.toHaveBeenCalled();
|
|
438
438
|
fireEvent.click(button);
|
|
439
439
|
expect(onClick).not.toHaveBeenCalled();
|
|
@@ -499,7 +499,7 @@ describe("ClickableBehavior", () => {
|
|
|
499
499
|
</ClickableBehavior>,
|
|
500
500
|
);
|
|
501
501
|
|
|
502
|
-
const anchor = screen.
|
|
502
|
+
const anchor = await screen.findByRole("link");
|
|
503
503
|
expect(anchor).not.toHaveTextContent("pressed");
|
|
504
504
|
fireEvent.keyDown(anchor, {keyCode: keyCodes.enter});
|
|
505
505
|
expect(anchor).not.toHaveTextContent("pressed");
|
|
@@ -507,7 +507,7 @@ describe("ClickableBehavior", () => {
|
|
|
507
507
|
expect(anchor).not.toHaveTextContent("pressed");
|
|
508
508
|
});
|
|
509
509
|
|
|
510
|
-
it("has onClick triggered just once per click by various means", () => {
|
|
510
|
+
it("has onClick triggered just once per click by various means", async () => {
|
|
511
511
|
const onClick = jest.fn();
|
|
512
512
|
render(
|
|
513
513
|
<ClickableBehavior
|
|
@@ -519,10 +519,10 @@ describe("ClickableBehavior", () => {
|
|
|
519
519
|
}}
|
|
520
520
|
</ClickableBehavior>,
|
|
521
521
|
);
|
|
522
|
-
const button = screen.
|
|
522
|
+
const button = await screen.findByRole("button");
|
|
523
523
|
expect(onClick).not.toHaveBeenCalled();
|
|
524
524
|
|
|
525
|
-
userEvent.click(button);
|
|
525
|
+
await userEvent.click(button);
|
|
526
526
|
expect(onClick).toHaveBeenCalledTimes(1);
|
|
527
527
|
|
|
528
528
|
fireEvent.keyDown(button, {keyCode: keyCodes.space});
|
|
@@ -539,7 +539,7 @@ describe("ClickableBehavior", () => {
|
|
|
539
539
|
expect(onClick).toHaveBeenCalledTimes(4);
|
|
540
540
|
});
|
|
541
541
|
|
|
542
|
-
it("resets state when set to disabled", () => {
|
|
542
|
+
it("resets state when set to disabled", async () => {
|
|
543
543
|
const onClick = jest.fn();
|
|
544
544
|
const {rerender} = render(
|
|
545
545
|
<ClickableBehavior
|
|
@@ -552,9 +552,9 @@ describe("ClickableBehavior", () => {
|
|
|
552
552
|
}}
|
|
553
553
|
</ClickableBehavior>,
|
|
554
554
|
);
|
|
555
|
-
const button = screen.
|
|
556
|
-
userEvent.tab(); // focus
|
|
557
|
-
userEvent.hover(button);
|
|
555
|
+
const button = await screen.findByRole("button");
|
|
556
|
+
await userEvent.tab(); // focus
|
|
557
|
+
await userEvent.hover(button);
|
|
558
558
|
|
|
559
559
|
rerender(
|
|
560
560
|
<ClickableBehavior disabled={true} onClick={(e: any) => onClick(e)}>
|
|
@@ -573,7 +573,7 @@ describe("ClickableBehavior", () => {
|
|
|
573
573
|
});
|
|
574
574
|
|
|
575
575
|
describe("full page load navigation", () => {
|
|
576
|
-
it("both navigates and calls onClick for an anchor link", () => {
|
|
576
|
+
it("both navigates and calls onClick for an anchor link", async () => {
|
|
577
577
|
const onClick = jest.fn();
|
|
578
578
|
// Use mount instead of a shallow render to trigger event defaults
|
|
579
579
|
render(
|
|
@@ -597,7 +597,7 @@ describe("ClickableBehavior", () => {
|
|
|
597
597
|
}}
|
|
598
598
|
</ClickableBehavior>,
|
|
599
599
|
);
|
|
600
|
-
const link = screen.
|
|
600
|
+
const link = await screen.findByRole("link");
|
|
601
601
|
|
|
602
602
|
// Space press should not trigger the onClick
|
|
603
603
|
fireEvent.keyDown(link, {keyCode: keyCodes.space});
|
|
@@ -646,8 +646,8 @@ describe("ClickableBehavior", () => {
|
|
|
646
646
|
);
|
|
647
647
|
|
|
648
648
|
// Act
|
|
649
|
-
const link = screen.
|
|
650
|
-
userEvent.click(link);
|
|
649
|
+
const link = await screen.findByRole("link");
|
|
650
|
+
await userEvent.click(link);
|
|
651
651
|
|
|
652
652
|
// Assert
|
|
653
653
|
await waitFor(() => {
|
|
@@ -680,14 +680,14 @@ describe("ClickableBehavior", () => {
|
|
|
680
680
|
);
|
|
681
681
|
|
|
682
682
|
// Act
|
|
683
|
-
const link = screen.
|
|
684
|
-
userEvent.click(link);
|
|
683
|
+
const link = await screen.findByRole("link");
|
|
684
|
+
await userEvent.click(link);
|
|
685
685
|
|
|
686
686
|
// Assert
|
|
687
687
|
expect(link).toHaveTextContent("waiting");
|
|
688
688
|
});
|
|
689
689
|
|
|
690
|
-
it("If onClick calls e.preventDefault() then we won't navigate", () => {
|
|
690
|
+
it("If onClick calls e.preventDefault() then we won't navigate", async () => {
|
|
691
691
|
// Arrange
|
|
692
692
|
render(
|
|
693
693
|
<ClickableBehavior
|
|
@@ -705,15 +705,15 @@ describe("ClickableBehavior", () => {
|
|
|
705
705
|
);
|
|
706
706
|
|
|
707
707
|
// Act
|
|
708
|
-
const button = screen.
|
|
709
|
-
userEvent.click(button);
|
|
708
|
+
const button = await screen.findByRole("button");
|
|
709
|
+
await userEvent.click(button);
|
|
710
710
|
|
|
711
711
|
// Assert
|
|
712
712
|
expect(window.location.assign).not.toHaveBeenCalled();
|
|
713
713
|
});
|
|
714
714
|
});
|
|
715
715
|
|
|
716
|
-
it("calls onClick correctly for a component that doesn't respond to enter", () => {
|
|
716
|
+
it("calls onClick correctly for a component that doesn't respond to enter", async () => {
|
|
717
717
|
const onClick = jest.fn();
|
|
718
718
|
// Use mount instead of a shallow render to trigger event defaults
|
|
719
719
|
render(
|
|
@@ -729,7 +729,7 @@ describe("ClickableBehavior", () => {
|
|
|
729
729
|
);
|
|
730
730
|
|
|
731
731
|
// Enter press should not do anything
|
|
732
|
-
const checkbox = screen.
|
|
732
|
+
const checkbox = await screen.findByRole("checkbox");
|
|
733
733
|
fireEvent.keyDown(checkbox, {keyCode: keyCodes.enter});
|
|
734
734
|
expect(onClick).toHaveBeenCalledTimes(0);
|
|
735
735
|
fireEvent.keyUp(checkbox, {keyCode: keyCodes.enter});
|
|
@@ -741,7 +741,7 @@ describe("ClickableBehavior", () => {
|
|
|
741
741
|
expect(onClick).toHaveBeenCalledTimes(1);
|
|
742
742
|
});
|
|
743
743
|
|
|
744
|
-
it("calls onClick for a button component on both enter/space", () => {
|
|
744
|
+
it("calls onClick for a button component on both enter/space", async () => {
|
|
745
745
|
const onClick = jest.fn();
|
|
746
746
|
// Use mount instead of a shallow render to trigger event defaults
|
|
747
747
|
render(
|
|
@@ -756,7 +756,7 @@ describe("ClickableBehavior", () => {
|
|
|
756
756
|
);
|
|
757
757
|
|
|
758
758
|
// Enter press
|
|
759
|
-
const button = screen.
|
|
759
|
+
const button = await screen.findByRole("button");
|
|
760
760
|
fireEvent.keyDown(button, {keyCode: keyCodes.enter});
|
|
761
761
|
expect(onClick).toHaveBeenCalledTimes(0);
|
|
762
762
|
fireEvent.keyUp(button, {keyCode: keyCodes.enter});
|
|
@@ -772,7 +772,7 @@ describe("ClickableBehavior", () => {
|
|
|
772
772
|
// This tests the case where we attach the childrenProps to an element that is
|
|
773
773
|
// not canonically clickable (like a div). The browser doesn't naturally
|
|
774
774
|
// trigger keyboard click events for such an element.
|
|
775
|
-
it("calls onClick listener on space/enter with a non-usually clickable element", () => {
|
|
775
|
+
it("calls onClick listener on space/enter with a non-usually clickable element", async () => {
|
|
776
776
|
const onClick = jest.fn();
|
|
777
777
|
// Use mount instead of a shallow render to trigger event defaults
|
|
778
778
|
const {container} = render(
|
|
@@ -803,7 +803,7 @@ describe("ClickableBehavior", () => {
|
|
|
803
803
|
expect(onClick).toHaveBeenCalledTimes(expectedNumberTimesCalled);
|
|
804
804
|
|
|
805
805
|
// Simulate a mouse click.
|
|
806
|
-
userEvent.click(clickableDiv);
|
|
806
|
+
await userEvent.click(clickableDiv);
|
|
807
807
|
expectedNumberTimesCalled += 1;
|
|
808
808
|
expect(onClick).toHaveBeenCalledTimes(expectedNumberTimesCalled);
|
|
809
809
|
|
|
@@ -815,7 +815,7 @@ describe("ClickableBehavior", () => {
|
|
|
815
815
|
expect(onClick).toHaveBeenCalledTimes(expectedNumberTimesCalled);
|
|
816
816
|
|
|
817
817
|
// Simulate another mouse click.
|
|
818
|
-
userEvent.click(clickableDiv);
|
|
818
|
+
await userEvent.click(clickableDiv);
|
|
819
819
|
expectedNumberTimesCalled += 1;
|
|
820
820
|
expect(onClick).toHaveBeenCalledTimes(expectedNumberTimesCalled);
|
|
821
821
|
});
|
|
@@ -830,7 +830,7 @@ describe("ClickableBehavior", () => {
|
|
|
830
830
|
// 2. Mouse down in the button, drag out of the button (don't let go),
|
|
831
831
|
// drag back into the button, and mouseup inside the button.
|
|
832
832
|
// This should result in a successful click.
|
|
833
|
-
it("does not call onClick on mouseup when the mouse presses inside and drags away", () => {
|
|
833
|
+
it("does not call onClick on mouseup when the mouse presses inside and drags away", async () => {
|
|
834
834
|
const onClick = jest.fn();
|
|
835
835
|
render(
|
|
836
836
|
<ClickableBehavior
|
|
@@ -843,14 +843,14 @@ describe("ClickableBehavior", () => {
|
|
|
843
843
|
</ClickableBehavior>,
|
|
844
844
|
);
|
|
845
845
|
|
|
846
|
-
const button = screen.
|
|
846
|
+
const button = await screen.findByRole("button");
|
|
847
847
|
fireEvent.mouseDown(button);
|
|
848
848
|
fireEvent.mouseLeave(button);
|
|
849
849
|
fireEvent.mouseUp(button);
|
|
850
850
|
expect(onClick).toHaveBeenCalledTimes(0);
|
|
851
851
|
});
|
|
852
852
|
|
|
853
|
-
it("does not call onClick on mouseup when the mouse presses outside and drags in", () => {
|
|
853
|
+
it("does not call onClick on mouseup when the mouse presses outside and drags in", async () => {
|
|
854
854
|
const onClick = jest.fn();
|
|
855
855
|
render(
|
|
856
856
|
<ClickableBehavior
|
|
@@ -863,13 +863,13 @@ describe("ClickableBehavior", () => {
|
|
|
863
863
|
</ClickableBehavior>,
|
|
864
864
|
);
|
|
865
865
|
|
|
866
|
-
const button = screen.
|
|
866
|
+
const button = await screen.findByRole("button");
|
|
867
867
|
fireEvent.mouseEnter(button, {buttons: 1});
|
|
868
868
|
fireEvent.mouseUp(button);
|
|
869
869
|
expect(onClick).toHaveBeenCalledTimes(0);
|
|
870
870
|
});
|
|
871
871
|
|
|
872
|
-
it("doesn't trigger enter key when browser doesn't stop the click", () => {
|
|
872
|
+
it("doesn't trigger enter key when browser doesn't stop the click", async () => {
|
|
873
873
|
const onClick = jest.fn();
|
|
874
874
|
// Use mount instead of a shallow render to trigger event defaults
|
|
875
875
|
render(
|
|
@@ -883,7 +883,7 @@ describe("ClickableBehavior", () => {
|
|
|
883
883
|
</ClickableBehavior>,
|
|
884
884
|
);
|
|
885
885
|
|
|
886
|
-
const checkbox = screen.
|
|
886
|
+
const checkbox = await screen.findByRole("checkbox");
|
|
887
887
|
// Enter press should not do anything
|
|
888
888
|
fireEvent.keyDown(checkbox, {keyCode: keyCodes.enter});
|
|
889
889
|
// This element still wants to have a click on enter press
|
|
@@ -899,7 +899,7 @@ describe("ClickableBehavior", () => {
|
|
|
899
899
|
true, // router
|
|
900
900
|
);
|
|
901
901
|
|
|
902
|
-
it("handles client-side navigation when there's a router context", () => {
|
|
902
|
+
it("handles client-side navigation when there's a router context", async () => {
|
|
903
903
|
// Arrange
|
|
904
904
|
render(
|
|
905
905
|
<MemoryRouter>
|
|
@@ -928,10 +928,12 @@ describe("ClickableBehavior", () => {
|
|
|
928
928
|
);
|
|
929
929
|
|
|
930
930
|
// Act
|
|
931
|
-
userEvent.click(screen.
|
|
931
|
+
await userEvent.click(await screen.findByRole("button"));
|
|
932
932
|
|
|
933
933
|
// Assert
|
|
934
|
-
expect(
|
|
934
|
+
expect(
|
|
935
|
+
await screen.findByText("Hello, world!"),
|
|
936
|
+
).toBeInTheDocument();
|
|
935
937
|
});
|
|
936
938
|
|
|
937
939
|
describe("beforeNav", () => {
|
|
@@ -969,17 +971,19 @@ describe("ClickableBehavior", () => {
|
|
|
969
971
|
);
|
|
970
972
|
|
|
971
973
|
// Act
|
|
972
|
-
userEvent.click(screen.
|
|
974
|
+
await userEvent.click(await screen.findByRole("button"));
|
|
973
975
|
|
|
974
976
|
// Assert
|
|
975
|
-
await waitFor(() => {
|
|
977
|
+
await waitFor(async () => {
|
|
976
978
|
expect(
|
|
977
|
-
screen.
|
|
979
|
+
await screen.findByText("Hello, world!"),
|
|
978
980
|
).toBeInTheDocument();
|
|
979
981
|
});
|
|
980
982
|
});
|
|
981
983
|
|
|
982
|
-
|
|
984
|
+
// NOTE(john): This no longer works after upgrading to user-event v14.
|
|
985
|
+
// The wait state is resolved before we can confirm that it's rendered.
|
|
986
|
+
it.skip("shows waiting state before navigating", async () => {
|
|
983
987
|
// Arrange
|
|
984
988
|
render(
|
|
985
989
|
<MemoryRouter>
|
|
@@ -1013,10 +1017,10 @@ describe("ClickableBehavior", () => {
|
|
|
1013
1017
|
);
|
|
1014
1018
|
|
|
1015
1019
|
// Act
|
|
1016
|
-
userEvent.click(screen.
|
|
1020
|
+
await userEvent.click(await screen.findByRole("button"));
|
|
1017
1021
|
|
|
1018
1022
|
// Assert
|
|
1019
|
-
expect(screen.
|
|
1023
|
+
expect(await screen.findByText("waiting")).toBeInTheDocument();
|
|
1020
1024
|
});
|
|
1021
1025
|
|
|
1022
1026
|
it("does not navigate if beforeNav rejects", async () => {
|
|
@@ -1051,7 +1055,7 @@ describe("ClickableBehavior", () => {
|
|
|
1051
1055
|
);
|
|
1052
1056
|
|
|
1053
1057
|
// Act
|
|
1054
|
-
userEvent.click(screen.
|
|
1058
|
+
await userEvent.click(await screen.findByRole("button"));
|
|
1055
1059
|
|
|
1056
1060
|
// Assert
|
|
1057
1061
|
expect(
|
|
@@ -1095,7 +1099,7 @@ describe("ClickableBehavior", () => {
|
|
|
1095
1099
|
);
|
|
1096
1100
|
|
|
1097
1101
|
// Act
|
|
1098
|
-
userEvent.click(screen.
|
|
1102
|
+
await userEvent.click(await screen.findByRole("button"));
|
|
1099
1103
|
|
|
1100
1104
|
// Assert
|
|
1101
1105
|
await waitFor(() => {
|
|
@@ -1134,13 +1138,15 @@ describe("ClickableBehavior", () => {
|
|
|
1134
1138
|
);
|
|
1135
1139
|
|
|
1136
1140
|
// Act
|
|
1137
|
-
userEvent.click(screen.
|
|
1141
|
+
await userEvent.click(await screen.findByRole("button"));
|
|
1138
1142
|
|
|
1139
1143
|
// Assert
|
|
1140
|
-
expect(
|
|
1144
|
+
expect(
|
|
1145
|
+
await screen.findByText("Hello, world!"),
|
|
1146
|
+
).toBeInTheDocument();
|
|
1141
1147
|
});
|
|
1142
1148
|
|
|
1143
|
-
it("If onClick calls e.preventDefault() then we won't navigate", () => {
|
|
1149
|
+
it("If onClick calls e.preventDefault() then we won't navigate", async () => {
|
|
1144
1150
|
// Arrange
|
|
1145
1151
|
render(
|
|
1146
1152
|
<MemoryRouter>
|
|
@@ -1169,7 +1175,7 @@ describe("ClickableBehavior", () => {
|
|
|
1169
1175
|
);
|
|
1170
1176
|
|
|
1171
1177
|
// Act
|
|
1172
|
-
userEvent.click(screen.
|
|
1178
|
+
await userEvent.click(await screen.findByRole("button"));
|
|
1173
1179
|
|
|
1174
1180
|
// Assert
|
|
1175
1181
|
expect(screen.queryByText("Hello, world!")).not.toBeInTheDocument();
|
|
@@ -1177,7 +1183,7 @@ describe("ClickableBehavior", () => {
|
|
|
1177
1183
|
});
|
|
1178
1184
|
|
|
1179
1185
|
describe("target='_blank'", () => {
|
|
1180
|
-
it("opens a new tab", () => {
|
|
1186
|
+
it("opens a new tab", async () => {
|
|
1181
1187
|
// Arrange
|
|
1182
1188
|
render(
|
|
1183
1189
|
<ClickableBehavior
|
|
@@ -1200,7 +1206,7 @@ describe("ClickableBehavior", () => {
|
|
|
1200
1206
|
);
|
|
1201
1207
|
|
|
1202
1208
|
// Act
|
|
1203
|
-
userEvent.click(screen.
|
|
1209
|
+
await userEvent.click(await screen.findByRole("link"));
|
|
1204
1210
|
|
|
1205
1211
|
// Assert
|
|
1206
1212
|
expect(window.open).toHaveBeenCalledWith(
|
|
@@ -1209,7 +1215,7 @@ describe("ClickableBehavior", () => {
|
|
|
1209
1215
|
);
|
|
1210
1216
|
});
|
|
1211
1217
|
|
|
1212
|
-
it("opens a new tab when using 'safeWithNav'", () => {
|
|
1218
|
+
it("opens a new tab when using 'safeWithNav'", async () => {
|
|
1213
1219
|
// Arrange
|
|
1214
1220
|
// @ts-expect-error [FEI-5019] - TS2554 - Expected 1 arguments, but got 0.
|
|
1215
1221
|
const safeWithNavMock = jest.fn().mockResolvedValue();
|
|
@@ -1235,7 +1241,7 @@ describe("ClickableBehavior", () => {
|
|
|
1235
1241
|
);
|
|
1236
1242
|
|
|
1237
1243
|
// Act
|
|
1238
|
-
userEvent.click(screen.
|
|
1244
|
+
await userEvent.click(await screen.findByRole("link"));
|
|
1239
1245
|
|
|
1240
1246
|
// Assert
|
|
1241
1247
|
expect(window.open).toHaveBeenCalledWith(
|
|
@@ -1244,7 +1250,7 @@ describe("ClickableBehavior", () => {
|
|
|
1244
1250
|
);
|
|
1245
1251
|
});
|
|
1246
1252
|
|
|
1247
|
-
it("calls 'safeWithNav'", () => {
|
|
1253
|
+
it("calls 'safeWithNav'", async () => {
|
|
1248
1254
|
// Arrange
|
|
1249
1255
|
// @ts-expect-error [FEI-5019] - TS2554 - Expected 1 arguments, but got 0.
|
|
1250
1256
|
const safeWithNavMock = jest.fn().mockResolvedValue();
|
|
@@ -1270,13 +1276,13 @@ describe("ClickableBehavior", () => {
|
|
|
1270
1276
|
);
|
|
1271
1277
|
|
|
1272
1278
|
// Act
|
|
1273
|
-
userEvent.click(screen.
|
|
1279
|
+
await userEvent.click(await screen.findByRole("link"));
|
|
1274
1280
|
|
|
1275
1281
|
// Assert
|
|
1276
1282
|
expect(safeWithNavMock).toHaveBeenCalled();
|
|
1277
1283
|
});
|
|
1278
1284
|
|
|
1279
|
-
it("opens a new tab when inside a router", () => {
|
|
1285
|
+
it("opens a new tab when inside a router", async () => {
|
|
1280
1286
|
// Arrange
|
|
1281
1287
|
render(
|
|
1282
1288
|
<MemoryRouter initialEntries={["/"]}>
|
|
@@ -1301,7 +1307,7 @@ describe("ClickableBehavior", () => {
|
|
|
1301
1307
|
);
|
|
1302
1308
|
|
|
1303
1309
|
// Act
|
|
1304
|
-
userEvent.click(screen.
|
|
1310
|
+
await userEvent.click(await screen.findByRole("link"));
|
|
1305
1311
|
|
|
1306
1312
|
// Assert
|
|
1307
1313
|
expect(window.open).toHaveBeenCalledWith(
|
|
@@ -1310,7 +1316,7 @@ describe("ClickableBehavior", () => {
|
|
|
1310
1316
|
);
|
|
1311
1317
|
});
|
|
1312
1318
|
|
|
1313
|
-
it("opens a new tab when using 'safeWithNav' inside a router", () => {
|
|
1319
|
+
it("opens a new tab when using 'safeWithNav' inside a router", async () => {
|
|
1314
1320
|
// Arrange
|
|
1315
1321
|
// @ts-expect-error [FEI-5019] - TS2554 - Expected 1 arguments, but got 0.
|
|
1316
1322
|
const safeWithNavMock = jest.fn().mockResolvedValue();
|
|
@@ -1338,7 +1344,7 @@ describe("ClickableBehavior", () => {
|
|
|
1338
1344
|
);
|
|
1339
1345
|
|
|
1340
1346
|
// Act
|
|
1341
|
-
userEvent.click(screen.
|
|
1347
|
+
await userEvent.click(await screen.findByRole("link"));
|
|
1342
1348
|
|
|
1343
1349
|
// Assert
|
|
1344
1350
|
expect(window.open).toHaveBeenCalledWith(
|
|
@@ -1347,7 +1353,7 @@ describe("ClickableBehavior", () => {
|
|
|
1347
1353
|
);
|
|
1348
1354
|
});
|
|
1349
1355
|
|
|
1350
|
-
it("calls 'safeWithNav' inside a router", () => {
|
|
1356
|
+
it("calls 'safeWithNav' inside a router", async () => {
|
|
1351
1357
|
// Arrange
|
|
1352
1358
|
// @ts-expect-error [FEI-5019] - TS2554 - Expected 1 arguments, but got 0.
|
|
1353
1359
|
const safeWithNavMock = jest.fn().mockResolvedValue();
|
|
@@ -1375,7 +1381,7 @@ describe("ClickableBehavior", () => {
|
|
|
1375
1381
|
);
|
|
1376
1382
|
|
|
1377
1383
|
// Act
|
|
1378
|
-
userEvent.click(screen.
|
|
1384
|
+
await userEvent.click(await screen.findByRole("link"));
|
|
1379
1385
|
|
|
1380
1386
|
// Assert
|
|
1381
1387
|
expect(safeWithNavMock).toHaveBeenCalled();
|
|
@@ -1383,7 +1389,7 @@ describe("ClickableBehavior", () => {
|
|
|
1383
1389
|
});
|
|
1384
1390
|
|
|
1385
1391
|
describe("rel", () => {
|
|
1386
|
-
it("should use the 'rel' that was passed in", () => {
|
|
1392
|
+
it("should use the 'rel' that was passed in", async () => {
|
|
1387
1393
|
// Arrange
|
|
1388
1394
|
const childrenMock = jest.fn().mockImplementation(() => null);
|
|
1389
1395
|
render(
|
|
@@ -1400,7 +1406,7 @@ describe("ClickableBehavior", () => {
|
|
|
1400
1406
|
expect(childrenProps.rel).toEqual("something_else");
|
|
1401
1407
|
});
|
|
1402
1408
|
|
|
1403
|
-
it("should use 'noopener noreferrer' as a default when target='_blank'", () => {
|
|
1409
|
+
it("should use 'noopener noreferrer' as a default when target='_blank'", async () => {
|
|
1404
1410
|
// Arrange
|
|
1405
1411
|
const childrenMock = jest.fn().mockImplementation(() => null);
|
|
1406
1412
|
render(
|
|
@@ -1416,7 +1422,7 @@ describe("ClickableBehavior", () => {
|
|
|
1416
1422
|
expect(childrenProps.rel).toEqual("noopener noreferrer");
|
|
1417
1423
|
});
|
|
1418
1424
|
|
|
1419
|
-
it("should not use the default if target != '_blank'", () => {
|
|
1425
|
+
it("should not use the default if target != '_blank'", async () => {
|
|
1420
1426
|
// Arrange
|
|
1421
1427
|
const childrenMock = jest.fn().mockImplementation(() => null);
|
|
1422
1428
|
render(
|