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

notifyManager.test.tsx

66 lines2.0 KB

1import { createNotifyManager } from '../notifyManager'
2import { sleep } from './utils'
3​
4describe('notifyManager', () => {
5 it('should use default notifyFn', async () => {
6 const notifyManagerTest = createNotifyManager()
7 const callbackSpy = jest.fn()
8 notifyManagerTest.schedule(callbackSpy)
9 await sleep(1)
10 expect(callbackSpy).toHaveBeenCalled()
11 })
12​
13 it('should use default batchNotifyFn', async () => {
14 const notifyManagerTest = createNotifyManager()
15 const callbackScheduleSpy = jest
16 .fn()
17 .mockImplementation(async () => await sleep(20))
18 const callbackBatchLevel2Spy = jest.fn().mockImplementation(async () => {
19 notifyManagerTest.schedule(callbackScheduleSpy)
20 })
21 const callbackBatchLevel1Spy = jest.fn().mockImplementation(async () => {
22 notifyManagerTest.batch(callbackBatchLevel2Spy)
23 })
24​
25 notifyManagerTest.batch(callbackBatchLevel1Spy)
26​
27 await sleep(30)
28 expect(callbackBatchLevel1Spy).toHaveBeenCalledTimes(1)
29 expect(callbackBatchLevel2Spy).toHaveBeenCalledTimes(1)
30 expect(callbackScheduleSpy).toHaveBeenCalledTimes(1)
31 })
32​
33 it('should notify if error is thrown', async () => {
34 const notifyManagerTest = createNotifyManager()
35 const notifySpy = jest.fn()
36​
37 notifyManagerTest.setNotifyFunction(notifySpy)
38​
39 try {
40 notifyManagerTest.batch(() => {
41 notifyManagerTest.schedule(jest.fn)
42 throw new Error('Foo')
43 })
44 } catch {}
45​
46 // needed for scheduleMicroTask to kick in
47 await sleep(1)
48​
49 expect(notifySpy).toHaveBeenCalledTimes(1)
50 })
51​
52 it('typedefs should catch proper signatures', async () => {
53 const notifyManagerTest = createNotifyManager()
54​
55 // we define some fn with its signature:
56 const fn: (a: string, b: number) => string = (a, b) => a + b
57​
58 //now somefn expect to be called with args [a: string, b: number]
59 const someFn = notifyManagerTest.batchCalls(fn)
60​
61 someFn('im happy', 4)
62​
63 //@ts-expect-error
64 someFn('im not happy', false)
65 })
66})