@@ -98,7 +98,7 @@ describe('useQueries', () => {
9898
9999 renderWithClient ( queryClient , < Page /> )
100100
101- await waitFor ( ( ) => expect ( states . length ) . toBe ( 7 ) )
101+ await waitFor ( ( ) => expect ( states . length ) . toBe ( 8 ) )
102102
103103 expect ( states [ 0 ] ) . toMatchObject ( [
104104 {
@@ -136,10 +136,14 @@ describe('useQueries', () => {
136136 { status : 'success' , data : 5 , isPreviousData : true , isFetching : true } ,
137137 ] )
138138 expect ( states [ 5 ] ) . toMatchObject ( [
139- { status : 'success' , data : 4 , isPreviousData : false , isFetching : false } ,
139+ { status : 'success' , data : 2 , isPreviousData : true , isFetching : true } ,
140140 { status : 'success' , data : 5 , isPreviousData : true , isFetching : true } ,
141141 ] )
142142 expect ( states [ 6 ] ) . toMatchObject ( [
143+ { status : 'success' , data : 4 , isPreviousData : false , isFetching : false } ,
144+ { status : 'success' , data : 5 , isPreviousData : true , isFetching : true } ,
145+ ] )
146+ expect ( states [ 7 ] ) . toMatchObject ( [
143147 { status : 'success' , data : 4 , isPreviousData : false , isFetching : false } ,
144148 { status : 'success' , data : 10 , isPreviousData : false , isFetching : false } ,
145149 ] )
@@ -175,7 +179,7 @@ describe('useQueries', () => {
175179
176180 renderWithClient ( queryClient , < Page /> )
177181
178- await waitFor ( ( ) => expect ( states . length ) . toBe ( 8 ) )
182+ await waitFor ( ( ) => expect ( states . length ) . toBe ( 10 ) )
179183
180184 expect ( states [ 0 ] ) . toMatchObject ( [
181185 {
@@ -226,7 +230,7 @@ describe('useQueries', () => {
226230 } ,
227231 ] )
228232 expect ( states [ 5 ] ) . toMatchObject ( [
229- { status : 'success' , data : 6 , isPreviousData : false , isFetching : false } ,
233+ { status : 'success' , data : 4 , isPreviousData : true , isFetching : true } ,
230234 { status : 'success' , data : 8 , isPreviousData : true , isFetching : true } ,
231235 {
232236 status : 'loading' ,
@@ -236,6 +240,26 @@ describe('useQueries', () => {
236240 } ,
237241 ] )
238242 expect ( states [ 6 ] ) . toMatchObject ( [
243+ { status : 'success' , data : 4 , isPreviousData : true , isFetching : true } ,
244+ { status : 'success' , data : 8 , isPreviousData : true , isFetching : true } ,
245+ {
246+ status : 'loading' ,
247+ data : undefined ,
248+ isPreviousData : false ,
249+ isFetching : true ,
250+ } ,
251+ ] )
252+ expect ( states [ 7 ] ) . toMatchObject ( [
253+ { status : 'success' , data : 6 , isPreviousData : false , isFetching : false } ,
254+ { status : 'success' , data : 8 , isPreviousData : true , isFetching : true } ,
255+ {
256+ status : 'loading' ,
257+ data : undefined ,
258+ isPreviousData : false ,
259+ isFetching : true ,
260+ } ,
261+ ] )
262+ expect ( states [ 8 ] ) . toMatchObject ( [
239263 { status : 'success' , data : 6 , isPreviousData : false , isFetching : false } ,
240264 { status : 'success' , data : 12 , isPreviousData : false , isFetching : false } ,
241265 {
@@ -245,13 +269,123 @@ describe('useQueries', () => {
245269 isFetching : true ,
246270 } ,
247271 ] )
248- expect ( states [ 7 ] ) . toMatchObject ( [
272+ expect ( states [ 9 ] ) . toMatchObject ( [
249273 { status : 'success' , data : 6 , isPreviousData : false , isFetching : false } ,
250274 { status : 'success' , data : 12 , isPreviousData : false , isFetching : false } ,
251275 { status : 'success' , data : 18 , isPreviousData : false , isFetching : false } ,
252276 ] )
253277 } )
254278
279+ it ( 'should keep previous data when switching between queries' , async ( ) => {
280+ const key = queryKey ( )
281+ const states : UseQueryResult [ ] [ ] = [ ]
282+
283+ function Page ( ) {
284+ const [ series1 , setSeries1 ] = React . useState ( 1 )
285+ const [ series2 , setSeries2 ] = React . useState ( 2 )
286+ const ids = [ series1 , series2 ]
287+
288+ const result = useQueries (
289+ ids . map ( id => {
290+ return {
291+ queryKey : [ key , id ] ,
292+ queryFn : async ( ) => {
293+ await sleep ( 5 )
294+ return id * 5
295+ } ,
296+ keepPreviousData : true ,
297+ }
298+ } )
299+ )
300+
301+ states . push ( result )
302+
303+ React . useEffect ( ( ) => {
304+ setActTimeout ( ( ) => {
305+ setSeries2 ( 3 )
306+ } , 20 )
307+ } , [ ] )
308+
309+ React . useEffect ( ( ) => {
310+ setActTimeout ( ( ) => {
311+ setSeries1 ( 2 )
312+ } , 50 )
313+ } , [ ] )
314+
315+ return null
316+ }
317+
318+ renderWithClient ( queryClient , < Page /> )
319+
320+ await waitFor ( ( ) => expect ( states . length ) . toBe ( 12 ) )
321+
322+ console . log ( 'states: ' + JSON . stringify ( states ) )
323+
324+ expect ( states [ 0 ] ) . toMatchObject ( [
325+ {
326+ status : 'loading' ,
327+ data : undefined ,
328+ isPreviousData : false ,
329+ isFetching : true ,
330+ } ,
331+ {
332+ status : 'loading' ,
333+ data : undefined ,
334+ isPreviousData : false ,
335+ isFetching : true ,
336+ } ,
337+ ] )
338+ expect ( states [ 1 ] ) . toMatchObject ( [
339+ { status : 'success' , data : 5 , isPreviousData : false , isFetching : false } ,
340+ {
341+ status : 'loading' ,
342+ data : undefined ,
343+ isPreviousData : false ,
344+ isFetching : true ,
345+ } ,
346+ ] )
347+ expect ( states [ 2 ] ) . toMatchObject ( [
348+ { status : 'success' , data : 5 , isPreviousData : false , isFetching : false } ,
349+ { status : 'success' , data : 10 , isPreviousData : false , isFetching : false } ,
350+ ] )
351+ expect ( states [ 3 ] ) . toMatchObject ( [
352+ { status : 'success' , data : 5 , isPreviousData : false , isFetching : false } ,
353+ { status : 'success' , data : 10 , isPreviousData : true , isFetching : true } ,
354+ ] )
355+ expect ( states [ 4 ] ) . toMatchObject ( [
356+ { status : 'success' , data : 5 , isPreviousData : false , isFetching : false } ,
357+ { status : 'success' , data : 10 , isPreviousData : true , isFetching : true } ,
358+ ] )
359+ expect ( states [ 5 ] ) . toMatchObject ( [
360+ { status : 'success' , data : 5 , isPreviousData : false , isFetching : false } ,
361+ { status : 'success' , data : 15 , isPreviousData : false , isFetching : false } ,
362+ ] )
363+ expect ( states [ 6 ] ) . toMatchObject ( [
364+ { status : 'success' , data : 10 , isPreviousData : false , isFetching : true } ,
365+ { status : 'success' , data : 15 , isPreviousData : false , isFetching : true } ,
366+ ] )
367+ expect ( states [ 7 ] ) . toMatchObject ( [
368+ { status : 'success' , data : 10 , isPreviousData : false , isFetching : true } ,
369+ { status : 'success' , data : 15 , isPreviousData : false , isFetching : true } ,
370+ ] )
371+ expect ( states [ 8 ] ) . toMatchObject ( [
372+ { status : 'success' , data : 10 , isPreviousData : false , isFetching : true } ,
373+ { status : 'success' , data : 15 , isPreviousData : false , isFetching : true } ,
374+ ] )
375+ expect ( states [ 9 ] ) . toMatchObject ( [
376+ { status : 'success' , data : 10 , isPreviousData : false , isFetching : true } ,
377+ { status : 'success' , data : 15 , isPreviousData : false , isFetching : true } ,
378+ ] )
379+ expect ( states [ 10 ] ) . toMatchObject ( [
380+ { status : 'success' , data : 10 , isPreviousData : false , isFetching : false } ,
381+ { status : 'success' , data : 15 , isPreviousData : false , isFetching : true } ,
382+ ] )
383+ expect ( states [ 11 ] ) . toMatchObject ( [
384+ { status : 'success' , data : 10 , isPreviousData : false , isFetching : false } ,
385+ { status : 'success' , data : 15 , isPreviousData : false , isFetching : false } ,
386+ ] )
387+ } )
388+
255389 it ( 'handles type parameter - tuple of tuples' , async ( ) => {
256390 const key1 = queryKey ( )
257391 const key2 = queryKey ( )
0 commit comments