forked from TanStack/query
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotifyManager.test.tsx
51 lines (42 loc) · 1.56 KB
/
notifyManager.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { createNotifyManager } from '../notifyManager'
import { sleep } from '../../../../tests/utils'
describe('notifyManager', () => {
it('should use default notifyFn', async () => {
const notifyManagerTest = createNotifyManager()
const callbackSpy = jest.fn()
notifyManagerTest.schedule(callbackSpy)
await sleep(1)
expect(callbackSpy).toHaveBeenCalled()
})
it('should use default batchNotifyFn', async () => {
const notifyManagerTest = createNotifyManager()
const callbackScheduleSpy = jest
.fn()
.mockImplementation(async () => await sleep(20))
const callbackBatchLevel2Spy = jest.fn().mockImplementation(async () => {
notifyManagerTest.schedule(callbackScheduleSpy)
})
const callbackBatchLevel1Spy = jest.fn().mockImplementation(async () => {
notifyManagerTest.batch(callbackBatchLevel2Spy)
})
notifyManagerTest.batch(callbackBatchLevel1Spy)
await sleep(30)
expect(callbackBatchLevel1Spy).toHaveBeenCalledTimes(1)
expect(callbackBatchLevel2Spy).toHaveBeenCalledTimes(1)
expect(callbackScheduleSpy).toHaveBeenCalledTimes(1)
})
it('should notify if error is thrown', async () => {
const notifyManagerTest = createNotifyManager()
const notifySpy = jest.fn()
notifyManagerTest.setNotifyFunction(notifySpy)
try {
notifyManagerTest.batch(() => {
notifyManagerTest.schedule(jest.fn)
throw new Error('Foo')
})
} catch {}
// needed for scheduleMicroTask to kick in
await sleep(1)
expect(notifySpy).toHaveBeenCalledTimes(1)
})
})