-
Notifications
You must be signed in to change notification settings - Fork 230
/
asyncHook.fakeTimers.test.ts
93 lines (68 loc) 路 2.11 KB
/
asyncHook.fakeTimers.test.ts
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
describe('async hook (fake timers) tests', () => {
beforeEach(() => {
jest.useFakeTimers()
})
afterEach(() => {
jest.useRealTimers()
})
runForRenderers(['default', 'dom', 'native', 'server/hydrated'], ({ renderHook }) => {
test('should wait for arbitrary expectation to pass when using advanceTimersByTime()', async () => {
const { waitFor } = renderHook(() => null)
let actual = 0
const expected = 1
setTimeout(() => {
actual = expected
}, 200)
let complete = false
jest.advanceTimersByTime(200)
await waitFor(() => {
expect(actual).toBe(expected)
complete = true
})
expect(complete).toBe(true)
})
test('should wait for arbitrary expectation to pass when using runOnlyPendingTimers()', async () => {
const { waitFor } = renderHook(() => null)
let actual = 0
const expected = 1
setTimeout(() => {
actual = expected
}, 200)
let complete = false
jest.runOnlyPendingTimers()
await waitFor(() => {
expect(actual).toBe(expected)
complete = true
})
expect(complete).toBe(true)
})
test('should waitFor arbitrary expectation to pass when fake timers are not advanced explicitly', async () => {
const fn = jest.fn().mockReturnValueOnce(false).mockReturnValueOnce(true)
const { waitFor } = renderHook(() => null)
await waitFor(() => {
expect(fn()).toBe(true)
})
})
test('should reject if timeout is passed close to when promise resolves', async () => {
const { waitFor } = renderHook(() => null)
let actual = 0
const expected = 1
setTimeout(() => {
actual = expected
}, 101)
let complete = false
await expect(
waitFor(
() => {
expect(actual).toBe(expected)
complete = true
},
{ timeout: 100, interval: 50 }
)
).rejects.toThrow(Error('Timed out in waitFor after 100ms.'))
expect(complete).toBe(false)
})
})
})
// eslint-disable-next-line jest/no-export
export {}