@@ -8,9 +8,12 @@ import {
8
8
waitFor ,
9
9
} from '@testing-library/react'
10
10
import userEvent from '@testing-library/user-event'
11
- import { HttpResponse , http } from 'msw'
11
+ import { HttpResponse , delay , http } from 'msw'
12
12
import util from 'util'
13
- import type { InfiniteQueryActionCreatorResult } from '@reduxjs/toolkit/query/react'
13
+ import type {
14
+ InfiniteQueryActionCreatorResult ,
15
+ QueryCacheKey ,
16
+ } from '@reduxjs/toolkit/query/react'
14
17
import {
15
18
QueryStatus ,
16
19
createApi ,
@@ -101,6 +104,40 @@ describe('Infinite queries', () => {
101
104
} ) ,
102
105
} )
103
106
107
+ let hitCounter = 0
108
+
109
+ type HitCounter = { page : number ; hitCounter : number }
110
+
111
+ const countersApi = createApi ( {
112
+ baseQuery : fakeBaseQuery ( ) ,
113
+ tagTypes : [ 'Counter' ] ,
114
+ endpoints : ( build ) => ( {
115
+ counters : build . infiniteQuery < HitCounter , string , number > ( {
116
+ queryFn ( page ) {
117
+ hitCounter ++
118
+
119
+ return { data : { page, hitCounter } }
120
+ } ,
121
+ infiniteQueryOptions : {
122
+ initialPageParam : 0 ,
123
+ getNextPageParam : (
124
+ lastPage ,
125
+ allPages ,
126
+ lastPageParam ,
127
+ allPageParams ,
128
+ ) => lastPageParam + 1 ,
129
+ } ,
130
+ providesTags : [ 'Counter' ] ,
131
+ } ) ,
132
+ mutation : build . mutation < null , void > ( {
133
+ queryFn : async ( ) => {
134
+ return { data : null }
135
+ } ,
136
+ invalidatesTags : [ 'Counter' ] ,
137
+ } ) ,
138
+ } ) ,
139
+ } )
140
+
104
141
let storeRef = setupApiStore (
105
142
pokemonApi ,
106
143
{ ...actionsReducer } ,
@@ -133,6 +170,8 @@ describe('Infinite queries', () => {
133
170
134
171
counters = { }
135
172
173
+ hitCounter = 0
174
+
136
175
process . env . NODE_ENV = 'development'
137
176
} )
138
177
@@ -404,32 +443,133 @@ describe('Infinite queries', () => {
404
443
} )
405
444
406
445
test ( 'refetches all existing pages' , async ( ) => {
407
- let hitCounter = 0
446
+ const checkResultData = (
447
+ result : InfiniteQueryResult ,
448
+ expectedValues : HitCounter [ ] ,
449
+ ) => {
450
+ expect ( result . status ) . toBe ( QueryStatus . fulfilled )
451
+ if ( result . status === QueryStatus . fulfilled ) {
452
+ expect ( result . data . pages ) . toEqual ( expectedValues )
453
+ }
454
+ }
408
455
409
- type HitCounter = { page : number ; hitCounter : number }
456
+ const storeRef = setupApiStore (
457
+ countersApi ,
458
+ { ...actionsReducer } ,
459
+ {
460
+ withoutTestLifecycles : true ,
461
+ } ,
462
+ )
410
463
411
- const countersApi = createApi ( {
412
- baseQuery : fakeBaseQuery ( ) ,
413
- endpoints : ( build ) => ( {
414
- counters : build . infiniteQuery < HitCounter , string , number > ( {
415
- queryFn ( page ) {
416
- hitCounter ++
464
+ await storeRef . store . dispatch (
465
+ countersApi . endpoints . counters . initiate ( 'item' , {
466
+ initialPageParam : 3 ,
467
+ } ) ,
468
+ )
417
469
418
- return { data : { page, hitCounter } }
419
- } ,
420
- infiniteQueryOptions : {
421
- initialPageParam : 0 ,
422
- getNextPageParam : (
423
- lastPage ,
424
- allPages ,
425
- lastPageParam ,
426
- allPageParams ,
427
- ) => lastPageParam + 1 ,
428
- } ,
429
- } ) ,
470
+ await storeRef . store . dispatch (
471
+ countersApi . endpoints . counters . initiate ( 'item' , {
472
+ direction : 'forward' ,
473
+ } ) ,
474
+ )
475
+
476
+ const thirdPromise = storeRef . store . dispatch (
477
+ countersApi . endpoints . counters . initiate ( 'item' , {
478
+ direction : 'forward' ,
430
479
} ) ,
480
+ )
481
+
482
+ const thirdRes = await thirdPromise
483
+
484
+ checkResultData ( thirdRes , [
485
+ { page : 3 , hitCounter : 1 } ,
486
+ { page : 4 , hitCounter : 2 } ,
487
+ { page : 5 , hitCounter : 3 } ,
488
+ ] )
489
+
490
+ const fourthRes = await thirdPromise . refetch ( )
491
+
492
+ checkResultData ( fourthRes , [
493
+ { page : 3 , hitCounter : 4 } ,
494
+ { page : 4 , hitCounter : 5 } ,
495
+ { page : 5 , hitCounter : 6 } ,
496
+ ] )
497
+ } )
498
+
499
+ test ( 'Refetches on invalidation' , async ( ) => {
500
+ const checkResultData = (
501
+ result : InfiniteQueryResult ,
502
+ expectedValues : HitCounter [ ] ,
503
+ ) => {
504
+ expect ( result . status ) . toBe ( QueryStatus . fulfilled )
505
+ if ( result . status === QueryStatus . fulfilled ) {
506
+ expect ( result . data . pages ) . toEqual ( expectedValues )
507
+ }
508
+ }
509
+
510
+ const storeRef = setupApiStore (
511
+ countersApi ,
512
+ { ...actionsReducer } ,
513
+ {
514
+ withoutTestLifecycles : true ,
515
+ } ,
516
+ )
517
+
518
+ await storeRef . store . dispatch (
519
+ countersApi . endpoints . counters . initiate ( 'item' , {
520
+ initialPageParam : 3 ,
521
+ } ) ,
522
+ )
523
+
524
+ await storeRef . store . dispatch (
525
+ countersApi . endpoints . counters . initiate ( 'item' , {
526
+ direction : 'forward' ,
527
+ } ) ,
528
+ )
529
+
530
+ const thirdPromise = storeRef . store . dispatch (
531
+ countersApi . endpoints . counters . initiate ( 'item' , {
532
+ direction : 'forward' ,
533
+ } ) ,
534
+ )
535
+
536
+ const thirdRes = await thirdPromise
537
+
538
+ checkResultData ( thirdRes , [
539
+ { page : 3 , hitCounter : 1 } ,
540
+ { page : 4 , hitCounter : 2 } ,
541
+ { page : 5 , hitCounter : 3 } ,
542
+ ] )
543
+
544
+ await storeRef . store . dispatch ( countersApi . endpoints . mutation . initiate ( ) )
545
+
546
+ let entry = countersApi . endpoints . counters . select ( 'item' ) (
547
+ storeRef . store . getState ( ) ,
548
+ )
549
+ const promise = storeRef . store . dispatch (
550
+ countersApi . util . getRunningQueryThunk ( 'counters' , 'item' ) ,
551
+ )
552
+ const promises = storeRef . store . dispatch (
553
+ countersApi . util . getRunningQueriesThunk ( ) ,
554
+ )
555
+ expect ( entry ) . toMatchObject ( {
556
+ status : 'pending' ,
431
557
} )
432
558
559
+ expect ( promise ) . toBeInstanceOf ( Promise )
560
+
561
+ expect ( promises ) . toEqual ( [ promise ] )
562
+
563
+ const finalRes = await promise
564
+
565
+ checkResultData ( finalRes as any , [
566
+ { page : 3 , hitCounter : 4 } ,
567
+ { page : 4 , hitCounter : 5 } ,
568
+ { page : 5 , hitCounter : 6 } ,
569
+ ] )
570
+ } )
571
+
572
+ test ( 'Refetches on polling' , async ( ) => {
433
573
const checkResultData = (
434
574
result : InfiniteQueryResult ,
435
575
expectedValues : HitCounter [ ] ,
@@ -474,9 +614,29 @@ describe('Infinite queries', () => {
474
614
{ page : 5 , hitCounter : 3 } ,
475
615
] )
476
616
477
- const fourthRes = await thirdPromise . refetch ( )
617
+ thirdPromise . updateSubscriptionOptions ( {
618
+ pollingInterval : 10 ,
619
+ } )
478
620
479
- checkResultData ( fourthRes , [
621
+ await delay ( 5 )
622
+
623
+ let entry = countersApi . endpoints . counters . select ( 'item' ) (
624
+ storeRef . store . getState ( ) ,
625
+ )
626
+
627
+ checkResultData ( thirdRes , [
628
+ { page : 3 , hitCounter : 1 } ,
629
+ { page : 4 , hitCounter : 2 } ,
630
+ { page : 5 , hitCounter : 3 } ,
631
+ ] )
632
+
633
+ await delay ( 10 )
634
+
635
+ entry = countersApi . endpoints . counters . select ( 'item' ) (
636
+ storeRef . store . getState ( ) ,
637
+ )
638
+
639
+ checkResultData ( entry as any , [
480
640
{ page : 3 , hitCounter : 4 } ,
481
641
{ page : 4 , hitCounter : 5 } ,
482
642
{ page : 5 , hitCounter : 6 } ,
0 commit comments