Open History to see the last commit
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})