@@ -16,6 +16,16 @@ beforeAll(() => {
16
16
17
17
afterAll ( ( ) => jest . restoreAllMocks ( ) )
18
18
19
+ function nextFrame ( ) {
20
+ return new Promise < void > ( ( resolve ) => {
21
+ requestAnimationFrame ( ( ) => {
22
+ requestAnimationFrame ( ( ) => {
23
+ resolve ( )
24
+ } )
25
+ } )
26
+ } )
27
+ }
28
+
19
29
const renderTemplate = createRenderTemplate ( {
20
30
FocusTrap,
21
31
} )
@@ -29,7 +39,7 @@ it('should focus the first focusable element inside the FocusTrap', async () =>
29
39
`
30
40
)
31
41
32
- await new Promise < void > ( nextTick )
42
+ await nextFrame ( )
33
43
34
44
assertActiveElement ( getByText ( 'Trigger' ) )
35
45
} )
@@ -52,7 +62,7 @@ it('should focus the autoFocus element inside the FocusTrap if that exists', asy
52
62
} ,
53
63
} )
54
64
55
- await new Promise < void > ( nextTick )
65
+ await nextFrame ( )
56
66
57
67
assertActiveElement ( document . getElementById ( 'b' ) )
58
68
} )
@@ -72,7 +82,7 @@ it('should focus the initialFocus element inside the FocusTrap if that exists',
72
82
} ,
73
83
} )
74
84
75
- await new Promise < void > ( nextTick )
85
+ await nextFrame ( )
76
86
77
87
assertActiveElement ( document . getElementById ( 'c' ) )
78
88
} )
@@ -92,7 +102,7 @@ it('should focus the initialFocus element inside the FocusTrap even if another e
92
102
} ,
93
103
} )
94
104
95
- await new Promise < void > ( nextTick )
105
+ await nextFrame ( )
96
106
97
107
assertActiveElement ( document . getElementById ( 'c' ) )
98
108
} )
@@ -109,7 +119,7 @@ it('should warn when there is no focusable element inside the FocusTrap', async
109
119
`
110
120
)
111
121
112
- await new Promise < void > ( nextTick )
122
+ await nextFrame ( )
113
123
114
124
expect ( spy . mock . calls [ 0 ] [ 0 ] ) . toBe ( 'There are no focusable elements inside the <FocusTrap />' )
115
125
spy . mockReset ( )
132
142
` ,
133
143
} )
134
144
135
- await new Promise < void > ( nextTick )
145
+ await nextFrame ( )
136
146
137
147
let [ a , b , c , d ] = Array . from ( document . querySelectorAll ( 'input' ) )
138
148
@@ -199,7 +209,7 @@ it('should restore the previously focused element, before entering the FocusTrap
199
209
} ,
200
210
} )
201
211
202
- await new Promise < void > ( nextTick )
212
+ await nextFrame ( )
203
213
204
214
// The input should have focus by default because of the autoFocus prop
205
215
assertActiveElement ( document . getElementById ( 'item-1' ) )
@@ -232,7 +242,7 @@ it('should be possible to tab to the next focusable element within the focus tra
232
242
`
233
243
)
234
244
235
- await new Promise < void > ( nextTick )
245
+ await nextFrame ( )
236
246
237
247
// Item A should be focused because the FocusTrap will focus the first item
238
248
assertActiveElement ( document . getElementById ( 'item-a' ) )
@@ -265,6 +275,8 @@ it('should be possible to shift+tab to the previous focusable element within the
265
275
`
266
276
)
267
277
278
+ await nextFrame ( )
279
+
268
280
// Item A should be focused because the FocusTrap will focus the first item
269
281
assertActiveElement ( document . getElementById ( 'item-a' ) )
270
282
@@ -297,6 +309,8 @@ it('should skip the initial "hidden" elements within the focus trap', async () =
297
309
`
298
310
)
299
311
312
+ await nextFrame ( )
313
+
300
314
// Item C should be focused because the FocusTrap had to skip the first 2
301
315
assertActiveElement ( document . getElementById ( 'item-c' ) )
302
316
} )
@@ -317,6 +331,8 @@ it('should be possible skip "hidden" elements within the focus trap', async () =
317
331
`
318
332
)
319
333
334
+ await nextFrame ( )
335
+
320
336
// Item A should be focused because the FocusTrap will focus the first item
321
337
assertActiveElement ( document . getElementById ( 'item-a' ) )
322
338
@@ -351,6 +367,8 @@ it('should be possible skip disabled elements within the focus trap', async () =
351
367
`
352
368
)
353
369
370
+ await nextFrame ( )
371
+
354
372
// Item A should be focused because the FocusTrap will focus the first item
355
373
assertActiveElement ( document . getElementById ( 'item-a' ) )
356
374
@@ -397,6 +415,8 @@ it('should try to focus all focusable items in order (and fail)', async () => {
397
415
} ,
398
416
} )
399
417
418
+ await nextFrame ( )
419
+
400
420
expect ( focusHandler . mock . calls ) . toEqual ( [ [ 'item-a' ] , [ 'item-b' ] , [ 'item-c' ] , [ 'item-d' ] ] )
401
421
expect ( spy ) . toHaveBeenCalledWith ( 'There are no focusable elements inside the <FocusTrap />' )
402
422
spy . mockReset ( )
@@ -430,6 +450,8 @@ it('should end up at the last focusable element', async () => {
430
450
} ,
431
451
} )
432
452
453
+ await nextFrame ( )
454
+
433
455
expect ( focusHandler . mock . calls ) . toEqual ( [ [ 'item-a' ] , [ 'item-b' ] , [ 'item-c' ] ] )
434
456
assertActiveElement ( getByText ( 'Item D' ) )
435
457
expect ( spy ) . not . toHaveBeenCalled ( )
0 commit comments