@@ -201,6 +201,110 @@ describe('InfiniteQueryBehavior', () => {
201
201
unsubscribe ( )
202
202
} )
203
203
204
+ test ( 'InfiniteQueryBehavior should apply pageParam' , async ( ) => {
205
+ const key = queryKey ( )
206
+
207
+ const queryFn = vi . fn ( ) . mockImplementation ( ( { pageParam } ) => {
208
+ return pageParam
209
+ } )
210
+
211
+ const observer = new InfiniteQueryObserver < number > ( queryClient , {
212
+ queryKey : key ,
213
+ queryFn,
214
+ initialPageParam : 0 ,
215
+ } )
216
+
217
+ let observerResult :
218
+ | InfiniteQueryObserverResult < unknown , unknown >
219
+ | undefined
220
+
221
+ const unsubscribe = observer . subscribe ( ( result ) => {
222
+ observerResult = result
223
+ } )
224
+
225
+ // Wait for the first page to be fetched
226
+ await waitFor ( ( ) =>
227
+ expect ( observerResult ) . toMatchObject ( {
228
+ isFetching : false ,
229
+ data : { pages : [ 0 ] , pageParams : [ 0 ] } ,
230
+ } ) ,
231
+ )
232
+
233
+ queryFn . mockClear ( )
234
+
235
+ // Fetch the next page using pageParam
236
+ await observer . fetchNextPage ( { pageParam : 1 } )
237
+
238
+ expect ( queryFn ) . toHaveBeenNthCalledWith ( 1 , {
239
+ queryKey : key ,
240
+ pageParam : 1 ,
241
+ meta : undefined ,
242
+ client : queryClient ,
243
+ direction : 'forward' ,
244
+ signal : expect . anything ( ) ,
245
+ } )
246
+
247
+ expect ( observerResult ) . toMatchObject ( {
248
+ isFetching : false ,
249
+ data : { pages : [ 0 , 1 ] , pageParams : [ 0 , 1 ] } ,
250
+ } )
251
+
252
+ queryFn . mockClear ( )
253
+
254
+ // Fetch the previous page using pageParam
255
+ await observer . fetchPreviousPage ( { pageParam : - 1 } )
256
+
257
+ expect ( queryFn ) . toHaveBeenNthCalledWith ( 1 , {
258
+ queryKey : key ,
259
+ pageParam : - 1 ,
260
+ meta : undefined ,
261
+ client : queryClient ,
262
+ direction : 'backward' ,
263
+ signal : expect . anything ( ) ,
264
+ } )
265
+
266
+ expect ( observerResult ) . toMatchObject ( {
267
+ isFetching : false ,
268
+ data : { pages : [ - 1 , 0 , 1 ] , pageParams : [ - 1 , 0 , 1 ] } ,
269
+ } )
270
+
271
+ queryFn . mockClear ( )
272
+
273
+ // Refetch pages: old manual page params should be used
274
+ await observer . refetch ( )
275
+
276
+ expect ( queryFn ) . toHaveBeenCalledTimes ( 3 )
277
+
278
+ expect ( queryFn ) . toHaveBeenNthCalledWith ( 1 , {
279
+ queryKey : key ,
280
+ pageParam : - 1 ,
281
+ meta : undefined ,
282
+ client : queryClient ,
283
+ direction : 'forward' ,
284
+ signal : expect . anything ( ) ,
285
+ } )
286
+
287
+ expect ( queryFn ) . toHaveBeenNthCalledWith ( 2 , {
288
+ queryKey : key ,
289
+ pageParam : 0 ,
290
+ meta : undefined ,
291
+ client : queryClient ,
292
+ direction : 'forward' ,
293
+ signal : expect . anything ( ) ,
294
+ } )
295
+
296
+ expect ( queryFn ) . toHaveBeenNthCalledWith ( 3 , {
297
+ queryKey : key ,
298
+ pageParam : 1 ,
299
+ meta : undefined ,
300
+ client : queryClient ,
301
+ direction : 'forward' ,
302
+ signal : expect . anything ( ) ,
303
+ } )
304
+
305
+ unsubscribe ( )
306
+ } )
307
+
204
308
test ( 'InfiniteQueryBehavior should support query cancellation' , async ( ) => {
205
309
const key = queryKey ( )
206
310
let abortSignal : AbortSignal | null = null
0 commit comments