|
1 |
| -import { describe, expect, it, vi } from 'vitest' |
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
2 | 2 | import { createNotifyManager } from '../notifyManager'
|
3 | 3 | import { sleep } from './utils'
|
4 | 4 |
|
5 | 5 | describe('notifyManager', () => {
|
| 6 | + beforeEach(() => { |
| 7 | + vi.useFakeTimers() |
| 8 | + }) |
| 9 | + |
| 10 | + afterEach(() => { |
| 11 | + vi.useRealTimers() |
| 12 | + }) |
| 13 | + |
6 | 14 | it('should use default notifyFn', async () => {
|
7 | 15 | const notifyManagerTest = createNotifyManager()
|
8 | 16 | const callbackSpy = vi.fn()
|
9 | 17 | notifyManagerTest.schedule(callbackSpy)
|
10 |
| - await sleep(1) |
| 18 | + await vi.advanceTimersByTimeAsync(0) |
11 | 19 | expect(callbackSpy).toHaveBeenCalled()
|
12 | 20 | })
|
13 | 21 |
|
14 | 22 | it('should use default batchNotifyFn', async () => {
|
15 | 23 | const notifyManagerTest = createNotifyManager()
|
16 |
| - const callbackScheduleSpy = vi |
17 |
| - .fn() |
18 |
| - .mockImplementation(async () => await sleep(20)) |
| 24 | + const callbackScheduleSpy = vi.fn().mockImplementation(() => sleep(20)) |
19 | 25 | const callbackBatchLevel2Spy = vi.fn().mockImplementation(async () => {
|
20 | 26 | notifyManagerTest.schedule(callbackScheduleSpy)
|
21 | 27 | })
|
22 | 28 | const callbackBatchLevel1Spy = vi.fn().mockImplementation(async () => {
|
23 | 29 | notifyManagerTest.batch(callbackBatchLevel2Spy)
|
24 | 30 | })
|
25 |
| - |
26 | 31 | notifyManagerTest.batch(callbackBatchLevel1Spy)
|
27 | 32 |
|
28 |
| - await sleep(30) |
| 33 | + await vi.advanceTimersByTimeAsync(20) |
29 | 34 | expect(callbackBatchLevel1Spy).toHaveBeenCalledTimes(1)
|
30 | 35 | expect(callbackBatchLevel2Spy).toHaveBeenCalledTimes(1)
|
31 | 36 | expect(callbackScheduleSpy).toHaveBeenCalledTimes(1)
|
@@ -62,8 +67,7 @@ describe('notifyManager', () => {
|
62 | 67 | })
|
63 | 68 | } catch {}
|
64 | 69 |
|
65 |
| - // needed for setTimeout to kick in |
66 |
| - await sleep(1) |
| 70 | + await vi.advanceTimersByTimeAsync(0) |
67 | 71 |
|
68 | 72 | expect(notifySpy).toHaveBeenCalledTimes(1)
|
69 | 73 | })
|
|
0 commit comments