-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseTimeout.spec.ts
173 lines (129 loc) · 4.01 KB
/
useTimeout.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import {
renderHook,
RenderHookResult,
act,
cleanup
} from '@testing-library/react-hooks'
import { FnReturnType } from '../src/useTimeout'
import { useTimeout } from '../src'
// return fn to spy and renderHook result
const setup = (
fn = jest.fn(),
ms = 5,
auto = false
): [
Function,
RenderHookResult<
{ cb: Function; delay: number; autoStart: boolean },
FnReturnType
>
] => {
return [
fn,
renderHook(
({ cb, delay = 5, autoStart = false }) =>
useTimeout(cb, delay, autoStart),
{
initialProps: { cb: fn, delay: ms, autoStart: auto }
}
)
]
}
const advanceTime = (time = 0) =>
act(() => {
jest.advanceTimersByTime(time)
})
describe('useTimeout', () => {
beforeEach(() => {
jest.useFakeTimers()
})
afterEach(() => {
jest.clearAllTimers()
cleanup()
})
it('should be defined', () => {
expect(useTimeout).toBeDefined()
})
it('should return two functions and a boolean', () => {
// eslint-disable-next-line @typescript-eslint/no-empty-function
const { result } = renderHook(() => useTimeout(() => {}, 5, true))
expect(result.current.length).toBe(3)
expect(typeof result.current[0]).toBe('function')
expect(typeof result.current[1]).toBe('function')
expect(typeof result.current[2]).toBe('boolean')
})
it('should not run callback on setup when autoStart is false', () => {
const [callback] = setup()
expect(callback).not.toHaveBeenCalled()
advanceTime(5)
expect(callback).not.toHaveBeenCalled()
})
it('should run callback, after delay has finished, on setup when autoStart is true', () => {
const [callback] = setup(undefined, undefined, true)
// need add if not setState warning will be displayed
advanceTime(5)
expect(callback).toHaveBeenCalledTimes(1)
})
it('first returned function should restart timeout', () => {
const [cb, hook] = setup(undefined, undefined, true)
advanceTime(3)
expect(cb).not.toHaveBeenCalled()
const [restart] = hook.result.current
act(() => restart())
advanceTime(2)
expect(cb).not.toHaveBeenCalled()
advanceTime(3)
expect(cb).toHaveBeenCalled()
})
it('second returned function should clear timeout', () => {
const [cb, hook] = setup(undefined, undefined, true)
advanceTime(2)
expect(cb).not.toHaveBeenCalled()
const [, clear] = hook.result.current
act(() => clear())
advanceTime(3)
expect(cb).not.toHaveBeenCalled()
})
it("third return value should return timeout's state in every moment", async () => {
const [, hook] = setup()
const [restart, , isActive] = hook.result.current
expect(isActive).toBe(null)
act(() => restart())
expect(hook.result.current[2]).toBe(false)
advanceTime(5)
expect(hook.result.current[2]).toBe(true)
act(() => hook.result.current[1]())
expect(hook.result.current[2]).toBe(null)
})
it('should restart timeout on delay change when autoStart is true', () => {
const [cb, hook] = setup(undefined, undefined, true)
advanceTime(2)
expect(cb).not.toHaveBeenCalledTimes(1)
hook.rerender({ delay: 7, cb, autoStart: true })
advanceTime(3)
expect(cb).not.toHaveBeenCalledTimes(1)
advanceTime(4)
expect(cb).toHaveBeenCalledTimes(1)
})
it('should not restart timeout on delay change when autoStart is false', () => {
const [cb, hook] = setup(undefined, 5, false)
advanceTime(2)
expect(cb).not.toHaveBeenCalledTimes(1)
hook.rerender({ delay: 7, cb, autoStart: false })
advanceTime(3)
expect(cb).not.toHaveBeenCalledTimes(1)
advanceTime(4)
expect(cb).not.toHaveBeenCalledTimes(1)
})
it('should not restart on callback change', () => {
const [cb, hook] = setup(undefined, 5)
const [restart] = hook.result.current
act(() => restart())
advanceTime(2)
const cb2 = jest.fn()
hook.rerender({ delay: 5, autoStart: false, cb: cb2 })
advanceTime(3)
expect(cb).not.toHaveBeenCalled()
expect(cb2).toHaveBeenCalledTimes(1)
})
})