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

notifyManager.test.tsx

123 lines3.6 KB

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})