forked from ant-design/ant-design
-
Notifications
You must be signed in to change notification settings - Fork 0
/
focusTest.tsx
152 lines (129 loc) · 4.34 KB
/
focusTest.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
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
import React from 'react';
import { sleep, render, fireEvent } from '../utils';
// eslint-disable-next-line jest/no-export
export default function focusTest(
Component: React.ComponentType<any>,
{ refFocus = false, testLib = false, blurDelay = 0 } = {},
) {
describe('focus and blur', () => {
let focused = false;
let blurred = false;
const mockFocus = jest.spyOn(HTMLElement.prototype, 'focus');
const mockBlur = jest.spyOn(HTMLElement.prototype, 'blur');
beforeAll(() => {
if (refFocus) {
mockFocus.mockImplementation(() => {
focused = true;
});
mockBlur.mockImplementation(() => {
blurred = true;
});
}
});
// ==================== React Test Lib ====================
if (testLib) {
it('Test: focus() and onFocus', () => {
const handleFocus = jest.fn();
const ref = React.createRef<any>();
const { unmount } = render(<Component onFocus={handleFocus} ref={ref} />);
ref.current.focus();
expect(handleFocus).toHaveBeenCalled();
unmount();
});
it('Test: blur() and onBlur', async () => {
const handleBlur = jest.fn();
const ref = React.createRef<any>();
const { unmount } = render(<Component ref={ref} onBlur={handleBlur} />);
ref.current.focus();
ref.current.blur();
expect(handleBlur).toHaveBeenCalled();
unmount();
});
it('Test: autoFocus', () => {
const handleFocus = jest.fn();
const { unmount } = render(<Component autoFocus onFocus={handleFocus} />);
expect(handleFocus).toHaveBeenCalled();
unmount();
});
return;
}
let containerHtml: HTMLElement;
beforeEach(() => {
containerHtml = document.createElement('div');
document.body.appendChild(containerHtml);
focused = false;
blurred = false;
});
afterAll(() => {
mockFocus.mockRestore();
mockBlur.mockRestore();
});
afterEach(() => {
document.body.removeChild(containerHtml);
});
const getElement = (container: { querySelector: Function }) =>
container.querySelector('input') ||
container.querySelector('button') ||
container.querySelector('textarea') ||
container.querySelector('div[tabIndex]');
if (refFocus) {
it('Ref: focus() and onFocus', () => {
const onFocus = jest.fn();
const ref = React.createRef<any>();
const { container } = render(
<div>
<Component onFocus={onFocus} ref={ref} />
</div>,
);
ref.current.focus();
expect(focused).toBeTruthy();
fireEvent.focus(getElement(container));
expect(onFocus).toHaveBeenCalled();
});
it('Ref: blur() and onBlur', async () => {
jest.useRealTimers();
const onBlur = jest.fn();
const ref = React.createRef<any>();
const { container } = render(
<div>
<Component onBlur={onBlur} ref={ref} />
</div>,
);
ref.current.blur();
expect(blurred).toBeTruthy();
fireEvent.blur(getElement(container));
await sleep(blurDelay);
expect(onBlur).toHaveBeenCalled();
});
it('Ref: autoFocus', () => {
const onFocus = jest.fn();
const { container } = render(<Component autoFocus onFocus={onFocus} />);
expect(focused).toBeTruthy();
fireEvent.focus(getElement(container));
expect(onFocus).toHaveBeenCalled();
});
} else {
it('focus() and onFocus', () => {
const handleFocus = jest.fn();
const { container } = render(<Component onFocus={handleFocus} />);
fireEvent.focus(getElement(container));
expect(handleFocus).toHaveBeenCalled();
});
it('blur() and onBlur', async () => {
jest.useRealTimers();
const handleBlur = jest.fn();
const { container } = render(<Component onBlur={handleBlur} />);
fireEvent.focus(getElement(container));
await sleep(0);
fireEvent.blur(getElement(container));
await sleep(0);
expect(handleBlur).toHaveBeenCalled();
});
it('autoFocus', () => {
const handleFocus = jest.fn();
render(<Component autoFocus onFocus={handleFocus} />);
expect(handleFocus).toHaveBeenCalled();
});
}
});
}