Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

notifyManager.test.tsx

102 lines3.2 KB

1import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2import { sleep } from '@tanstack/query-test-utils'
3import { createNotifyManager } from '../notifyManager'
4​
5describe('notifyManager', () => {
6 beforeEach(() => {
7 vi.useFakeTimers()
8 })
9​
10 afterEach(() => {
11 vi.useRealTimers()
12 })
13​
14 it('should use default notifyFn', async () => {
15 const notifyManagerTest = createNotifyManager()
16 const callbackSpy = vi.fn()
17 notifyManagerTest.schedule(callbackSpy)
18 await vi.advanceTimersByTimeAsync(0)
19 expect(callbackSpy).toHaveBeenCalled()
20 })
21​
22 it('should use default batchNotifyFn', async () => {
23 const notifyManagerTest = createNotifyManager()
24 const callbackScheduleSpy = vi.fn().mockImplementation(() => sleep(20))
25 const callbackBatchLevel2Spy = vi.fn().mockImplementation(() => {
26 notifyManagerTest.schedule(callbackScheduleSpy)
27 })
28 const callbackBatchLevel1Spy = vi.fn().mockImplementation(() => {
29 notifyManagerTest.batch(callbackBatchLevel2Spy)
30 })
31 notifyManagerTest.batch(callbackBatchLevel1Spy)
32​
33 await vi.advanceTimersByTimeAsync(0)
34 expect(callbackBatchLevel1Spy).toHaveBeenCalledTimes(1)
35 expect(callbackBatchLevel2Spy).toHaveBeenCalledTimes(1)
36 expect(callbackScheduleSpy).toHaveBeenCalledTimes(1)
37 })
38​
39 it('should use a custom scheduler when configured', async () => {
40 const customCallback = vi.fn((cb) => queueMicrotask(cb))
41​
42 const notifyManagerTest = createNotifyManager()
43 const notifySpy = vi.fn()
44 notifyManagerTest.setScheduler(customCallback)
45 notifyManagerTest.setNotifyFunction(notifySpy)
46​
47 notifyManagerTest.batch(() => notifyManagerTest.schedule(vi.fn))
48​
49 expect(customCallback).toHaveBeenCalledOnce()
50 await vi.advanceTimersByTimeAsync(0)
51 expect(notifySpy).toHaveBeenCalledTimes(1)
52 })
53​
54 it('should notify if error is thrown', async () => {
55 const notifyManagerTest = createNotifyManager()
56 const notifySpy = vi.fn()
57​
58 notifyManagerTest.setNotifyFunction(notifySpy)
59​
60 try {
61 notifyManagerTest.batch(() => {
62 notifyManagerTest.schedule(vi.fn)
63 throw new Error('Foo')
64 })
65 } catch {}
66​
67 await vi.advanceTimersByTimeAsync(0)
68 expect(notifySpy).toHaveBeenCalledTimes(1)
69 })
70​
71 it('should use custom batch notify function', async () => {
72 const notifyManagerTest = createNotifyManager()
73 const batchNotifySpy = vi.fn((cb) => cb())
74 const callbackSpy1 = vi.fn()
75 const callbackSpy2 = vi.fn()
76​
77 notifyManagerTest.setBatchNotifyFunction(batchNotifySpy)
78​
79 notifyManagerTest.batch(() => {
80 notifyManagerTest.schedule(callbackSpy1)
81 notifyManagerTest.schedule(callbackSpy2)
82 })
83​
84 await vi.advanceTimersByTimeAsync(0)
85 expect(batchNotifySpy).toHaveBeenCalled()
86 expect(callbackSpy1).toHaveBeenCalled()
87 expect(callbackSpy2).toHaveBeenCalled()
88 })
89​
90 it('should batch calls correctly', async () => {
91 const notifyManagerTest = createNotifyManager()
92 const callbackSpy = vi.fn()
93​
94 const batchedFn = notifyManagerTest.batchCalls((a: number, b: string) => {
95 callbackSpy(a, b)
96 })
97​
98 batchedFn(1, 'test')
99 await vi.advanceTimersByTimeAsync(0)
100 expect(callbackSpy).toHaveBeenCalledWith(1, 'test')
101 })
102})