1
- import { describe , expect , it , vi } from 'vitest'
2
- import { fireEvent , waitFor } from '@testing-library/react'
1
+ import { afterEach , beforeEach , describe , expect , it , vi } from 'vitest'
2
+ import { fireEvent } from '@testing-library/react'
3
3
import * as React from 'react'
4
4
import { ErrorBoundary } from 'react-error-boundary'
5
5
import { queryKey , sleep } from '@tanstack/query-test-utils'
@@ -20,6 +20,14 @@ import type {
20
20
} from '..'
21
21
22
22
describe ( 'useSuspenseQuery' , ( ) => {
23
+ beforeEach ( ( ) => {
24
+ vi . useFakeTimers ( )
25
+ } )
26
+
27
+ afterEach ( ( ) => {
28
+ vi . useRealTimers ( )
29
+ } )
30
+
23
31
const queryCache = new QueryCache ( )
24
32
const queryClient = new QueryClient ( { queryCache } )
25
33
@@ -61,10 +69,10 @@ describe('useSuspenseQuery', () => {
61
69
</ React . Suspense > ,
62
70
)
63
71
64
- await waitFor ( ( ) => rendered . getByText ( 'data: 1' ) )
72
+ await vi . waitFor ( ( ) => rendered . getByText ( 'data: 1' ) )
65
73
fireEvent . click ( rendered . getByLabelText ( 'toggle' ) )
66
74
67
- await waitFor ( ( ) => rendered . getByText ( 'data: 2' ) )
75
+ await vi . waitFor ( ( ) => rendered . getByText ( 'data: 2' ) )
68
76
69
77
expect ( renders ) . toBe ( 6 )
70
78
expect ( states . length ) . toBe ( 2 )
@@ -104,7 +112,7 @@ describe('useSuspenseQuery', () => {
104
112
</ React . Suspense > ,
105
113
)
106
114
107
- await waitFor ( ( ) => rendered . getByText ( 'data: 1' ) )
115
+ await vi . waitFor ( ( ) => rendered . getByText ( 'data: 1' ) )
108
116
109
117
expect ( states . length ) . toBe ( 1 )
110
118
expect ( states [ 0 ] ) . toMatchObject ( {
@@ -113,7 +121,7 @@ describe('useSuspenseQuery', () => {
113
121
} )
114
122
115
123
fireEvent . click ( rendered . getByText ( 'next' ) )
116
- await waitFor ( ( ) => rendered . getByText ( 'data: 2' ) )
124
+ await vi . waitFor ( ( ) => rendered . getByText ( 'data: 2' ) )
117
125
118
126
expect ( states . length ) . toBe ( 2 )
119
127
expect ( states [ 1 ] ) . toMatchObject ( {
@@ -144,7 +152,7 @@ describe('useSuspenseQuery', () => {
144
152
</ React . Suspense > ,
145
153
)
146
154
147
- await waitFor ( ( ) => rendered . getByText ( 'rendered' ) )
155
+ await vi . waitFor ( ( ) => rendered . getByText ( 'rendered' ) )
148
156
149
157
expect ( queryFn ) . toHaveBeenCalledTimes ( 1 )
150
158
} )
@@ -184,7 +192,7 @@ describe('useSuspenseQuery', () => {
184
192
expect ( queryCache . find ( { queryKey : key } ) ) . toBeFalsy ( )
185
193
186
194
fireEvent . click ( rendered . getByLabelText ( 'toggle' ) )
187
- await waitFor ( ( ) => rendered . getByText ( 'rendered' ) )
195
+ await vi . waitFor ( ( ) => rendered . getByText ( 'rendered' ) )
188
196
189
197
expect ( queryCache . find ( { queryKey : key } ) ?. getObserversCount ( ) ) . toBe ( 1 )
190
198
@@ -249,15 +257,15 @@ describe('useSuspenseQuery', () => {
249
257
</ QueryErrorResetBoundary > ,
250
258
)
251
259
252
- await waitFor ( ( ) => rendered . getByText ( 'Loading...' ) )
260
+ await vi . waitFor ( ( ) => rendered . getByText ( 'Loading...' ) )
253
261
254
- await waitFor ( ( ) => rendered . getByText ( 'error boundary' ) )
262
+ await vi . waitFor ( ( ) => rendered . getByText ( 'error boundary' ) )
255
263
256
- await waitFor ( ( ) => rendered . getByText ( 'retry' ) )
264
+ await vi . waitFor ( ( ) => rendered . getByText ( 'retry' ) )
257
265
258
266
fireEvent . click ( rendered . getByText ( 'retry' ) )
259
267
260
- await waitFor ( ( ) => rendered . getByText ( 'rendered' ) )
268
+ await vi . waitFor ( ( ) => rendered . getByText ( 'rendered' ) )
261
269
262
270
expect ( consoleMock . mock . calls [ 0 ] ?. [ 1 ] ) . toStrictEqual (
263
271
new Error ( 'Suspense Error Bingo' ) ,
@@ -317,21 +325,25 @@ describe('useSuspenseQuery', () => {
317
325
</ QueryErrorResetBoundary > ,
318
326
)
319
327
320
- await waitFor ( ( ) =>
328
+ await vi . waitFor ( ( ) =>
321
329
expect ( rendered . getByText ( 'Loading...' ) ) . toBeInTheDocument ( ) ,
322
330
)
323
- await waitFor ( ( ) =>
331
+ await vi . waitFor ( ( ) =>
324
332
expect ( rendered . getByText ( 'error boundary' ) ) . toBeInTheDocument ( ) ,
325
333
)
326
- await waitFor ( ( ) => expect ( rendered . getByText ( 'retry' ) ) . toBeInTheDocument ( ) )
334
+ await vi . waitFor ( ( ) =>
335
+ expect ( rendered . getByText ( 'retry' ) ) . toBeInTheDocument ( ) ,
336
+ )
327
337
fireEvent . click ( rendered . getByText ( 'retry' ) )
328
- await waitFor ( ( ) =>
338
+ await vi . waitFor ( ( ) =>
329
339
expect ( rendered . getByText ( 'error boundary' ) ) . toBeInTheDocument ( ) ,
330
340
)
331
- await waitFor ( ( ) => expect ( rendered . getByText ( 'retry' ) ) . toBeInTheDocument ( ) )
341
+ await vi . waitFor ( ( ) =>
342
+ expect ( rendered . getByText ( 'retry' ) ) . toBeInTheDocument ( ) ,
343
+ )
332
344
succeed = true
333
345
fireEvent . click ( rendered . getByText ( 'retry' ) )
334
- await waitFor ( ( ) =>
346
+ await vi . waitFor ( ( ) =>
335
347
expect ( rendered . getByText ( 'rendered' ) ) . toBeInTheDocument ( ) ,
336
348
)
337
349
consoleMock . mockRestore ( )
@@ -368,8 +380,8 @@ describe('useSuspenseQuery', () => {
368
380
369
381
const rendered = renderWithClient ( queryClient , < Page /> )
370
382
371
- await waitFor ( ( ) => rendered . getByText ( 'Loading...' ) )
372
- await waitFor ( ( ) => rendered . getByText ( 'data: 1' ) )
383
+ await vi . waitFor ( ( ) => rendered . getByText ( 'Loading...' ) )
384
+ await vi . waitFor ( ( ) => rendered . getByText ( 'data: 1' ) )
373
385
374
386
expect (
375
387
typeof queryClient . getQueryCache ( ) . find ( { queryKey : key } ) ?. observers [ 0 ]
@@ -414,17 +426,17 @@ describe('useSuspenseQuery', () => {
414
426
415
427
const rendered = renderWithClient ( queryClient , < Page /> )
416
428
417
- await waitFor ( ( ) =>
429
+ await vi . waitFor ( ( ) =>
418
430
expect ( rendered . getByText ( 'Loading...' ) ) . toBeInTheDocument ( ) ,
419
431
)
420
- await waitFor ( ( ) =>
432
+ await vi . waitFor ( ( ) =>
421
433
expect ( rendered . getByText ( `data: ${ key1 } ` ) ) . toBeInTheDocument ( ) ,
422
434
)
423
435
fireEvent . click ( rendered . getByText ( 'switch' ) )
424
- await waitFor ( ( ) =>
436
+ await vi . waitFor ( ( ) =>
425
437
expect ( rendered . getByText ( 'Loading...' ) ) . toBeInTheDocument ( ) ,
426
438
)
427
- await waitFor ( ( ) =>
439
+ await vi . waitFor ( ( ) =>
428
440
expect ( rendered . getByText ( `data: ${ key2 } ` ) ) . toBeInTheDocument ( ) ,
429
441
)
430
442
} )
@@ -480,21 +492,25 @@ describe('useSuspenseQuery', () => {
480
492
481
493
const rendered = renderWithClient ( queryClient , < App /> )
482
494
483
- await waitFor ( ( ) =>
495
+ await vi . waitFor ( ( ) =>
484
496
expect ( rendered . getByText ( 'Loading...' ) ) . toBeInTheDocument ( ) ,
485
497
)
486
- await waitFor ( ( ) =>
498
+ await vi . waitFor ( ( ) =>
487
499
expect ( rendered . getByText ( 'error boundary' ) ) . toBeInTheDocument ( ) ,
488
500
)
489
- await waitFor ( ( ) => expect ( rendered . getByText ( 'retry' ) ) . toBeInTheDocument ( ) )
501
+ await vi . waitFor ( ( ) =>
502
+ expect ( rendered . getByText ( 'retry' ) ) . toBeInTheDocument ( ) ,
503
+ )
490
504
fireEvent . click ( rendered . getByText ( 'retry' ) )
491
- await waitFor ( ( ) =>
505
+ await vi . waitFor ( ( ) =>
492
506
expect ( rendered . getByText ( 'error boundary' ) ) . toBeInTheDocument ( ) ,
493
507
)
494
- await waitFor ( ( ) => expect ( rendered . getByText ( 'retry' ) ) . toBeInTheDocument ( ) )
508
+ await vi . waitFor ( ( ) =>
509
+ expect ( rendered . getByText ( 'retry' ) ) . toBeInTheDocument ( ) ,
510
+ )
495
511
succeed = true
496
512
fireEvent . click ( rendered . getByText ( 'retry' ) )
497
- await waitFor ( ( ) =>
513
+ await vi . waitFor ( ( ) =>
498
514
expect ( rendered . getByText ( 'rendered' ) ) . toBeInTheDocument ( ) ,
499
515
)
500
516
consoleMock . mockRestore ( )
@@ -536,10 +552,10 @@ describe('useSuspenseQuery', () => {
536
552
537
553
const rendered = renderWithClient ( queryClient , < App /> )
538
554
539
- await waitFor ( ( ) =>
555
+ await vi . waitFor ( ( ) =>
540
556
expect ( rendered . getByText ( 'Loading...' ) ) . toBeInTheDocument ( ) ,
541
557
)
542
- await waitFor ( ( ) =>
558
+ await vi . waitFor ( ( ) =>
543
559
expect ( rendered . getByText ( 'error boundary' ) ) . toBeInTheDocument ( ) ,
544
560
)
545
561
consoleMock . mockRestore ( )
@@ -583,10 +599,10 @@ describe('useSuspenseQuery', () => {
583
599
584
600
const rendered = renderWithClient ( queryClient , < App /> )
585
601
586
- await waitFor ( ( ) =>
602
+ await vi . waitFor ( ( ) =>
587
603
expect ( rendered . getByText ( 'Loading...' ) ) . toBeInTheDocument ( ) ,
588
604
)
589
- await waitFor ( ( ) =>
605
+ await vi . waitFor ( ( ) =>
590
606
expect ( rendered . getByText ( 'error boundary' ) ) . toBeInTheDocument ( ) ,
591
607
)
592
608
consoleMock . mockRestore ( )
@@ -647,16 +663,16 @@ describe('useSuspenseQuery', () => {
647
663
const rendered = renderWithClient ( queryClient , < App /> )
648
664
649
665
// render suspense fallback (Loading...)
650
- await waitFor ( ( ) => rendered . getByText ( 'Loading...' ) )
666
+ await vi . waitFor ( ( ) => rendered . getByText ( 'Loading...' ) )
651
667
// resolve promise -> render Page (rendered)
652
- await waitFor ( ( ) => rendered . getByText ( 'rendered' ) )
668
+ await vi . waitFor ( ( ) => rendered . getByText ( 'rendered' ) )
653
669
654
670
// change query key
655
671
succeed = false
656
672
// reset query -> and throw error
657
673
fireEvent . click ( rendered . getByLabelText ( 'fail' ) )
658
674
// render error boundary fallback (error boundary)
659
- await waitFor ( ( ) => rendered . getByText ( 'error boundary' ) )
675
+ await vi . waitFor ( ( ) => rendered . getByText ( 'error boundary' ) )
660
676
expect ( consoleMock . mock . calls [ 0 ] ?. [ 1 ] ) . toStrictEqual (
661
677
new Error ( 'Suspense Error Bingo' ) ,
662
678
)
@@ -718,16 +734,16 @@ describe('useSuspenseQuery', () => {
718
734
const rendered = renderWithClient ( queryClient , < App /> )
719
735
720
736
// render suspense fallback (Loading...)
721
- await waitFor ( ( ) => rendered . getByText ( 'Loading...' ) )
737
+ await vi . waitFor ( ( ) => rendered . getByText ( 'Loading...' ) )
722
738
// resolve promise -> render Page (rendered)
723
- await waitFor ( ( ) => rendered . getByText ( 'rendered' ) )
739
+ await vi . waitFor ( ( ) => rendered . getByText ( 'rendered' ) )
724
740
725
741
// change promise result to error
726
742
succeed = false
727
743
// change query key
728
744
fireEvent . click ( rendered . getByLabelText ( 'fail' ) )
729
745
// render error boundary fallback (error boundary)
730
- await waitFor ( ( ) => rendered . getByText ( 'error boundary' ) )
746
+ await vi . waitFor ( ( ) => rendered . getByText ( 'error boundary' ) )
731
747
expect ( consoleMock . mock . calls [ 0 ] ?. [ 1 ] ) . toStrictEqual (
732
748
new Error ( 'Suspense Error Bingo' ) ,
733
749
)
@@ -769,15 +785,17 @@ describe('useSuspenseQuery', () => {
769
785
</ React . Suspense > ,
770
786
)
771
787
772
- await waitFor ( ( ) =>
788
+ await vi . waitFor ( ( ) =>
773
789
expect ( state ) . toMatchObject ( {
774
790
data : 1 ,
775
791
status : 'success' ,
776
792
} ) ,
777
793
)
778
794
779
795
expect ( renders ) . toBe ( 3 )
780
- await waitFor ( ( ) => expect ( rendered . queryByText ( 'rendered' ) ) . not . toBeNull ( ) )
796
+ await vi . waitFor ( ( ) =>
797
+ expect ( rendered . queryByText ( 'rendered' ) ) . not . toBeNull ( ) ,
798
+ )
781
799
} )
782
800
783
801
it ( 'should not throw background errors to the error boundary' , async ( ) => {
@@ -828,11 +846,11 @@ describe('useSuspenseQuery', () => {
828
846
const rendered = renderWithClient ( queryClient , < App /> )
829
847
830
848
// render suspense fallback (Loading...)
831
- await waitFor ( ( ) =>
849
+ await vi . waitFor ( ( ) =>
832
850
expect ( rendered . getByText ( 'Loading...' ) ) . toBeInTheDocument ( ) ,
833
851
)
834
852
// resolve promise -> render Page (rendered)
835
- await waitFor ( ( ) =>
853
+ await vi . waitFor ( ( ) =>
836
854
expect ( rendered . getByText ( 'rendered data success' ) ) . toBeInTheDocument ( ) ,
837
855
)
838
856
@@ -841,7 +859,7 @@ describe('useSuspenseQuery', () => {
841
859
// refetch
842
860
fireEvent . click ( rendered . getByRole ( 'button' , { name : 'refetch' } ) )
843
861
// we are now in error state but still have data to show
844
- await waitFor ( ( ) =>
862
+ await vi . waitFor ( ( ) =>
845
863
expect ( rendered . getByText ( 'rendered data error' ) ) . toBeInTheDocument ( ) ,
846
864
)
847
865
@@ -889,18 +907,18 @@ describe('useSuspenseQuery', () => {
889
907
< Page />
890
908
</ React . Suspense > ,
891
909
)
892
- await waitFor ( ( ) =>
910
+ await vi . waitFor ( ( ) =>
893
911
expect ( rendered . getByText ( 'loading' ) ) . toBeInTheDocument ( ) ,
894
912
)
895
- await waitFor ( ( ) =>
913
+ await vi . waitFor ( ( ) =>
896
914
expect ( rendered . getByText ( 'data: 1' ) ) . toBeInTheDocument ( ) ,
897
915
)
898
916
fireEvent . click ( rendered . getByLabelText ( 'toggle' ) )
899
917
900
- await waitFor ( ( ) =>
918
+ await vi . waitFor ( ( ) =>
901
919
expect ( rendered . getByText ( 'loading' ) ) . toBeInTheDocument ( ) ,
902
920
)
903
- await waitFor ( ( ) =>
921
+ await vi . waitFor ( ( ) =>
904
922
expect ( rendered . getByText ( 'data: 2' ) ) . toBeInTheDocument ( ) ,
905
923
)
906
924
} )
@@ -961,9 +979,9 @@ describe('useSuspenseQuery', () => {
961
979
</ React . Suspense > ,
962
980
)
963
981
964
- await waitFor ( ( ) => rendered . getByText ( 'count: 1' ) )
965
- await waitFor ( ( ) => rendered . getByText ( 'count: 2' ) )
966
- await waitFor ( ( ) => rendered . getByText ( 'count: 3' ) )
982
+ await vi . waitFor ( ( ) => rendered . getByText ( 'count: 1' ) )
983
+ await vi . waitFor ( ( ) => rendered . getByText ( 'count: 2' ) )
984
+ await vi . waitFor ( ( ) => rendered . getByText ( 'count: 3' ) )
967
985
968
986
expect ( count ) . toBeGreaterThanOrEqual ( 3 )
969
987
} )
0 commit comments