@@ -18,11 +18,11 @@ describe('streamedQuery', () => {
18
18
vi . useRealTimers ( )
19
19
} )
20
20
21
- function createAsyncNumberGenerator ( amount : number ) {
21
+ function createAsyncNumberGenerator ( amount : number , start = 0 ) {
22
22
return {
23
23
async * [ Symbol . asyncIterator ] ( ) {
24
- let num = 0
25
- while ( num < amount ) {
24
+ let num = start
25
+ while ( num < amount + start ) {
26
26
await sleep ( 50 )
27
27
yield num ++
28
28
}
@@ -74,6 +74,61 @@ describe('streamedQuery', () => {
74
74
unsubscribe ( )
75
75
} )
76
76
77
+ test ( 'should allow Arrays to be returned from the stream' , async ( ) => {
78
+ const key = queryKey ( )
79
+ const observer = new QueryObserver ( queryClient , {
80
+ queryKey : key ,
81
+ queryFn : streamedQuery ( {
82
+ queryFn : async function * ( ) {
83
+ for await ( const num of createAsyncNumberGenerator ( 3 ) ) {
84
+ yield [ num , num ] as const
85
+ }
86
+ } ,
87
+ } ) ,
88
+ } )
89
+
90
+ const unsubscribe = observer . subscribe ( vi . fn ( ) )
91
+
92
+ expect ( observer . getCurrentResult ( ) ) . toMatchObject ( {
93
+ status : 'pending' ,
94
+ fetchStatus : 'fetching' ,
95
+ data : undefined ,
96
+ } )
97
+
98
+ await vi . advanceTimersByTimeAsync ( 50 )
99
+
100
+ expect ( observer . getCurrentResult ( ) ) . toMatchObject ( {
101
+ status : 'success' ,
102
+ fetchStatus : 'fetching' ,
103
+ data : [ [ 0 , 0 ] ] ,
104
+ } )
105
+
106
+ await vi . advanceTimersByTimeAsync ( 50 )
107
+
108
+ expect ( observer . getCurrentResult ( ) ) . toMatchObject ( {
109
+ status : 'success' ,
110
+ fetchStatus : 'fetching' ,
111
+ data : [
112
+ [ 0 , 0 ] ,
113
+ [ 1 , 1 ] ,
114
+ ] ,
115
+ } )
116
+
117
+ await vi . advanceTimersByTimeAsync ( 50 )
118
+
119
+ expect ( observer . getCurrentResult ( ) ) . toMatchObject ( {
120
+ status : 'success' ,
121
+ fetchStatus : 'idle' ,
122
+ data : [
123
+ [ 0 , 0 ] ,
124
+ [ 1 , 1 ] ,
125
+ [ 2 , 2 ] ,
126
+ ] ,
127
+ } )
128
+
129
+ unsubscribe ( )
130
+ } )
131
+
77
132
test ( 'should replace on refetch' , async ( ) => {
78
133
const key = queryKey ( )
79
134
const observer = new QueryObserver ( queryClient , {
@@ -183,6 +238,64 @@ describe('streamedQuery', () => {
183
238
unsubscribe ( )
184
239
} )
185
240
241
+ test ( 'should support refetchMode replace' , async ( ) => {
242
+ const key = queryKey ( )
243
+ let offset = 0
244
+ const observer = new QueryObserver ( queryClient , {
245
+ queryKey : key ,
246
+ queryFn : streamedQuery ( {
247
+ queryFn : ( ) => createAsyncNumberGenerator ( 2 , offset ) ,
248
+ refetchMode : 'replace' ,
249
+ } ) ,
250
+ } )
251
+
252
+ const unsubscribe = observer . subscribe ( vi . fn ( ) )
253
+
254
+ expect ( observer . getCurrentResult ( ) ) . toMatchObject ( {
255
+ status : 'pending' ,
256
+ fetchStatus : 'fetching' ,
257
+ data : undefined ,
258
+ } )
259
+
260
+ await vi . advanceTimersByTimeAsync ( 100 )
261
+
262
+ expect ( observer . getCurrentResult ( ) ) . toMatchObject ( {
263
+ status : 'success' ,
264
+ fetchStatus : 'idle' ,
265
+ data : [ 0 , 1 ] ,
266
+ } )
267
+
268
+ offset = 100
269
+
270
+ void observer . refetch ( )
271
+
272
+ await vi . advanceTimersByTimeAsync ( 10 )
273
+
274
+ expect ( observer . getCurrentResult ( ) ) . toMatchObject ( {
275
+ status : 'success' ,
276
+ fetchStatus : 'fetching' ,
277
+ data : [ 0 , 1 ] ,
278
+ } )
279
+
280
+ await vi . advanceTimersByTimeAsync ( 40 )
281
+
282
+ expect ( observer . getCurrentResult ( ) ) . toMatchObject ( {
283
+ status : 'success' ,
284
+ fetchStatus : 'fetching' ,
285
+ data : [ 0 , 1 ] ,
286
+ } )
287
+
288
+ await vi . advanceTimersByTimeAsync ( 50 )
289
+
290
+ expect ( observer . getCurrentResult ( ) ) . toMatchObject ( {
291
+ status : 'success' ,
292
+ fetchStatus : 'idle' ,
293
+ data : [ 100 , 101 ] ,
294
+ } )
295
+
296
+ unsubscribe ( )
297
+ } )
298
+
186
299
test ( 'should abort ongoing stream when refetch happens' , async ( ) => {
187
300
const key = queryKey ( )
188
301
const observer = new QueryObserver ( queryClient , {
0 commit comments