-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
use-timeout.test.ts
106 lines (79 loc) · 2.99 KB
/
use-timeout.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
import { renderHook } from '@testing-library/react';
import { act } from 'react-dom/test-utils';
import { useTimeout } from './use-timeout';
const defaultTimeout = 2000;
const callback = jest.fn();
const setupTimer = (timeout: number = defaultTimeout) => ({
timeout,
advanceTimerToNextTick: () => jest.advanceTimersByTime(timeout),
});
describe('@mantine/hooks/use-timeout', () => {
beforeAll(() => {
jest.useFakeTimers();
jest.spyOn(global, 'setTimeout');
jest.spyOn(global, 'clearTimeout');
});
afterEach(() => {
jest.clearAllMocks();
jest.clearAllTimers();
});
afterAll(() => {
jest.useRealTimers();
});
it('initialize', () => {
const hook = renderHook(() => useTimeout(callback, defaultTimeout, { autoInvoke: false }));
const { start, clear } = hook.result.current;
expect(typeof start).toBe('function');
expect(typeof clear).toBe('function');
});
it('callback should NOT fire before calling start function, if autoInvoke is false', () => {
const { timeout, advanceTimerToNextTick } = setupTimer();
renderHook(() => useTimeout(callback, timeout, { autoInvoke: false }));
advanceTimerToNextTick();
expect(callback).not.toHaveBeenCalled();
expect(setTimeout).not.toHaveBeenCalled();
expect(clearTimeout).not.toHaveBeenCalled();
});
it('callback should fire after calling start function, if autoInvoke is false', () => {
const { timeout, advanceTimerToNextTick } = setupTimer();
const hook = renderHook(() => useTimeout(callback, timeout, { autoInvoke: false }));
act(() => {
hook.result.current.start();
});
advanceTimerToNextTick();
expect(callback).toHaveBeenCalled();
expect(setTimeout).toHaveBeenCalled();
});
it('callback should fire without calling start when autoInvoke is true', () => {
const { timeout, advanceTimerToNextTick } = setupTimer(null);
renderHook(() => useTimeout(callback, timeout, { autoInvoke: true }));
advanceTimerToNextTick();
expect(callback).toBeCalled();
expect(setTimeout).toHaveBeenCalled();
});
it('timeout is cleared on calling clear', () => {
const { timeout, advanceTimerToNextTick } = setupTimer(10);
const hook = renderHook(() => useTimeout(callback, timeout, { autoInvoke: false }));
act(() => {
hook.result.current.start();
});
act(() => {
hook.result.current.clear();
});
advanceTimerToNextTick();
expect(callback).not.toBeCalled();
expect(setTimeout).toHaveBeenCalled();
expect(clearTimeout).toHaveBeenCalled();
});
it('start function passes parameters to callback', () => {
const { timeout, advanceTimerToNextTick } = setupTimer(10);
const hook = renderHook(() => useTimeout(callback, timeout));
const MOCK_CALLBACK_VALUE = 'MOCK_CALLBACK_VALUE';
act(() => {
hook.result.current.start(MOCK_CALLBACK_VALUE);
});
advanceTimerToNextTick();
expect(setTimeout).toHaveBeenCalled();
expect(callback).toBeCalledWith([MOCK_CALLBACK_VALUE]);
});
});