Skip to content

Commit aec8321

Browse files
committed
test: add a test for not a signal that is not consumed by streamFn
1 parent 2211c1a commit aec8321

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

packages/query-core/src/__tests__/streamedQuery.test.tsx

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,12 @@ describe('streamedQuery', () => {
329329
const observer = new QueryObserver(queryClient, {
330330
queryKey: key,
331331
queryFn: streamedQuery({
332-
streamFn: () => createAsyncNumberGenerator(3),
332+
streamFn: (context) => {
333+
// just consume the signal
334+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
335+
const numbers = context.signal ? 3 : 0
336+
return createAsyncNumberGenerator(numbers)
337+
},
333338
refetchMode: 'append',
334339
}),
335340
})
@@ -420,6 +425,42 @@ describe('streamedQuery', () => {
420425
})
421426
})
422427

428+
test('should not abort when signal not consumed', async () => {
429+
const key = queryKey()
430+
const observer = new QueryObserver(queryClient, {
431+
queryKey: key,
432+
queryFn: streamedQuery({
433+
streamFn: () => createAsyncNumberGenerator(3),
434+
}),
435+
})
436+
437+
const unsubscribe = observer.subscribe(vi.fn())
438+
439+
expect(queryClient.getQueryState(key)).toMatchObject({
440+
status: 'pending',
441+
fetchStatus: 'fetching',
442+
data: undefined,
443+
})
444+
445+
await vi.advanceTimersByTimeAsync(60)
446+
447+
expect(queryClient.getQueryState(key)).toMatchObject({
448+
status: 'success',
449+
fetchStatus: 'fetching',
450+
data: [0],
451+
})
452+
453+
unsubscribe()
454+
455+
await vi.advanceTimersByTimeAsync(50)
456+
457+
expect(queryClient.getQueryState(key)).toMatchObject({
458+
status: 'success',
459+
fetchStatus: 'idle',
460+
data: [0, 1],
461+
})
462+
})
463+
423464
test('should support custom reducer', async () => {
424465
const key = queryKey()
425466

0 commit comments

Comments
 (0)