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

notifyManager.test.tsx

133 lines3.8 KB

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