Skip to content

Commit 7273ba4

Browse files
chris110408Lei Chenmpeyper
authored
test: add tests to prove jest useFakeTimers is supported (testing-library#641)
Co-authored-by: Lei Chen <leichen@Leis-MacBook-Pro.local> Co-authored-by: Michael Peyper <mpeyper7@gmail.com>
1 parent a54eeb3 commit 7273ba4

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed
+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
describe('async hook (fake timers) tests', () => {
2+
beforeEach(() => {
3+
jest.useFakeTimers()
4+
})
5+
6+
afterEach(() => {
7+
jest.useRealTimers()
8+
})
9+
10+
runForRenderers(['default', 'dom', 'native', 'server/hydrated'], ({ renderHook }) => {
11+
test('should wait for arbitrary expectation to pass when using advanceTimersByTime()', async () => {
12+
const { waitFor } = renderHook(() => null)
13+
14+
let actual = 0
15+
const expected = 1
16+
17+
setTimeout(() => {
18+
actual = expected
19+
}, 200)
20+
21+
let complete = false
22+
23+
jest.advanceTimersByTime(200)
24+
25+
await waitFor(() => {
26+
expect(actual).toBe(expected)
27+
complete = true
28+
})
29+
30+
expect(complete).toBe(true)
31+
})
32+
33+
test('should wait for arbitrary expectation to pass when using runOnlyPendingTimers()', async () => {
34+
const { waitFor } = renderHook(() => null)
35+
36+
let actual = 0
37+
const expected = 1
38+
39+
setTimeout(() => {
40+
actual = expected
41+
}, 200)
42+
43+
let complete = false
44+
45+
jest.runOnlyPendingTimers()
46+
47+
await waitFor(() => {
48+
expect(actual).toBe(expected)
49+
complete = true
50+
})
51+
52+
expect(complete).toBe(true)
53+
})
54+
})
55+
})
56+
57+
// eslint-disable-next-line jest/no-export
58+
export {}

0 commit comments

Comments
 (0)