forked from ant-design/ant-design
-
Notifications
You must be signed in to change notification settings - Fork 1
/
delay-timer.test.tsx
89 lines (75 loc) · 2.82 KB
/
delay-timer.test.tsx
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
import React, { useState } from 'react';
import { act } from 'react-dom/test-utils';
import { fireEvent, render } from '../../../tests/utils';
import Button from '../button';
const specialDelay = 9529;
const Content = () => {
const [loading, setLoading] = useState(false);
const toggleLoading = () => {
setLoading(!loading);
};
const [visible, setVisible] = useState(true);
const toggleVisible = () => {
setVisible(!visible);
};
return (
<div>
<button type="button" id="toggle_loading" onClick={toggleLoading}>
Toggle Loading
</button>
<button type="button" id="toggle_visible" onClick={toggleVisible}>
Toggle Visible
</button>
{visible && <Button type="text" loading={loading ? { delay: specialDelay } : false} />}
</div>
);
};
it('Delay loading timer in Button component', () => {
const otherTimer: any = 9528;
jest.spyOn(window, 'setTimeout').mockReturnValue(otherTimer);
jest.restoreAllMocks();
const wrapper = render(<Content />);
const btnTimer: any = 9527;
jest.spyOn(window, 'setTimeout').mockReturnValue(btnTimer);
jest.spyOn(window, 'clearTimeout');
const setTimeoutMock = window.setTimeout as any as jest.Mock;
const clearTimeoutMock = window.clearTimeout as any as jest.Mock;
// other component may call setTimeout or clearTimeout
const setTimeoutCount = () => {
const items = setTimeoutMock.mock.calls.filter(item => item[1] === specialDelay);
return items.length;
};
const clearTimeoutCount = () => {
const items = clearTimeoutMock.mock.calls.filter(item => item[0] === btnTimer);
return items.length;
};
// switch loading state to true
fireEvent.click(wrapper.container.querySelectorAll('#toggle_loading')[0]);
expect(setTimeoutCount()).toBe(1);
expect(clearTimeoutCount()).toBe(0);
// trigger timer handler
act(() => {
setTimeoutMock.mock.calls[0][0]();
});
expect(setTimeoutCount()).toBe(1);
expect(clearTimeoutCount()).toBe(0);
// switch loading state to false
fireEvent.click(wrapper.container.querySelectorAll('#toggle_loading')[0]);
expect(setTimeoutCount()).toBe(1);
expect(clearTimeoutCount()).toBe(0);
// switch loading state to true
fireEvent.click(wrapper.container.querySelectorAll('#toggle_loading')[0]);
expect(setTimeoutCount()).toBe(2);
expect(clearTimeoutCount()).toBe(0);
// switch loading state to false
fireEvent.click(wrapper.container.querySelectorAll('#toggle_loading')[0]);
expect(setTimeoutCount()).toBe(2);
expect(clearTimeoutCount()).toBe(1);
// switch loading state to true
fireEvent.click(wrapper.container.querySelectorAll('#toggle_loading')[0]);
// remove Button component
fireEvent.click(wrapper.container.querySelectorAll('#toggle_visible')[0]);
expect(setTimeoutCount()).toBe(3);
expect(clearTimeoutCount()).toBe(2);
jest.restoreAllMocks();
});