@@ -73,331 +73,6 @@ describe('useQueries', () => {
73
73
expect ( results [ 2 ] ) . toMatchObject ( [ { data : 1 } , { data : 2 } ] )
74
74
} )
75
75
76
- it ( 'should keep previous data if amount of queries is the same' , async ( ) => {
77
- const key1 = queryKey ( )
78
- const key2 = queryKey ( )
79
- const states : UseQueryResult [ ] [ ] = [ ]
80
-
81
- function Page ( ) {
82
- const [ count , setCount ] = React . useState ( 1 )
83
- const result = useQueries ( {
84
- queries : [
85
- {
86
- queryKey : [ key1 , count ] ,
87
- placeholderData : ( previousData ?: number ) => previousData ,
88
- queryFn : async ( ) => {
89
- await sleep ( 10 )
90
- return count * 2
91
- } ,
92
- } ,
93
- {
94
- queryKey : [ key2 , count ] ,
95
- placeholderData : ( previousData ?: number ) => previousData ,
96
- queryFn : async ( ) => {
97
- await sleep ( 35 )
98
- return count * 5
99
- } ,
100
- } ,
101
- ] ,
102
- } )
103
- states . push ( result )
104
-
105
- const isFetching = result . some ( ( r ) => r . isFetching )
106
-
107
- return (
108
- < div >
109
- < div >
110
- data1: { String ( result [ 0 ] . data ?? 'null' ) } , data2:{ ' ' }
111
- { String ( result [ 1 ] . data ?? 'null' ) }
112
- </ div >
113
- < div > isFetching: { String ( isFetching ) } </ div >
114
- < button onClick = { ( ) => setCount ( ( prev ) => prev + 1 ) } > inc</ button >
115
- </ div >
116
- )
117
- }
118
-
119
- const rendered = renderWithClient ( queryClient , < Page /> )
120
-
121
- await waitFor ( ( ) => rendered . getByText ( 'data1: 2, data2: 5' ) )
122
- fireEvent . click ( rendered . getByRole ( 'button' , { name : / i n c / i } ) )
123
-
124
- await waitFor ( ( ) => rendered . getByText ( 'data1: 4, data2: 10' ) )
125
- await waitFor ( ( ) => rendered . getByText ( 'isFetching: false' ) )
126
-
127
- expect ( states [ states . length - 1 ] ) . toMatchObject ( [
128
- {
129
- status : 'success' ,
130
- data : 4 ,
131
- isPlaceholderData : false ,
132
- isFetching : false ,
133
- } ,
134
- {
135
- status : 'success' ,
136
- data : 10 ,
137
- isPlaceholderData : false ,
138
- isFetching : false ,
139
- } ,
140
- ] )
141
- } )
142
-
143
- it ( 'should keep previous data for variable amounts of useQueries' , async ( ) => {
144
- const key = queryKey ( )
145
- const states : UseQueryResult [ ] [ ] = [ ]
146
-
147
- function Page ( ) {
148
- const [ count , setCount ] = React . useState ( 2 )
149
- const result = useQueries ( {
150
- queries : Array . from ( { length : count } , ( _ , i ) => ( {
151
- queryKey : [ key , count , i + 1 ] ,
152
- placeholderData : ( previousData ?: number ) => previousData ,
153
- queryFn : async ( ) => {
154
- await sleep ( 35 * ( i + 1 ) )
155
- return ( i + 1 ) * count * 2
156
- } ,
157
- } ) ) ,
158
- } )
159
-
160
- states . push ( result )
161
-
162
- const isFetching = result . some ( ( r ) => r . isFetching )
163
-
164
- return (
165
- < div >
166
- < div > data: { result . map ( ( it ) => it . data ) . join ( ',' ) } </ div >
167
- < div > isFetching: { String ( isFetching ) } </ div >
168
- < button onClick = { ( ) => setCount ( ( prev ) => prev + 1 ) } > inc</ button >
169
- </ div >
170
- )
171
- }
172
-
173
- const rendered = renderWithClient ( queryClient , < Page /> )
174
-
175
- await waitFor ( ( ) => rendered . getByText ( 'data: 4,8' ) )
176
- fireEvent . click ( rendered . getByRole ( 'button' , { name : / i n c / i } ) )
177
-
178
- await waitFor ( ( ) => rendered . getByText ( 'data: 6,12,18' ) )
179
- await waitFor ( ( ) => rendered . getByText ( 'isFetching: false' ) )
180
-
181
- expect ( states [ states . length - 1 ] ) . toMatchObject ( [
182
- {
183
- status : 'success' ,
184
- data : 6 ,
185
- isPlaceholderData : false ,
186
- isFetching : false ,
187
- } ,
188
- {
189
- status : 'success' ,
190
- data : 12 ,
191
- isPlaceholderData : false ,
192
- isFetching : false ,
193
- } ,
194
- {
195
- status : 'success' ,
196
- data : 18 ,
197
- isPlaceholderData : false ,
198
- isFetching : false ,
199
- } ,
200
- ] )
201
- } )
202
-
203
- it ( 'should keep previous data when switching between queries' , async ( ) => {
204
- const key = queryKey ( )
205
- const states : UseQueryResult [ ] [ ] = [ ]
206
-
207
- function Page ( ) {
208
- const [ series1 , setSeries1 ] = React . useState ( 1 )
209
- const [ series2 , setSeries2 ] = React . useState ( 2 )
210
- const ids = [ series1 , series2 ]
211
-
212
- const result = useQueries ( {
213
- queries : ids . map ( ( id ) => {
214
- return {
215
- queryKey : [ key , id ] ,
216
- queryFn : async ( ) => {
217
- await sleep ( 5 )
218
- return id * 5
219
- } ,
220
- placeholderData : ( previousData ?: number ) => previousData ,
221
- }
222
- } ) ,
223
- } )
224
-
225
- states . push ( result )
226
-
227
- const isFetching = result . some ( ( r ) => r . isFetching )
228
-
229
- return (
230
- < div >
231
- < div >
232
- data1: { String ( result [ 0 ] ?. data ?? 'null' ) } , data2:{ ' ' }
233
- { String ( result [ 1 ] ?. data ?? 'null' ) }
234
- </ div >
235
- < div > isFetching: { String ( isFetching ) } </ div >
236
- < button onClick = { ( ) => setSeries2 ( 3 ) } > setSeries2</ button >
237
- < button onClick = { ( ) => setSeries1 ( 2 ) } > setSeries1</ button >
238
- </ div >
239
- )
240
- }
241
-
242
- const rendered = renderWithClient ( queryClient , < Page /> )
243
-
244
- await waitFor ( ( ) => rendered . getByText ( 'data1: 5, data2: 10' ) )
245
- fireEvent . click ( rendered . getByRole ( 'button' , { name : / s e t S e r i e s 2 / i } ) )
246
-
247
- await waitFor ( ( ) => rendered . getByText ( 'data1: 5, data2: 15' ) )
248
- fireEvent . click ( rendered . getByRole ( 'button' , { name : / s e t S e r i e s 1 / i } ) )
249
-
250
- await waitFor ( ( ) => rendered . getByText ( 'data1: 10, data2: 15' ) )
251
- await waitFor ( ( ) => rendered . getByText ( 'isFetching: false' ) )
252
-
253
- expect ( states [ states . length - 1 ] ) . toMatchObject ( [
254
- {
255
- status : 'success' ,
256
- data : 10 ,
257
- isPlaceholderData : false ,
258
- isFetching : false ,
259
- } ,
260
- {
261
- status : 'success' ,
262
- data : 15 ,
263
- isPlaceholderData : false ,
264
- isFetching : false ,
265
- } ,
266
- ] )
267
- } )
268
-
269
- it ( 'should not go to infinite render loop with previous data when toggling queries' , async ( ) => {
270
- const key = queryKey ( )
271
- const states : UseQueryResult [ ] [ ] = [ ]
272
-
273
- function Page ( ) {
274
- const [ enableId1 , setEnableId1 ] = React . useState ( true )
275
- const ids = enableId1 ? [ 1 , 2 ] : [ 2 ]
276
-
277
- const result = useQueries ( {
278
- queries : ids . map ( ( id ) => {
279
- return {
280
- queryKey : [ key , id ] ,
281
- queryFn : async ( ) => {
282
- await sleep ( 5 )
283
- return id * 5
284
- } ,
285
- placeholderData : ( previousData ?: number ) => previousData ,
286
- }
287
- } ) ,
288
- } )
289
-
290
- states . push ( result )
291
-
292
- const isFetching = result . some ( ( r ) => r . isFetching )
293
-
294
- return (
295
- < div >
296
- < div >
297
- data1: { String ( result [ 0 ] ?. data ?? 'null' ) } , data2:{ ' ' }
298
- { String ( result [ 1 ] ?. data ?? 'null' ) }
299
- </ div >
300
- < div > isFetching: { String ( isFetching ) } </ div >
301
- < button onClick = { ( ) => setEnableId1 ( false ) } > set1Disabled</ button >
302
- < button onClick = { ( ) => setEnableId1 ( true ) } > set2Enabled</ button >
303
- </ div >
304
- )
305
- }
306
-
307
- const rendered = renderWithClient ( queryClient , < Page /> )
308
-
309
- await waitFor ( ( ) => rendered . getByText ( 'data1: 5, data2: 10' ) )
310
- fireEvent . click ( rendered . getByRole ( 'button' , { name : / s e t 1 D i s a b l e d / i } ) )
311
-
312
- await waitFor ( ( ) => rendered . getByText ( 'data1: 10, data2: null' ) )
313
- await waitFor ( ( ) => rendered . getByText ( 'isFetching: false' ) )
314
- fireEvent . click ( rendered . getByRole ( 'button' , { name : / s e t 2 E n a b l e d / i } ) )
315
-
316
- await waitFor ( ( ) => rendered . getByText ( 'data1: 5, data2: 10' ) )
317
- await waitFor ( ( ) => rendered . getByText ( 'isFetching: false' ) )
318
-
319
- await waitFor ( ( ) => expect ( states . length ) . toBe ( 6 ) )
320
-
321
- expect ( states [ 0 ] ) . toMatchObject ( [
322
- {
323
- status : 'loading' ,
324
- data : undefined ,
325
- isPlaceholderData : false ,
326
- isFetching : true ,
327
- } ,
328
- {
329
- status : 'loading' ,
330
- data : undefined ,
331
- isPlaceholderData : false ,
332
- isFetching : true ,
333
- } ,
334
- ] )
335
- expect ( states [ 1 ] ) . toMatchObject ( [
336
- {
337
- status : 'success' ,
338
- data : 5 ,
339
- isPlaceholderData : false ,
340
- isFetching : false ,
341
- } ,
342
- {
343
- status : 'success' ,
344
- data : 10 ,
345
- isPlaceholderData : false ,
346
- isFetching : false ,
347
- } ,
348
- ] )
349
- expect ( states [ 2 ] ) . toMatchObject ( [
350
- {
351
- status : 'success' ,
352
- data : 10 ,
353
- isPlaceholderData : false ,
354
- isFetching : false ,
355
- } ,
356
- ] )
357
- expect ( states [ 3 ] ) . toMatchObject ( [
358
- {
359
- status : 'success' ,
360
- data : 5 ,
361
- isPlaceholderData : false ,
362
- isFetching : true ,
363
- } ,
364
- {
365
- status : 'success' ,
366
- data : 10 ,
367
- isPlaceholderData : false ,
368
- isFetching : false ,
369
- } ,
370
- ] )
371
- expect ( states [ 4 ] ) . toMatchObject ( [
372
- {
373
- status : 'success' ,
374
- data : 5 ,
375
- isPlaceholderData : false ,
376
- isFetching : true ,
377
- } ,
378
- {
379
- status : 'success' ,
380
- data : 10 ,
381
- isPlaceholderData : false ,
382
- isFetching : false ,
383
- } ,
384
- ] )
385
- expect ( states [ 5 ] ) . toMatchObject ( [
386
- {
387
- status : 'success' ,
388
- data : 5 ,
389
- isPlaceholderData : false ,
390
- isFetching : false ,
391
- } ,
392
- {
393
- status : 'success' ,
394
- data : 10 ,
395
- isPlaceholderData : false ,
396
- isFetching : false ,
397
- } ,
398
- ] )
399
- } )
400
-
401
76
it ( 'handles type parameter - tuple of tuples' , async ( ) => {
402
77
const key1 = queryKey ( )
403
78
const key2 = queryKey ( )
0 commit comments