@@ -14,6 +14,7 @@ import {
1414 QueryStatus ,
1515 createApi ,
1616 fetchBaseQuery ,
17+ fakeBaseQuery ,
1718 skipToken ,
1819} from '@reduxjs/toolkit/query/react'
1920import {
@@ -46,8 +47,6 @@ describe('Infinite queries', () => {
4647 const pokemonApi = createApi ( {
4748 baseQuery : fetchBaseQuery ( { baseUrl : 'https://pokeapi.co/api/v2/' } ) ,
4849 endpoints : ( builder ) => ( {
49- // GOAL: Specify both the query arg (for cache key serialization)
50- // and the page param type (for feeding into the query URL)
5150 getInfinitePokemon : builder . infiniteQuery < Pokemon [ ] , string , number > ( {
5251 infiniteQueryOptions : {
5352 initialPageParam : 0 ,
@@ -67,23 +66,67 @@ describe('Infinite queries', () => {
6766 return firstPageParam > 0 ? firstPageParam - 1 : undefined
6867 } ,
6968 } ,
70-
71- // Actual query arg type should be `number`
7269 query ( pageParam ) {
7370 return `https://example.com/listItems?page=${ pageParam } `
7471 } ,
7572 } ) ,
73+ getInfinitePokemonWithMax : builder . infiniteQuery <
74+ Pokemon [ ] ,
75+ string ,
76+ number
77+ > ( {
78+ infiniteQueryOptions : {
79+ initialPageParam : 0 ,
80+ maxPages : 3 ,
81+ getNextPageParam : (
82+ lastPage ,
83+ allPages ,
84+ lastPageParam ,
85+ allPageParams ,
86+ ) => lastPageParam + 1 ,
87+ getPreviousPageParam : (
88+ firstPage ,
89+ allPages ,
90+ firstPageParam ,
91+ allPageParams ,
92+ ) => {
93+ return firstPageParam > 0 ? firstPageParam - 1 : undefined
94+ } ,
95+ } ,
96+ query ( pageParam ) {
97+ return `https://example.com/listItems?page=${ pageParam } `
98+ } ,
99+ } ) ,
100+ counter : builder . query < number , string > ( {
101+ queryFn : async ( ) => {
102+ return { data : 0 }
103+ } ,
104+ } ) ,
76105 } ) ,
77106 } )
78107
79- let storeRef = setupApiStore ( pokemonApi , undefined , {
80- withoutTestLifecycles : true ,
81- } )
108+ let storeRef = setupApiStore (
109+ pokemonApi ,
110+ { ...actionsReducer } ,
111+ {
112+ withoutTestLifecycles : true ,
113+ } ,
114+ )
82115
83116 beforeEach ( ( ) => {
84- storeRef = setupApiStore ( pokemonApi , undefined , {
85- withoutTestLifecycles : true ,
86- } )
117+ storeRef = setupApiStore (
118+ pokemonApi ,
119+ { ...actionsReducer } ,
120+ {
121+ withoutTestLifecycles : true ,
122+ } ,
123+ )
124+
125+ process . env . NODE_ENV = 'development'
126+ } )
127+
128+ afterEach ( ( ) => {
129+ process . env . NODE_ENV = 'test'
87130 } )
88131
89132 test ( 'Basic infinite query behavior' , async ( ) => {
@@ -110,7 +153,6 @@ describe('Infinite queries', () => {
110153 )
111154
112155 expect ( entry1SecondPage . status ) . toBe ( QueryStatus . fulfilled )
113- // console.log('Value: ', util.inspect(entry1SecondPage, { depth: Infinity }))
114156 if ( entry1SecondPage . status === QueryStatus . fulfilled ) {
115157 expect ( entry1SecondPage . data . pages ) . toEqual ( [
116158 // two pages, one entry each
@@ -126,19 +168,13 @@ describe('Infinite queries', () => {
126168 )
127169
128170 if ( entry1PrevPageMissing . status === QueryStatus . fulfilled ) {
129- // There is no p
130171 expect ( entry1PrevPageMissing . data . pages ) . toEqual ( [
131172 // two pages, one entry each
132173 [ { id : '0' , name : 'Pokemon 0' } ] ,
133174 [ { id : '1' , name : 'Pokemon 1' } ] ,
134175 ] )
135176 }
136177
137- // console.log(
138- // 'API state: ',
139- // util.inspect(storeRef.store.getState().api, { depth: Infinity }),
140- // )
141-
142178 const entry2InitialLoad = await storeRef . store . dispatch (
143179 pokemonApi . endpoints . getInfinitePokemon . initiate ( 'water' , {
144180 initialPageParam : 3 ,
@@ -181,4 +217,81 @@ describe('Infinite queries', () => {
181217 ] )
182218 }
183219 } )
220+
221+ test ( 'does not have a page limit without maxPages' , async ( ) => {
222+ for ( let i = 1 ; i <= 10 ; i ++ ) {
223+ const res = await storeRef . store . dispatch (
224+ pokemonApi . endpoints . getInfinitePokemon . initiate ( 'fire' , {
225+ direction : 'forward' ,
226+ } ) ,
227+ )
228+
229+ if ( res . status === QueryStatus . fulfilled ) {
230+ expect ( res . data . pages ) . toHaveLength ( i )
231+ }
232+ }
233+ } )
234+
235+ test ( 'applies a page limit with maxPages' , async ( ) => {
236+ for ( let i = 1 ; i <= 10 ; i ++ ) {
237+ const res = await storeRef . store . dispatch (
238+ pokemonApi . endpoints . getInfinitePokemonWithMax . initiate ( 'fire' , {
239+ direction : 'forward' ,
240+ } ) ,
241+ )
242+ if ( res . status === QueryStatus . fulfilled ) {
243+ // Should have 1, 2, 3 (repeating) pages
244+ expect ( res . data . pages ) . toHaveLength ( Math . min ( i , 3 ) )
245+ }
246+ }
247+
248+ // Should now have entries 7, 8, 9 after the loop
249+
250+ const res = await storeRef . store . dispatch (
251+ pokemonApi . endpoints . getInfinitePokemonWithMax . initiate ( 'fire' , {
252+ direction : 'backward' ,
253+ } ) ,
254+ )
255+
256+ if ( res . status === QueryStatus . fulfilled ) {
257+ // When we go back 1, we now have 6, 7, 8
258+ expect ( res . data . pages ) . toEqual ( [
259+ [ { id : '6' , name : 'Pokemon 6' } ] ,
260+ [ { id : '7' , name : 'Pokemon 7' } ] ,
261+ [ { id : '8' , name : 'Pokemon 8' } ] ,
262+ ] )
263+ }
264+ } )
265+
266+ test ( 'validates maxPages during createApi call' , async ( ) => {
267+ const createApiWithMaxPages = (
268+ maxPages : number ,
269+ getPreviousPageParam : ( ( ) => number ) | undefined ,
270+ ) => {
271+ createApi ( {
272+ baseQuery : fakeBaseQuery ( ) ,
273+ endpoints : ( build ) => ( {
274+ getInfinitePokemon : build . infiniteQuery < Pokemon [ ] , string , number > ( {
275+ query ( pageParam ) {
276+ return `https://example.com/listItems?page=${ pageParam } `
277+ } ,
278+ infiniteQueryOptions : {
279+ initialPageParam : 0 ,
280+ maxPages,
281+ getNextPageParam : ( ) => 1 ,
282+ getPreviousPageParam,
283+ } ,
284+ } ) ,
285+ } ) ,
286+ } )
287+ }
288+
289+ expect ( ( ) => createApiWithMaxPages ( 0 , ( ) => 0 ) ) . toThrowError (
290+ `maxPages for endpoint 'getInfinitePokemon' must be a number greater than 0` ,
291+ )
292+
293+ expect ( ( ) => createApiWithMaxPages ( 1 , undefined ) ) . toThrowError (
294+ `getPreviousPageParam for endpoint 'getInfinitePokemon' must be a function if maxPages is used` ,
295+ )
296+ } )
184297} )
0 commit comments