Open History to see the last commit
1import { sleep } from '@tanstack/query-test-utils'
2import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
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<() => void>()
17 notifyManagerTest.schedule(callbackSpy)
18 await vi.advanceTimersByTimeAsync(0)
19 expect(callbackSpy).toHaveBeenCalledWith()
20 })
21β
22 it('should use default batchNotifyFn', async () => {
23 const notifyManagerTest = createNotifyManager()
24 const callbackScheduleSpy = vi.fn<() => Promise<void>>(() => sleep(20))
25 const callbackBatchLevel2Spy = vi
26 .fn<() => void>()
27 .mockImplementation(() => {
28 notifyManagerTest.schedule(callbackScheduleSpy)
29 })
30 const callbackBatchLevel1Spy = vi
31 .fn<() => void>()
32 .mockImplementation(() => {
33 notifyManagerTest.batch(callbackBatchLevel2Spy)
34 })
35 notifyManagerTest.batch(callbackBatchLevel1Spy)
36β
37 await vi.advanceTimersByTimeAsync(0)
38 expect(callbackBatchLevel1Spy).toHaveBeenCalledTimes(1)
39 expect(callbackBatchLevel2Spy).toHaveBeenCalledTimes(1)
40 expect(callbackScheduleSpy).toHaveBeenCalledTimes(1)
41 })
42β
43 it('should use a custom scheduler when configured', async () => {
44 const customCallback = vi.fn<(callback: () => void) => void>((cb) =>
45 queueMicrotask(cb),
46 )
47β
48 const notifyManagerTest = createNotifyManager()
49 const notifySpy = vi.fn<(callback: () => void) => void>()
50 notifyManagerTest.setScheduler(customCallback)
51 notifyManagerTest.setNotifyFunction(notifySpy)
52β
53 notifyManagerTest.batch(() => notifyManagerTest.schedule(vi.fn))
54β
55 expect(customCallback).toHaveBeenCalledTimes(1)
56 await vi.advanceTimersByTimeAsync(0)
57 expect(notifySpy).toHaveBeenCalledTimes(1)
58 })
59β
60 it('should notify if error is thrown', async () => {
61 const notifyManagerTest = createNotifyManager()
62 const notifySpy = vi.fn<(callback: () => void) => void>()
63β
64 notifyManagerTest.setNotifyFunction(notifySpy)
65β
66 try {
67 notifyManagerTest.batch(() => {
68 notifyManagerTest.schedule(vi.fn)
69 throw new Error('Foo')
70 })
71 } catch {}
72β
73 await vi.advanceTimersByTimeAsync(0)
74 expect(notifySpy).toHaveBeenCalledTimes(1)
75 })
76β
77 it('should use custom batch notify function', async () => {
78 const notifyManagerTest = createNotifyManager()
79 const batchNotifySpy = vi.fn<(callback: () => void) => void>((cb) => cb())
80 const callbackSpy1 = vi.fn<() => void>()
81 const callbackSpy2 = vi.fn<() => void>()
82β
83 notifyManagerTest.setBatchNotifyFunction(batchNotifySpy)
84β
85 notifyManagerTest.batch(() => {
86 notifyManagerTest.schedule(callbackSpy1)
87 notifyManagerTest.schedule(callbackSpy2)
88 })
89β
90 await vi.advanceTimersByTimeAsync(0)
91 expect(batchNotifySpy).toHaveBeenCalledWith(expect.any(Function))
92 expect(callbackSpy1).toHaveBeenCalledWith()
93 expect(callbackSpy2).toHaveBeenCalledWith()
94 })
95β
96 it('should batch calls correctly', async () => {
97 const notifyManagerTest = createNotifyManager()
98 const callbackSpy = vi.fn<(a: number, b: string) => void>()
99β
100 const batchedFn = notifyManagerTest.batchCalls((a: number, b: string) => {
101 callbackSpy(a, b)
102 })
103β
104 batchedFn(1, 'test')
105 await vi.advanceTimersByTimeAsync(0)
106 expect(callbackSpy).toHaveBeenCalledWith(1, 'test')
107 })
108})