@@ -98,7 +98,7 @@ describe('useQueries', () => {
98
98
99
99
renderWithClient ( queryClient , < Page /> )
100
100
101
- await waitFor ( ( ) => expect ( states . length ) . toBe ( 7 ) )
101
+ await waitFor ( ( ) => expect ( states . length ) . toBe ( 8 ) )
102
102
103
103
expect ( states [ 0 ] ) . toMatchObject ( [
104
104
{
@@ -136,10 +136,14 @@ describe('useQueries', () => {
136
136
{ status : 'success' , data : 5 , isPreviousData : true , isFetching : true } ,
137
137
] )
138
138
expect ( states [ 5 ] ) . toMatchObject ( [
139
- { status : 'success' , data : 4 , isPreviousData : false , isFetching : false } ,
139
+ { status : 'success' , data : 2 , isPreviousData : true , isFetching : true } ,
140
140
{ status : 'success' , data : 5 , isPreviousData : true , isFetching : true } ,
141
141
] )
142
142
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 ( [
143
147
{ status : 'success' , data : 4 , isPreviousData : false , isFetching : false } ,
144
148
{ status : 'success' , data : 10 , isPreviousData : false , isFetching : false } ,
145
149
] )
@@ -175,7 +179,7 @@ describe('useQueries', () => {
175
179
176
180
renderWithClient ( queryClient , < Page /> )
177
181
178
- await waitFor ( ( ) => expect ( states . length ) . toBe ( 8 ) )
182
+ await waitFor ( ( ) => expect ( states . length ) . toBe ( 10 ) )
179
183
180
184
expect ( states [ 0 ] ) . toMatchObject ( [
181
185
{
@@ -226,7 +230,7 @@ describe('useQueries', () => {
226
230
} ,
227
231
] )
228
232
expect ( states [ 5 ] ) . toMatchObject ( [
229
- { status : 'success' , data : 6 , isPreviousData : false , isFetching : false } ,
233
+ { status : 'success' , data : 4 , isPreviousData : true , isFetching : true } ,
230
234
{ status : 'success' , data : 8 , isPreviousData : true , isFetching : true } ,
231
235
{
232
236
status : 'loading' ,
@@ -236,6 +240,26 @@ describe('useQueries', () => {
236
240
} ,
237
241
] )
238
242
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 ( [
239
263
{ status : 'success' , data : 6 , isPreviousData : false , isFetching : false } ,
240
264
{ status : 'success' , data : 12 , isPreviousData : false , isFetching : false } ,
241
265
{
@@ -245,13 +269,121 @@ describe('useQueries', () => {
245
269
isFetching : true ,
246
270
} ,
247
271
] )
248
- expect ( states [ 7 ] ) . toMatchObject ( [
272
+ expect ( states [ 9 ] ) . toMatchObject ( [
249
273
{ status : 'success' , data : 6 , isPreviousData : false , isFetching : false } ,
250
274
{ status : 'success' , data : 12 , isPreviousData : false , isFetching : false } ,
251
275
{ status : 'success' , data : 18 , isPreviousData : false , isFetching : false } ,
252
276
] )
253
277
} )
254
278
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
+ expect ( states [ 0 ] ) . toMatchObject ( [
323
+ {
324
+ status : 'loading' ,
325
+ data : undefined ,
326
+ isPreviousData : false ,
327
+ isFetching : true ,
328
+ } ,
329
+ {
330
+ status : 'loading' ,
331
+ data : undefined ,
332
+ isPreviousData : false ,
333
+ isFetching : true ,
334
+ } ,
335
+ ] )
336
+ expect ( states [ 1 ] ) . toMatchObject ( [
337
+ { status : 'success' , data : 5 , isPreviousData : false , isFetching : false } ,
338
+ {
339
+ status : 'loading' ,
340
+ data : undefined ,
341
+ isPreviousData : false ,
342
+ isFetching : true ,
343
+ } ,
344
+ ] )
345
+ expect ( states [ 2 ] ) . toMatchObject ( [
346
+ { status : 'success' , data : 5 , isPreviousData : false , isFetching : false } ,
347
+ { status : 'success' , data : 10 , isPreviousData : false , isFetching : false } ,
348
+ ] )
349
+ expect ( states [ 3 ] ) . toMatchObject ( [
350
+ { status : 'success' , data : 5 , isPreviousData : false , isFetching : false } ,
351
+ { status : 'success' , data : 10 , isPreviousData : true , isFetching : true } ,
352
+ ] )
353
+ expect ( states [ 4 ] ) . toMatchObject ( [
354
+ { status : 'success' , data : 5 , isPreviousData : false , isFetching : false } ,
355
+ { status : 'success' , data : 10 , isPreviousData : true , isFetching : true } ,
356
+ ] )
357
+ expect ( states [ 5 ] ) . toMatchObject ( [
358
+ { status : 'success' , data : 5 , isPreviousData : false , isFetching : false } ,
359
+ { status : 'success' , data : 15 , isPreviousData : false , isFetching : false } ,
360
+ ] )
361
+ expect ( states [ 6 ] ) . toMatchObject ( [
362
+ { status : 'success' , data : 10 , isPreviousData : false , isFetching : true } ,
363
+ { status : 'success' , data : 15 , isPreviousData : false , isFetching : true } ,
364
+ ] )
365
+ expect ( states [ 7 ] ) . toMatchObject ( [
366
+ { status : 'success' , data : 10 , isPreviousData : false , isFetching : true } ,
367
+ { status : 'success' , data : 15 , isPreviousData : false , isFetching : true } ,
368
+ ] )
369
+ expect ( states [ 8 ] ) . toMatchObject ( [
370
+ { status : 'success' , data : 10 , isPreviousData : false , isFetching : true } ,
371
+ { status : 'success' , data : 15 , isPreviousData : false , isFetching : true } ,
372
+ ] )
373
+ expect ( states [ 9 ] ) . toMatchObject ( [
374
+ { status : 'success' , data : 10 , isPreviousData : false , isFetching : true } ,
375
+ { status : 'success' , data : 15 , isPreviousData : false , isFetching : true } ,
376
+ ] )
377
+ expect ( states [ 10 ] ) . toMatchObject ( [
378
+ { status : 'success' , data : 10 , isPreviousData : false , isFetching : false } ,
379
+ { status : 'success' , data : 15 , isPreviousData : false , isFetching : true } ,
380
+ ] )
381
+ expect ( states [ 11 ] ) . toMatchObject ( [
382
+ { status : 'success' , data : 10 , isPreviousData : false , isFetching : false } ,
383
+ { status : 'success' , data : 15 , isPreviousData : false , isFetching : false } ,
384
+ ] )
385
+ } )
386
+
255
387
it ( 'handles type parameter - tuple of tuples' , async ( ) => {
256
388
const key1 = queryKey ( )
257
389
const key2 = queryKey ( )
0 commit comments