Open History to see the last commit
1import {
2 afterEach,
3 assertType,
4 beforeEach,
5 describe,
6 expect,
7 expectTypeOf,
8 it,
9 vi,
10} from 'vitest'
11import { sleep } from '@tanstack/query-test-utils'
12import { createNotifyManager } from '../notifyManager'
13β
14describe('notifyManager', () => {
15 beforeEach(() => {
16 vi.useFakeTimers()
17 })
18β
19 afterEach(() => {
20 vi.useRealTimers()
21 })
22β
23 it('should use default notifyFn', async () => {
24 const notifyManagerTest = createNotifyManager()
25 const callbackSpy = vi.fn()
26 notifyManagerTest.schedule(callbackSpy)
27 await vi.advanceTimersByTimeAsync(0)
28 expect(callbackSpy).toHaveBeenCalled()
29 })
30β
31 it('should use default batchNotifyFn', async () => {
32 const notifyManagerTest = createNotifyManager()
33 const callbackScheduleSpy = vi.fn().mockImplementation(() => sleep(20))
34 const callbackBatchLevel2Spy = vi.fn().mockImplementation(() => {
35 notifyManagerTest.schedule(callbackScheduleSpy)
36 })
37 const callbackBatchLevel1Spy = vi.fn().mockImplementation(() => {
38 notifyManagerTest.batch(callbackBatchLevel2Spy)
39 })
40 notifyManagerTest.batch(callbackBatchLevel1Spy)
41β
42 await vi.advanceTimersByTimeAsync(0)
43 expect(callbackBatchLevel1Spy).toHaveBeenCalledTimes(1)
44 expect(callbackBatchLevel2Spy).toHaveBeenCalledTimes(1)
45 expect(callbackScheduleSpy).toHaveBeenCalledTimes(1)
46 })
47β
48 it('should use a custom scheduler when configured', async () => {
49 const customCallback = vi.fn((cb) => queueMicrotask(cb))
50β
51 const notifyManagerTest = createNotifyManager()
52 const notifySpy = vi.fn()
53 notifyManagerTest.setScheduler(customCallback)
54 notifyManagerTest.setNotifyFunction(notifySpy)
55β
56 notifyManagerTest.batch(() => notifyManagerTest.schedule(vi.fn))
57β
58 expect(customCallback).toHaveBeenCalledOnce()
59β
60 await vi.advanceTimersByTimeAsync(0)
61 expect(notifySpy).toHaveBeenCalledTimes(1)
62 })
63β
64 it('should notify if error is thrown', async () => {
65 const notifyManagerTest = createNotifyManager()
66 const notifySpy = vi.fn()
67β
68 notifyManagerTest.setNotifyFunction(notifySpy)
69β
70 try {
71 notifyManagerTest.batch(() => {
72 notifyManagerTest.schedule(vi.fn)
73 throw new Error('Foo')
74 })
75 } catch {}
76β
77 await vi.advanceTimersByTimeAsync(0)
78β
79 expect(notifySpy).toHaveBeenCalledTimes(1)
80 })
81β
82 it('typeDefs should catch proper signatures', () => {
83 const notifyManagerTest = createNotifyManager()
84β
85 // we define some fn with its signature:
86 const fn: (a: string, b: number) => string = (a, b) => a + b
87β
88 // now someFn expect to be called with args [a: string, b: number]
89 const someFn = notifyManagerTest.batchCalls(fn)
90β
91 expectTypeOf(someFn).parameters.toEqualTypeOf<Parameters<typeof fn>>()
92 assertType<Parameters<typeof someFn>>(['im happy', 4])
93 assertType<Parameters<typeof someFn>>([
94 'im not happy',
95 // @ts-expect-error
96 false,
97 ])
98 })
99β
100 it('should use custom batch notify function', async () => {
101 const notifyManagerTest = createNotifyManager()
102 const batchNotifySpy = vi.fn((cb) => cb())
103 const callbackSpy1 = vi.fn()
104 const callbackSpy2 = vi.fn()
105β
106 notifyManagerTest.setBatchNotifyFunction(batchNotifySpy)
107β
108 notifyManagerTest.batch(() => {
109 notifyManagerTest.schedule(callbackSpy1)
110 notifyManagerTest.schedule(callbackSpy2)
111 })
112β
113 await vi.advanceTimersByTimeAsync(0)
114β
115 expect(batchNotifySpy).toHaveBeenCalled()
116 expect(callbackSpy1).toHaveBeenCalled()
117 expect(callbackSpy2).toHaveBeenCalled()
118 })
119β
120 it('should batch calls correctly', async () => {
121 const notifyManagerTest = createNotifyManager()
122 const callbackSpy = vi.fn()
123β
124 const batchedFn = notifyManagerTest.batchCalls((a: number, b: string) => {
125 callbackSpy(a, b)
126 })
127β
128 batchedFn(1, 'test')
129 await vi.advanceTimersByTimeAsync(0)
130β
131 expect(callbackSpy).toHaveBeenCalledWith(1, 'test')
132 })
133})