@@ -8,9 +8,12 @@ import {
88 waitFor ,
99} from '@testing-library/react'
1010import userEvent from '@testing-library/user-event'
11- import { HttpResponse , http } from 'msw'
11+ import { HttpResponse , delay , http } from 'msw'
1212import 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'
1417import {
1518 QueryStatus ,
1619 createApi ,
@@ -566,6 +569,80 @@ describe('Infinite queries', () => {
566569 ] )
567570 } )
568571
572+ test ( 'Refetches on polling' , async ( ) => {
573+ const checkResultData = (
574+ result : InfiniteQueryResult ,
575+ expectedValues : HitCounter [ ] ,
576+ ) => {
577+ expect ( result . status ) . toBe ( QueryStatus . fulfilled )
578+ if ( result . status === QueryStatus . fulfilled ) {
579+ expect ( result . data . pages ) . toEqual ( expectedValues )
580+ }
581+ }
582+
583+ const storeRef = setupApiStore (
584+ countersApi ,
585+ { ...actionsReducer } ,
586+ {
587+ withoutTestLifecycles : true ,
588+ } ,
589+ )
590+
591+ await storeRef . store . dispatch (
592+ countersApi . endpoints . counters . initiate ( 'item' , {
593+ initialPageParam : 3 ,
594+ } ) ,
595+ )
596+
597+ await storeRef . store . dispatch (
598+ countersApi . endpoints . counters . initiate ( 'item' , {
599+ direction : 'forward' ,
600+ } ) ,
601+ )
602+
603+ const thirdPromise = storeRef . store . dispatch (
604+ countersApi . endpoints . counters . initiate ( 'item' , {
605+ direction : 'forward' ,
606+ } ) ,
607+ )
608+
609+ const thirdRes = await thirdPromise
610+
611+ checkResultData ( thirdRes , [
612+ { page : 3 , hitCounter : 1 } ,
613+ { page : 4 , hitCounter : 2 } ,
614+ { page : 5 , hitCounter : 3 } ,
615+ ] )
616+
617+ thirdPromise . updateSubscriptionOptions ( {
618+ pollingInterval : 10 ,
619+ } )
620+
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 , [
640+ { page : 3 , hitCounter : 4 } ,
641+ { page : 4 , hitCounter : 5 } ,
642+ { page : 5 , hitCounter : 6 } ,
643+ ] )
644+ } )
645+
569646 test ( 'can fetch pages with refetchOnMountOrArgChange active' , async ( ) => {
570647 const pokemonApiWithRefetch = createApi ( {
571648 baseQuery : fetchBaseQuery ( { baseUrl : 'https://pokeapi.co/api/v2/' } ) ,
0 commit comments