-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseCounter.test.ts
50 lines (34 loc) · 1.24 KB
/
useCounter.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
import useCounter from './useCounter'
import { renderHook, act } from '@testing-library/react-hooks'
jest.useFakeTimers()
test('Updates by time', () => {
const delay = 100
const { result } = renderHook(() => useCounter(delay))
act(() => jest.advanceTimersByTime(delay))
expect(result.current).toBe(1)
})
test('Updates by next timer', () => {
const { result } = renderHook(() => useCounter(100))
act(() => jest.advanceTimersToNextTimer())
expect(result.current).toBe(1)
})
test('Updates by next timer after awaiting next update', async () => {
const { result, waitForNextUpdate } = renderHook(() => useCounter(100))
act(() => jest.advanceTimersToNextTimer())
await waitForNextUpdate()
expect(result.current).toBe(1)
})
test('Updates by next timer inside setImmediate', async () => {
const { result, waitForNextUpdate } = renderHook(() => useCounter(100))
setImmediate(() => {
act(() => jest.advanceTimersToNextTimer())
})
await waitForNextUpdate()
expect(result.current).toBe(1)
})
test('Updates by next timer after tiny delay', () => {
const { result } = renderHook(() => useCounter(100))
act(() => jest.advanceTimersByTime(1))
act(() => jest.advanceTimersToNextTimer())
expect(result.current).toBe(1)
})