-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
notifyManager.test.tsx
67 lines (53 loc) 路 2.01 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { describe, expect, it, vi } from 'vitest'
import { createNotifyManager } from '../notifyManager'
import { sleep } from './utils'
describe('notifyManager', () => {
it('should use default notifyFn', async () => {
const notifyManagerTest = createNotifyManager()
const callbackSpy = vi.fn()
notifyManagerTest.schedule(callbackSpy)
await sleep(1)
expect(callbackSpy).toHaveBeenCalled()
})
it('should use default batchNotifyFn', async () => {
const notifyManagerTest = createNotifyManager()
const callbackScheduleSpy = vi
.fn()
.mockImplementation(async () => await sleep(20))
const callbackBatchLevel2Spy = vi.fn().mockImplementation(async () => {
notifyManagerTest.schedule(callbackScheduleSpy)
})
const callbackBatchLevel1Spy = vi.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 = vi.fn()
notifyManagerTest.setNotifyFunction(notifySpy)
try {
notifyManagerTest.batch(() => {
notifyManagerTest.schedule(vi.fn)
throw new Error('Foo')
})
} catch {}
// needed for scheduleMicroTask to kick in
await sleep(1)
expect(notifySpy).toHaveBeenCalledTimes(1)
})
it('typedefs should catch proper signatures', async () => {
const notifyManagerTest = createNotifyManager()
// we define some fn with its signature:
const fn: (a: string, b: number) => string = (a, b) => a + b
//now somefn expect to be called with args [a: string, b: number]
const someFn = notifyManagerTest.batchCalls(fn)
someFn('im happy', 4)
//@ts-expect-error
someFn('im not happy', false)
})
})