Skip to content

Commit dcfc100

Browse files
authored
test(query-core): use fake timer for notifyManager.test.tsx (#8773)
* test(query-core): use fake timer for notifyManager.test.tsx * chore: update
1 parent 8bfa77b commit dcfc100

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

packages/query-core/src/__tests__/notifyManager.test.tsx

+13-9
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,36 @@
1-
import { describe, expect, it, vi } from 'vitest'
1+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
22
import { createNotifyManager } from '../notifyManager'
33
import { sleep } from './utils'
44

55
describe('notifyManager', () => {
6+
beforeEach(() => {
7+
vi.useFakeTimers()
8+
})
9+
10+
afterEach(() => {
11+
vi.useRealTimers()
12+
})
13+
614
it('should use default notifyFn', async () => {
715
const notifyManagerTest = createNotifyManager()
816
const callbackSpy = vi.fn()
917
notifyManagerTest.schedule(callbackSpy)
10-
await sleep(1)
18+
await vi.advanceTimersByTimeAsync(0)
1119
expect(callbackSpy).toHaveBeenCalled()
1220
})
1321

1422
it('should use default batchNotifyFn', async () => {
1523
const notifyManagerTest = createNotifyManager()
16-
const callbackScheduleSpy = vi
17-
.fn()
18-
.mockImplementation(async () => await sleep(20))
24+
const callbackScheduleSpy = vi.fn().mockImplementation(() => sleep(20))
1925
const callbackBatchLevel2Spy = vi.fn().mockImplementation(async () => {
2026
notifyManagerTest.schedule(callbackScheduleSpy)
2127
})
2228
const callbackBatchLevel1Spy = vi.fn().mockImplementation(async () => {
2329
notifyManagerTest.batch(callbackBatchLevel2Spy)
2430
})
25-
2631
notifyManagerTest.batch(callbackBatchLevel1Spy)
2732

28-
await sleep(30)
33+
await vi.advanceTimersByTimeAsync(20)
2934
expect(callbackBatchLevel1Spy).toHaveBeenCalledTimes(1)
3035
expect(callbackBatchLevel2Spy).toHaveBeenCalledTimes(1)
3136
expect(callbackScheduleSpy).toHaveBeenCalledTimes(1)
@@ -62,8 +67,7 @@ describe('notifyManager', () => {
6267
})
6368
} catch {}
6469

65-
// needed for setTimeout to kick in
66-
await sleep(1)
70+
await vi.advanceTimersByTimeAsync(0)
6771

6872
expect(notifySpy).toHaveBeenCalledTimes(1)
6973
})

0 commit comments

Comments
 (0)