Skip to content

Commit 2702b34

Browse files
authored
test(query-core/utils): add tests for consumed signal cancellation and single consumption in 'addConsumeAwareSignal' (#10864)
1 parent 4fb2da1 commit 2702b34

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,5 +657,36 @@ describe('core/utils', () => {
657657

658658
expect(onCancelled).toHaveBeenCalledTimes(1)
659659
})
660+
661+
it('should flag cancellation when the consumed signal aborts, mirroring streamed/infinite queries', () => {
662+
const controller = new AbortController()
663+
let cancelled = false
664+
const context = addConsumeAwareSignal(
665+
{ queryKey: queryKey() },
666+
() => controller.signal,
667+
() => (cancelled = true),
668+
)
669+
670+
void context.signal
671+
expect(cancelled).toBe(false)
672+
673+
controller.abort()
674+
expect(cancelled).toBe(true)
675+
})
676+
677+
it('should consume the signal only once across repeated accesses', () => {
678+
const controller = new AbortController()
679+
const addEventListener = vi.spyOn(controller.signal, 'addEventListener')
680+
const context = addConsumeAwareSignal(
681+
{ queryKey: queryKey() },
682+
() => controller.signal,
683+
vi.fn(),
684+
)
685+
686+
expect(context.signal).toBe(controller.signal)
687+
expect(context.signal).toBe(controller.signal)
688+
689+
expect(addEventListener).toHaveBeenCalledTimes(1)
690+
})
660691
})
661692
})

0 commit comments

Comments
 (0)