@@ -101,6 +101,40 @@ describe('Infinite queries', () => {
101101 } ) ,
102102 } )
103103
104+ let hitCounter = 0
105+
106+ type HitCounter = { page : number ; hitCounter : number }
107+
108+ const countersApi = createApi ( {
109+ baseQuery : fakeBaseQuery ( ) ,
110+ tagTypes : [ 'Counter' ] ,
111+ endpoints : ( build ) => ( {
112+ counters : build . infiniteQuery < HitCounter , string , number > ( {
113+ queryFn ( page ) {
114+ hitCounter ++
115+
116+ return { data : { page, hitCounter } }
117+ } ,
118+ infiniteQueryOptions : {
119+ initialPageParam : 0 ,
120+ getNextPageParam : (
121+ lastPage ,
122+ allPages ,
123+ lastPageParam ,
124+ allPageParams ,
125+ ) => lastPageParam + 1 ,
126+ } ,
127+ providesTags : [ 'Counter' ] ,
128+ } ) ,
129+ mutation : build . mutation < null , void > ( {
130+ queryFn : async ( ) => {
131+ return { data : null }
132+ } ,
133+ invalidatesTags : [ 'Counter' ] ,
134+ } ) ,
135+ } ) ,
136+ } )
137+
104138 let storeRef = setupApiStore (
105139 pokemonApi ,
106140 { ...actionsReducer } ,
@@ -133,6 +167,8 @@ describe('Infinite queries', () => {
133167
134168 counters = { }
135169
170+ hitCounter = 0
171+
136172 process . env . NODE_ENV = 'development'
137173 } )
138174
@@ -404,32 +440,60 @@ describe('Infinite queries', () => {
404440 } )
405441
406442 test ( 'refetches all existing pages' , async ( ) => {
407- let hitCounter = 0
443+ const checkResultData = (
444+ result : InfiniteQueryResult ,
445+ expectedValues : HitCounter [ ] ,
446+ ) => {
447+ expect ( result . status ) . toBe ( QueryStatus . fulfilled )
448+ if ( result . status === QueryStatus . fulfilled ) {
449+ expect ( result . data . pages ) . toEqual ( expectedValues )
450+ }
451+ }
408452
409- type HitCounter = { page : number ; hitCounter : number }
453+ const storeRef = setupApiStore (
454+ countersApi ,
455+ { ...actionsReducer } ,
456+ {
457+ withoutTestLifecycles : true ,
458+ } ,
459+ )
410460
411- const countersApi = createApi ( {
412- baseQuery : fakeBaseQuery ( ) ,
413- endpoints : ( build ) => ( {
414- counters : build . infiniteQuery < HitCounter , string , number > ( {
415- queryFn ( page ) {
416- hitCounter ++
461+ await storeRef . store . dispatch (
462+ countersApi . endpoints . counters . initiate ( 'item' , {
463+ initialPageParam : 3 ,
464+ } ) ,
465+ )
417466
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- } ) ,
467+ await storeRef . store . dispatch (
468+ countersApi . endpoints . counters . initiate ( 'item' , {
469+ direction : 'forward' ,
430470 } ) ,
431- } )
471+ )
432472
473+ const thirdPromise = storeRef . store . dispatch (
474+ countersApi . endpoints . counters . initiate ( 'item' , {
475+ direction : 'forward' ,
476+ } ) ,
477+ )
478+
479+ const thirdRes = await thirdPromise
480+
481+ checkResultData ( thirdRes , [
482+ { page : 3 , hitCounter : 1 } ,
483+ { page : 4 , hitCounter : 2 } ,
484+ { page : 5 , hitCounter : 3 } ,
485+ ] )
486+
487+ const fourthRes = await thirdPromise . refetch ( )
488+
489+ checkResultData ( fourthRes , [
490+ { page : 3 , hitCounter : 4 } ,
491+ { page : 4 , hitCounter : 5 } ,
492+ { page : 5 , hitCounter : 6 } ,
493+ ] )
494+ } )
495+
496+ test ( 'Refetches on invalidation' , async ( ) => {
433497 const checkResultData = (
434498 result : InfiniteQueryResult ,
435499 expectedValues : HitCounter [ ] ,
@@ -474,9 +538,28 @@ describe('Infinite queries', () => {
474538 { page : 5 , hitCounter : 3 } ,
475539 ] )
476540
477- const fourthRes = await thirdPromise . refetch ( )
541+ await storeRef . store . dispatch ( countersApi . endpoints . mutation . initiate ( ) )
478542
479- checkResultData ( fourthRes , [
543+ let entry = countersApi . endpoints . counters . select ( 'item' ) (
544+ storeRef . store . getState ( ) ,
545+ )
546+ const promise = storeRef . store . dispatch (
547+ countersApi . util . getRunningQueryThunk ( 'counters' , 'item' ) ,
548+ )
549+ const promises = storeRef . store . dispatch (
550+ countersApi . util . getRunningQueriesThunk ( ) ,
551+ )
552+ expect ( entry ) . toMatchObject ( {
553+ status : 'pending' ,
554+ } )
555+
556+ expect ( promise ) . toBeInstanceOf ( Promise )
557+
558+ expect ( promises ) . toEqual ( [ promise ] )
559+
560+ const finalRes = await promise
561+
562+ checkResultData ( finalRes as any , [
480563 { page : 3 , hitCounter : 4 } ,
481564 { page : 4 , hitCounter : 5 } ,
482565 { page : 5 , hitCounter : 6 } ,
0 commit comments