-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathuseTimeoutFn.test.ts
137 lines (105 loc) Β· 3.34 KB
/
useTimeoutFn.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
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
import { act, renderHook, RenderHookResult } from '@testing-library/react-hooks';
import { useTimeoutFn } from '../src';
import { UseTimeoutFnReturn } from '../src/useTimeoutFn';
describe('useTimeoutFn', () => {
beforeAll(() => {
jest.useFakeTimers();
});
afterEach(() => {
jest.clearAllTimers();
});
afterAll(() => {
jest.useRealTimers();
});
it('should be defined', () => {
expect(useTimeoutFn).toBeDefined();
});
it('should return three functions', () => {
const hook = renderHook(() => useTimeoutFn(() => {}, 5));
expect(hook.result.current.length).toBe(3);
expect(typeof hook.result.current[0]).toBe('function');
expect(typeof hook.result.current[1]).toBe('function');
expect(typeof hook.result.current[2]).toBe('function');
});
function getHook(
ms: number = 5,
fn: Function = jest.fn()
): [Function, RenderHookResult<{ delay: number; cb: Function }, UseTimeoutFnReturn>] {
return [
fn,
renderHook(({ delay = 5, cb }) => useTimeoutFn(cb, delay), {
initialProps: { delay: ms, cb: fn },
}),
];
}
it('should call passed function after given amount of time', () => {
const [spy] = getHook();
expect(spy).not.toHaveBeenCalled();
jest.advanceTimersByTime(5);
expect(spy).toHaveBeenCalledTimes(1);
});
it('should cancel function call on unmount', () => {
const [spy, hook] = getHook();
expect(spy).not.toHaveBeenCalled();
hook.unmount();
jest.advanceTimersByTime(5);
expect(spy).not.toHaveBeenCalled();
});
it('first function should return actual state of timeout', () => {
let [, hook] = getHook();
let [isReady] = hook.result.current;
expect(isReady()).toBe(false);
hook.unmount();
expect(isReady()).toBe(null);
[, hook] = getHook();
[isReady] = hook.result.current;
jest.advanceTimersByTime(5);
expect(isReady()).toBe(true);
});
it('second function should cancel timeout', () => {
const [spy, hook] = getHook();
const [isReady, cancel] = hook.result.current;
expect(spy).not.toHaveBeenCalled();
expect(isReady()).toBe(false);
act(() => {
cancel();
});
jest.advanceTimersByTime(5);
expect(spy).not.toHaveBeenCalled();
expect(isReady()).toBe(null);
});
it('third function should reset timeout', () => {
const [spy, hook] = getHook();
const [isReady, cancel, reset] = hook.result.current;
expect(isReady()).toBe(false);
act(() => {
cancel();
});
jest.advanceTimersByTime(5);
expect(isReady()).toBe(null);
act(() => {
reset();
});
expect(isReady()).toBe(false);
jest.advanceTimersByTime(5);
expect(spy).toHaveBeenCalledTimes(1);
expect(isReady()).toBe(true);
});
it('should reset timeout on delay change', () => {
const [spy, hook] = getHook(50);
expect(spy).not.toHaveBeenCalled();
hook.rerender({ delay: 5, cb: spy });
jest.advanceTimersByTime(5);
expect(spy).toHaveBeenCalledTimes(1);
});
it('should NOT reset timeout on function change', () => {
const [spy, hook] = getHook(50);
jest.advanceTimersByTime(25);
expect(spy).not.toHaveBeenCalled();
const spy2 = jest.fn();
hook.rerender({ delay: 50, cb: spy2 });
jest.advanceTimersByTime(25);
expect(spy).not.toHaveBeenCalled();
expect(spy2).toHaveBeenCalledTimes(1);
});
});