Open History to see the last commit
1import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2import { createNotifyManager } from '../notifyManager'
3import { sleep } from './utils'
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(20)
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β
51 // wait until the microtask has run
52 await new Promise<void>((res) => queueMicrotask(res))
53β
54 expect(notifySpy).toHaveBeenCalledTimes(1)
55 })
56β
57 it('should notify if error is thrown', async () => {
58 const notifyManagerTest = createNotifyManager()
59 const notifySpy = vi.fn()
60β
61 notifyManagerTest.setNotifyFunction(notifySpy)
62β
63 try {
64 notifyManagerTest.batch(() => {
65 notifyManagerTest.schedule(vi.fn)
66 throw new Error('Foo')
67 })
68 } catch {}
69β
70 await vi.advanceTimersByTimeAsync(0)
71β
72 expect(notifySpy).toHaveBeenCalledTimes(1)
73 })
74β
75 it('typeDefs should catch proper signatures', () => {
76 const notifyManagerTest = createNotifyManager()
77β
78 // we define some fn with its signature:
79 const fn: (a: string, b: number) => string = (a, b) => a + b
80β
81 // now someFn expect to be called with args [a: string, b: number]
82 const someFn = notifyManagerTest.batchCalls(fn)
83β
84 someFn('im happy', 4)
85β
86 // @ts-expect-error
87 someFn('im not happy', false)
88 })
89β
90 it('should use custom batch notify function', async () => {
91 const notifyManagerTest = createNotifyManager()
92 const batchNotifySpy = vi.fn((cb) => cb())
93 const callbackSpy1 = vi.fn()
94 const callbackSpy2 = vi.fn()
95β
96 notifyManagerTest.setBatchNotifyFunction(batchNotifySpy)
97β
98 notifyManagerTest.batch(() => {
99 notifyManagerTest.schedule(callbackSpy1)
100 notifyManagerTest.schedule(callbackSpy2)
101 })
102β
103 await vi.advanceTimersByTimeAsync(0)
104β
105 expect(batchNotifySpy).toHaveBeenCalled()
106 expect(callbackSpy1).toHaveBeenCalled()
107 expect(callbackSpy2).toHaveBeenCalled()
108 })
109β
110 it('should batch calls correctly', async () => {
111 const notifyManagerTest = createNotifyManager()
112 const callbackSpy = vi.fn()
113β
114 const batchedFn = notifyManagerTest.batchCalls((a: number, b: string) => {
115 callbackSpy(a, b)
116 })
117β
118 batchedFn(1, 'test')
119 await vi.advanceTimersByTimeAsync(0)
120β
121 expect(callbackSpy).toHaveBeenCalledWith(1, 'test')
122 })
123})