-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseInterval.spec.ts
122 lines (92 loc) · 2.85 KB
/
useInterval.spec.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import {
cleanup,
renderHook,
RenderHookResult
} from '@testing-library/react-hooks'
import { useInterval } from '../src'
const setup = (
fn: () => void = jest.fn(),
ms: number | null = 0
): [
Function,
RenderHookResult<{ cb: Function; delay: number | null }, void>
] => {
return [
fn,
renderHook(({ cb, delay = 0 }) => useInterval(cb, delay), {
initialProps: { cb: fn, delay: ms }
})
]
}
describe('useInterval', () => {
beforeEach(() => {
jest.useFakeTimers()
})
afterEach(() => {
jest.clearAllTimers()
cleanup()
})
it('should init hook if delay is null', () => {
const [cb, hook] = setup(undefined, null)
expect(hook.result.current).toBeUndefined()
expect(setInterval).not.toHaveBeenCalled()
expect(cb).not.toHaveBeenCalled()
})
it('should init hook with custom delay', () => {
const [cb, hook] = setup(undefined, 100)
expect(hook.result.current).toBeUndefined()
expect(setInterval).toHaveBeenCalled()
expect(cb).not.toHaveBeenCalled()
})
it('should start with 0 if delay is not provided', () => {
const [, hook] = setup()
expect(hook.result.current).toBeUndefined()
expect(setInterval).toHaveBeenCalledTimes(1)
expect(setInterval).toHaveBeenCalledWith(expect.any(Function), 0)
})
it('should execute callback repeatedly with custom delay', () => {
const [cb] = setup(undefined, 100)
expect(cb).not.toHaveBeenCalled()
jest.advanceTimersByTime(99)
expect(cb).not.toHaveBeenCalled()
jest.advanceTimersByTime(1)
expect(cb).toHaveBeenCalledTimes(1)
jest.advanceTimersByTime(100)
expect(cb).toHaveBeenCalledTimes(2)
})
it('should not clear interval if callback changes', () => {
const cb2 = jest.fn()
const [cb, hook] = setup(undefined, 100)
expect(cb).not.toHaveBeenCalled()
jest.advanceTimersByTime(50)
expect(cb).not.toHaveBeenCalled()
hook.rerender({ cb: cb2, delay: 100 })
jest.advanceTimersByTime(50)
expect(cb).not.toHaveBeenCalled()
expect(cb2).toHaveBeenCalled()
})
it('should clear interval on delay change', () => {
const cb = jest.fn()
let delay = 100
const { rerender } = renderHook(() => useInterval(cb, delay))
expect(cb).not.toHaveBeenCalled()
jest.advanceTimersByTime(50)
delay = 100 + 20
rerender()
expect(clearInterval).toHaveBeenCalled()
jest.advanceTimersByTime(50)
expect(cb).not.toHaveBeenCalled()
jest.advanceTimersByTime(50 + 20)
expect(cb).toHaveBeenCalled()
})
it('should clear interval on unmount', () => {
const cb = jest.fn()
const { unmount } = renderHook(() => useInterval(cb, 100))
expect(setInterval).toHaveBeenCalled()
expect(cb).not.toHaveBeenCalled()
jest.advanceTimersByTime(50)
unmount()
expect(clearInterval).toHaveBeenCalled()
expect(cb).not.toHaveBeenCalled()
})
})