-
Notifications
You must be signed in to change notification settings - Fork 196
/
Copy pathSelect.SearchInput.spec.js
309 lines (269 loc) · 9.27 KB
/
Select.SearchInput.spec.js
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/* eslint-disable no-undef */
import React, { useState } from 'react';
import { mount } from 'enzyme';
import { render, fireEvent } from '@testing-library/react';
import TreeSelect, { TreeNode } from '../src';
import KeyCode from '@rc-component/util/lib/KeyCode';
describe('TreeSelect.SearchInput', () => {
it('select item will clean searchInput', () => {
const onSearch = jest.fn();
const wrapper = mount(
<TreeSelect onSearch={onSearch} open>
<TreeNode value="test" />
</TreeSelect>,
);
wrapper.search('test');
expect(onSearch).toHaveBeenCalledWith('test');
onSearch.mockReset();
wrapper.selectNode();
expect(onSearch).not.toHaveBeenCalled();
expect(wrapper.find('input').first().props().value).toBeFalsy();
});
it('expandedKeys', () => {
const wrapper = mount(
<TreeSelect
open
showSearch
treeExpandedKeys={['bamboo', 'light']}
treeData={[
{
title: 'bamboo',
value: 'bamboo',
children: [{ title: '111', value: '111' }],
},
{
title: 'light',
value: 'light',
children: [{ title: '222', value: '222' }],
},
]}
/>,
);
expect(wrapper.find('NodeList').prop('expandedKeys')).toEqual(['bamboo', 'light']);
function search(value) {
wrapper.find('input').first().simulate('change', { target: { value } });
wrapper.update();
}
function listProps() {
return wrapper.find('NodeList').props();
}
// Clean up
search('bambooA');
// Return back
search('bamboo');
// Back to default
search('');
expect(listProps().expandedKeys).toEqual(['bamboo', 'light']);
});
it('not trigger loadData when clearing the search', () => {
let called = 0;
const handleLoadData = jest.fn();
const Demo = () => {
const [value, setValue] = useState();
const [treeData, setTreeData] = useState([]);
const addDefaultTreeData = () => {
setTreeData([
{ id: 1, pId: 0, value: '1', title: 'Expand to load' },
{ id: 2, pId: 0, value: '2', title: 'Expand to load' },
{ id: 3, pId: 0, value: '3', title: 'Tree Node', isLeaf: true },
]);
};
const genTreeNode = (parentId, isLeaf = false) => {
const random = Math.random().toString(36).substring(2, 6);
return {
id: random,
pId: parentId,
value: random,
title: isLeaf ? 'Tree Node' : 'Expand to load',
isLeaf,
};
};
const onLoadData = ({ id, ...rest }) =>
new Promise(resolve => {
called += 1;
handleLoadData({ id, ...rest });
setTreeData(
treeData.concat([genTreeNode(id, false), genTreeNode(id, true), genTreeNode(id, true)]),
);
resolve(undefined);
});
const onChange = newValue => {
setValue(newValue);
};
return (
<>
<TreeSelect
treeDataSimpleMode
value={value}
placeholder="Please select"
onChange={onChange}
loadData={onLoadData}
treeData={treeData}
treeNodeFilterProp="title"
showSearch
/>
<button onClick={addDefaultTreeData}>设置数据</button>
</>
);
};
const wrapper = mount(<Demo />);
expect(wrapper.find('button').length).toBe(1);
expect(handleLoadData).not.toHaveBeenCalled();
function search(value) {
wrapper.find('input').first().simulate('change', { target: { value } });
wrapper.update();
}
search('Tree Node');
expect(handleLoadData).not.toHaveBeenCalled();
search('');
expect(handleLoadData).not.toHaveBeenCalled();
expect(wrapper.find('.rc-tree-select-empty').length).toBe(1);
wrapper.find('button').simulate('click');
expect(wrapper.find('.rc-tree-select-empty').length).toBe(0);
expect(wrapper.find('.rc-tree-select-tree').length).toBe(1);
search('Tree Node');
expect(handleLoadData).not.toHaveBeenCalled();
search('');
expect(handleLoadData).not.toHaveBeenCalled();
expect(called).toBe(0);
search('ex');
const nodes = wrapper.find(`[title="${'Expand to load'}"]`).hostNodes();
nodes.first().simulate('click');
expect(called).toBe(0); // should not trrigger all nodes to load data
});
it('should trrigger `loadData` when click node', () => {
let called = 0;
const Demo = () => {
const [value, setValue] = useState();
const onLoadData = ({ id, ...rest }) =>
new Promise(resolve => {
called += 1;
resolve(undefined);
});
const onChange = newValue => {
setValue(newValue);
};
return (
<TreeSelect
treeDataSimpleMode
value={value}
placeholder="Please select"
onChange={onChange}
loadData={onLoadData}
treeData={[
{ id: 1, pId: 0, value: '1', title: 'Expand to load' },
{ id: 2, pId: 0, value: '2', title: 'Expand to load' },
{ id: 3, pId: 0, value: '3', title: 'Tree Node', isLeaf: true },
]}
treeNodeFilterProp="title"
treeExpandAction="click"
showSearch
/>
);
};
const wrapper = mount(<Demo />);
function search(value) {
wrapper.find('input').first().simulate('change', { target: { value } });
wrapper.update();
}
search('ex');
const nodes = wrapper.find(`[title="${'Expand to load'}"]`).hostNodes();
nodes.first().simulate('click');
expect(called).toBe(1);
});
describe('keyboard events', () => {
it('should select first matched node when press enter', () => {
const onSelect = jest.fn();
const { getByRole } = render(
<TreeSelect
showSearch
open
onSelect={onSelect}
treeData={[
{ value: '1', label: '1' },
{ value: '2', label: '2', disabled: true },
{ value: '3', label: '3' },
]}
/>,
);
// Search and press enter, should select first matched non-disabled node
const input = getByRole('combobox');
fireEvent.change(input, { target: { value: '1' } });
fireEvent.keyDown(input, { keyCode: KeyCode.ENTER });
fireEvent.keyUp(input, { keyCode: KeyCode.ENTER });
expect(onSelect).toHaveBeenCalledWith('1', expect.anything());
onSelect.mockReset();
// Search disabled node and press enter, should not select
fireEvent.change(input, { target: { value: '2' } });
fireEvent.keyDown(input, { keyCode: KeyCode.ENTER });
fireEvent.keyUp(input, { keyCode: KeyCode.ENTER });
expect(onSelect).not.toHaveBeenCalled();
onSelect.mockReset();
// Search and press enter, should select first matched non-disabled node
fireEvent.change(input, { target: { value: '3' } });
fireEvent.keyDown(input, { keyCode: KeyCode.ENTER });
fireEvent.keyUp(input, { keyCode: KeyCode.ENTER });
expect(onSelect).toHaveBeenCalledWith('3', expect.anything());
});
it('should not select node when no matches found', () => {
const onSelect = jest.fn();
const { getByRole } = render(
<TreeSelect
showSearch
onSelect={onSelect}
open
treeData={[
{ value: '1', label: '1' },
{ value: '2', label: '2' },
]}
/>,
);
// Search non-existent value and press enter, should not select any node
const input = getByRole('combobox');
fireEvent.change(input, { target: { value: 'not-exist' } });
fireEvent.keyDown(input, { keyCode: KeyCode.ENTER });
expect(onSelect).not.toHaveBeenCalled();
});
it('should ignore enter press when all matched nodes are disabled', () => {
const onSelect = jest.fn();
const { getByRole } = render(
<TreeSelect
showSearch
onSelect={onSelect}
open
treeData={[
{ value: '1', label: '1', disabled: true },
{ value: '2', label: '2', disabled: true },
]}
/>,
);
// When all matched nodes are disabled, press enter should not select any node
const input = getByRole('combobox');
fireEvent.change(input, { target: { value: '1' } });
fireEvent.keyDown(input, { keyCode: KeyCode.ENTER });
expect(onSelect).not.toHaveBeenCalled();
});
it('should activate first matched node when searching', () => {
const { getByRole, container } = render(
<TreeSelect
showSearch
open
treeData={[
{ value: '1', label: '1' },
{ value: '2', label: '2', disabled: true },
{ value: '3', label: '3' },
]}
/>,
);
// When searching, first matched non-disabled node should be activated
const input = getByRole('combobox');
fireEvent.change(input, { target: { value: '1' } });
expect(container.querySelector('.rc-tree-select-tree-treenode-active')).toHaveTextContent(
'1',
);
// Should skip disabled nodes
fireEvent.change(input, { target: { value: '2' } });
expect(container.querySelectorAll('.rc-tree-select-tree-treenode-active')).toHaveLength(0);
});
});
});