@@ -128,4 +128,111 @@ describe('useQueries', () => {
128128 { status : 'success' , data : 10 , isPreviousData : false , isFetching : false } ,
129129 ] )
130130 } )
131+
132+ it ( 'should keep previous data for variable amounts of useQueries' , async ( ) => {
133+ const key = queryKey ( )
134+ const states : UseQueryResult [ ] [ ] = [ ]
135+
136+ function Page ( ) {
137+ const [ count , setCount ] = React . useState ( 2 )
138+ const result = useQueries (
139+ Array . from ( { length : count } , ( _ , i ) => ( {
140+ queryKey : [ key , count , i + 1 ] ,
141+ keepPreviousData : true ,
142+ queryFn : async ( ) => {
143+ await sleep ( 5 )
144+ return ( i + 1 ) * count * 2
145+ } ,
146+ } ) )
147+ )
148+
149+ states . push ( result )
150+
151+ React . useEffect ( ( ) => {
152+ setActTimeout ( ( ) => {
153+ setCount ( prev => prev + 1 )
154+ } , 20 )
155+ } , [ ] )
156+
157+ return null
158+ }
159+
160+ renderWithClient ( queryClient , < Page /> )
161+
162+ await waitFor ( ( ) => expect ( states . length ) . toBe ( 8 ) )
163+
164+ expect ( states [ 0 ] ) . toMatchObject ( [
165+ {
166+ status : 'loading' ,
167+ data : undefined ,
168+ isPreviousData : false ,
169+ isFetching : true ,
170+ } ,
171+ {
172+ status : 'loading' ,
173+ data : undefined ,
174+ isPreviousData : false ,
175+ isFetching : true ,
176+ } ,
177+ ] )
178+ expect ( states [ 1 ] ) . toMatchObject ( [
179+ { status : 'success' , data : 4 , isPreviousData : false , isFetching : false } ,
180+ {
181+ status : 'loading' ,
182+ data : undefined ,
183+ isPreviousData : false ,
184+ isFetching : true ,
185+ } ,
186+ ] )
187+ expect ( states [ 2 ] ) . toMatchObject ( [
188+ { status : 'success' , data : 4 , isPreviousData : false , isFetching : false } ,
189+ { status : 'success' , data : 8 , isPreviousData : false , isFetching : false } ,
190+ ] )
191+
192+ expect ( states [ 3 ] ) . toMatchObject ( [
193+ { status : 'success' , data : 4 , isPreviousData : true , isFetching : true } ,
194+ { status : 'success' , data : 8 , isPreviousData : true , isFetching : true } ,
195+ {
196+ status : 'loading' ,
197+ data : undefined ,
198+ isPreviousData : false ,
199+ isFetching : true ,
200+ } ,
201+ ] )
202+ expect ( states [ 4 ] ) . toMatchObject ( [
203+ { status : 'success' , data : 4 , isPreviousData : true , isFetching : true } ,
204+ { status : 'success' , data : 8 , isPreviousData : true , isFetching : true } ,
205+ {
206+ status : 'loading' ,
207+ data : undefined ,
208+ isPreviousData : false ,
209+ isFetching : true ,
210+ } ,
211+ ] )
212+ expect ( states [ 5 ] ) . toMatchObject ( [
213+ { status : 'success' , data : 6 , isPreviousData : false , isFetching : false } ,
214+ { status : 'success' , data : 8 , isPreviousData : true , isFetching : true } ,
215+ {
216+ status : 'loading' ,
217+ data : undefined ,
218+ isPreviousData : false ,
219+ isFetching : true ,
220+ } ,
221+ ] )
222+ expect ( states [ 6 ] ) . toMatchObject ( [
223+ { status : 'success' , data : 6 , isPreviousData : false , isFetching : false } ,
224+ { status : 'success' , data : 12 , isPreviousData : false , isFetching : false } ,
225+ {
226+ status : 'loading' ,
227+ data : undefined ,
228+ isPreviousData : false ,
229+ isFetching : true ,
230+ } ,
231+ ] )
232+ expect ( states [ 7 ] ) . toMatchObject ( [
233+ { status : 'success' , data : 6 , isPreviousData : false , isFetching : false } ,
234+ { status : 'success' , data : 12 , isPreviousData : false , isFetching : false } ,
235+ { status : 'success' , data : 18 , isPreviousData : false , isFetching : false } ,
236+ ] )
237+ } )
131238} )
0 commit comments