@@ -3,7 +3,6 @@ import { fireEvent } from '@testing-library/preact'
33import type { VNode } from 'preact'
44import { Suspense } from 'preact/compat'
55import { afterEach , beforeEach , describe , expect , it , vi } from 'vitest'
6- import type { Mock } from 'vitest'
76
87import {
98 QueryCache ,
@@ -13,33 +12,6 @@ import {
1312} from '..'
1413import { renderWithClient } from './utils'
1514
16- const generateInfiniteQueryOptions = (
17- data : Array < { data : string ; currentPage : number ; totalPages : number } > ,
18- ) => {
19- let currentPage = 0
20-
21- return {
22- queryFn : vi
23- . fn < ( ...args : Array < any > ) => Promise < ( typeof data ) [ number ] > > ( )
24- . mockImplementation ( async ( ) => {
25- const currentPageData = data [ currentPage ]
26- if ( ! currentPageData ) {
27- throw new Error ( `No data defined for page ${ currentPage } ` )
28- }
29-
30- await sleep ( 10 )
31- currentPage ++
32-
33- return currentPageData
34- } ) ,
35- initialPageParam : 1 ,
36- getNextPageParam : ( lastPage : ( typeof data ) [ number ] ) =>
37- lastPage . currentPage === lastPage . totalPages
38- ? undefined
39- : lastPage . currentPage + 1 ,
40- }
41- }
42-
4315describe ( 'usePrefetchInfiniteQuery' , ( ) => {
4416 let queryCache : QueryCache
4517 let queryClient : QueryClient
@@ -60,18 +32,21 @@ describe('usePrefetchInfiniteQuery', () => {
6032
6133 it ( 'should prefetch an infinite query if query state does not exist' , async ( ) => {
6234 const data = [
63- { data : 'Do you fetch on render?' , currentPage : 1 , totalPages : 3 } ,
64- { data : 'Or do you render as you fetch?' , currentPage : 2 , totalPages : 3 } ,
65- {
66- data : 'Either way, Tanstack Query helps you!' ,
67- currentPage : 3 ,
68- totalPages : 3 ,
69- } ,
35+ 'Do you fetch on render?' ,
36+ 'Or do you render as you fetch?' ,
37+ 'Either way, Tanstack Query helps you!' ,
7038 ]
7139
7240 const queryOpts = {
7341 queryKey : queryKey ( ) ,
74- ...generateInfiniteQueryOptions ( data ) ,
42+ queryFn : vi
43+ . fn < ( context : { pageParam : number } ) => Promise < string > > ( )
44+ . mockImplementation ( ( { pageParam } ) =>
45+ sleep ( 10 ) . then ( ( ) => data [ pageParam ] ! ) ,
46+ ) ,
47+ initialPageParam : 0 ,
48+ getNextPageParam : ( _lastPage : string , allPages : Array < string > ) =>
49+ allPages . length < data . length ? allPages . length : undefined ,
7550 }
7651
7752 function Page ( ) {
@@ -80,7 +55,7 @@ describe('usePrefetchInfiniteQuery', () => {
8055 return (
8156 < div >
8257 { state . data . pages . map ( ( page , index ) => (
83- < div key = { index } > data: { page . data } </ div >
58+ < div key = { index } > data: { page } </ div >
8459 ) ) }
8560 < button onClick = { ( ) => state . fetchNextPage ( ) } > Next Page</ button >
8661 </ div >
@@ -114,26 +89,35 @@ describe('usePrefetchInfiniteQuery', () => {
11489 } )
11590
11691 it ( 'should not display fallback if the query cache is already populated' , async ( ) => {
92+ const data = [
93+ 'Prefetch rocks!' ,
94+ 'No waterfalls, boy!' ,
95+ 'Tanstack Query #ftw' ,
96+ ]
97+
11798 const queryOpts = {
11899 queryKey : queryKey ( ) ,
119- ...generateInfiniteQueryOptions ( [
120- { data : 'Prefetch rocks!' , currentPage : 1 , totalPages : 3 } ,
121- { data : 'No waterfalls, boy!' , currentPage : 2 , totalPages : 3 } ,
122- { data : 'Tanstack Query #ftw' , currentPage : 3 , totalPages : 3 } ,
123- ] ) ,
100+ queryFn : vi
101+ . fn < ( context : { pageParam : number } ) => Promise < string > > ( )
102+ . mockImplementation ( ( { pageParam } ) =>
103+ sleep ( 10 ) . then ( ( ) => data [ pageParam ] ! ) ,
104+ ) ,
105+ initialPageParam : 0 ,
106+ getNextPageParam : ( _lastPage : string , allPages : Array < string > ) =>
107+ allPages . length < data . length ? allPages . length : undefined ,
124108 }
125109
126110 queryClient . prefetchInfiniteQuery ( { ...queryOpts , pages : 3 } )
127111 await vi . advanceTimersByTimeAsync ( 30 )
128- ; ( queryOpts . queryFn as Mock ) . mockClear ( )
112+ queryOpts . queryFn . mockClear ( )
129113
130114 function Page ( ) {
131115 const state = useSuspenseInfiniteQuery ( queryOpts )
132116
133117 return (
134118 < div >
135119 { state . data . pages . map ( ( page , index ) => (
136- < div key = { index } > data: { page . data } </ div >
120+ < div key = { index } > data: { page } </ div >
137121 ) ) }
138122 < button onClick = { ( ) => state . fetchNextPage ( ) } > Next Page</ button >
139123 </ div >
@@ -162,13 +146,20 @@ describe('usePrefetchInfiniteQuery', () => {
162146 } )
163147
164148 it ( 'should not create an endless loop when using inside a suspense boundary' , async ( ) => {
149+ const data = [ 'Infinite Page 1' , 'Infinite Page 2' , 'Infinite Page 3' ]
150+
165151 const queryOpts = {
166152 queryKey : queryKey ( ) ,
167- ...generateInfiniteQueryOptions ( [
168- { data : 'Infinite Page 1' , currentPage : 1 , totalPages : 3 } ,
169- { data : 'Infinite Page 2' , currentPage : 1 , totalPages : 3 } ,
170- { data : 'Infinite Page 3' , currentPage : 1 , totalPages : 3 } ,
171- ] ) ,
153+ queryFn : vi
154+ . fn < ( context : { pageParam : number } ) => Promise < string > > ( )
155+ . mockImplementation ( ( { pageParam } ) =>
156+ sleep ( 10 ) . then ( ( ) => data [ pageParam ] ! ) ,
157+ ) ,
158+ initialPageParam : 0 ,
159+ // always reports another page available, to guard against an endless
160+ // auto-advance loop rather than a bounded pagination sequence
161+ getNextPageParam : ( _lastPage : string , allPages : Array < string > ) =>
162+ allPages . length ,
172163 }
173164
174165 function Prefetch ( { children } : { children : VNode } ) {
@@ -182,7 +173,7 @@ describe('usePrefetchInfiniteQuery', () => {
182173 return (
183174 < div >
184175 { state . data . pages . map ( ( page , index ) => (
185- < div key = { index } > data: { page . data } </ div >
176+ < div key = { index } > data: { page } </ div >
186177 ) ) }
187178 < button onClick = { ( ) => state . fetchNextPage ( ) } > Next Page</ button >
188179 </ div >
0 commit comments