Skip to content

Commit 1f79277

Browse files
committed
feat: add test cases
1 parent b817881 commit 1f79277

File tree

1 file changed

+81
-32
lines changed

1 file changed

+81
-32
lines changed

tests/Select.SearchInput.spec.js

+81-32
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable no-undef */
22
import React, { useState } from 'react';
33
import { mount } from 'enzyme';
4-
import TreeSelect, { TreeNode } from '../src';
4+
import TreeSelect, { TreeNode, TreeSelectProps } from '../src';
55

66
describe('TreeSelect.SearchInput', () => {
77
it('select item will clean searchInput', () => {
@@ -19,12 +19,7 @@ describe('TreeSelect.SearchInput', () => {
1919

2020
wrapper.selectNode();
2121
expect(onSearch).not.toHaveBeenCalled();
22-
expect(
23-
wrapper
24-
.find('input')
25-
.first()
26-
.props().value,
27-
).toBeFalsy();
22+
expect(wrapper.find('input').first().props().value).toBeFalsy();
2823
});
2924

3025
it('expandedKeys', () => {
@@ -51,10 +46,7 @@ describe('TreeSelect.SearchInput', () => {
5146
expect(wrapper.find('NodeList').prop('expandedKeys')).toEqual(['bamboo', 'light']);
5247

5348
function search(value) {
54-
wrapper
55-
.find('input')
56-
.first()
57-
.simulate('change', { target: { value } });
49+
wrapper.find('input').first().simulate('change', { target: { value } });
5850
wrapper.update();
5951
}
6052

@@ -85,8 +77,8 @@ describe('TreeSelect.SearchInput', () => {
8577
{ id: 1, pId: 0, value: '1', title: 'Expand to load' },
8678
{ id: 2, pId: 0, value: '2', title: 'Expand to load' },
8779
{ id: 3, pId: 0, value: '3', title: 'Tree Node', isLeaf: true },
88-
])
89-
}
80+
]);
81+
};
9082

9183
const genTreeNode = (parentId, isLeaf = false) => {
9284
const random = Math.random().toString(36).substring(2, 6);
@@ -100,22 +92,16 @@ describe('TreeSelect.SearchInput', () => {
10092
};
10193

10294
const onLoadData = ({ id, ...rest }) =>
103-
new Promise((resolve) => {
104-
setTimeout(() => {
105-
called += 1;
106-
handleLoadData({ id, ...rest });
107-
setTreeData(
108-
treeData.concat([
109-
genTreeNode(id, false),
110-
genTreeNode(id, true),
111-
genTreeNode(id, true),
112-
])
113-
);
114-
resolve(undefined);
115-
}, 300);
95+
new Promise(resolve => {
96+
called += 1;
97+
handleLoadData({ id, ...rest });
98+
setTreeData(
99+
treeData.concat([genTreeNode(id, false), genTreeNode(id, true), genTreeNode(id, true)]),
100+
);
101+
resolve(undefined);
116102
});
117103

118-
const onChange = (newValue) => {
104+
const onChange = newValue => {
119105
setValue(newValue);
120106
};
121107

@@ -130,7 +116,6 @@ describe('TreeSelect.SearchInput', () => {
130116
treeData={treeData}
131117
treeNodeFilterProp="title"
132118
showSearch
133-
filterTreeNode={false}
134119
/>
135120
<button onClick={addDefaultTreeData}>设置数据</button>
136121
</>
@@ -141,10 +126,7 @@ describe('TreeSelect.SearchInput', () => {
141126
expect(handleLoadData).not.toHaveBeenCalled();
142127

143128
function search(value) {
144-
wrapper
145-
.find('input')
146-
.first()
147-
.simulate('change', { target: { value } });
129+
wrapper.find('input').first().simulate('change', { target: { value } });
148130
wrapper.update();
149131
}
150132
search('Tree Node');
@@ -165,5 +147,72 @@ describe('TreeSelect.SearchInput', () => {
165147
search('');
166148
expect(handleLoadData).not.toHaveBeenCalled();
167149
expect(called).toBe(0);
150+
151+
search('ex');
152+
const nodes = wrapper.find(`[title="${'Expand to load'}"]`).hostNodes();
153+
nodes.first().simulate('click');
154+
expect(called).toBe(0); // should not trrigger all nodes to load data
155+
});
156+
157+
it('not trigger loadData when clearing the search', () => {
158+
let called = 0;
159+
const handleLoadData = jest.fn();
160+
const Demo = () => {
161+
const [value, setValue] = useState();
162+
163+
const genTreeNode = (parentId, isLeaf = false) => {
164+
const random = Math.random().toString(36).substring(2, 6);
165+
return {
166+
id: random,
167+
pId: parentId,
168+
value: random,
169+
title: isLeaf ? 'Tree Node' : 'Expand to load',
170+
isLeaf,
171+
};
172+
};
173+
174+
const onLoadData = ({ id, ...rest }) =>
175+
new Promise(resolve => {
176+
called += 1;
177+
handleLoadData({ id, ...rest });
178+
setTreeData(
179+
treeData.concat([genTreeNode(id, false), genTreeNode(id, true), genTreeNode(id, true)]),
180+
);
181+
resolve(undefined);
182+
});
183+
184+
const onChange = newValue => {
185+
setValue(newValue);
186+
};
187+
188+
return (
189+
<TreeSelect
190+
treeDataSimpleMode
191+
value={value}
192+
placeholder="Please select"
193+
onChange={onChange}
194+
loadData={onLoadData}
195+
treeData={[
196+
{ id: 1, pId: 0, value: '1', title: 'Expand to load' },
197+
{ id: 2, pId: 0, value: '2', title: 'Expand to load' },
198+
{ id: 3, pId: 0, value: '3', title: 'Tree Node', isLeaf: true },
199+
]}
200+
treeNodeFilterProp="title"
201+
treeExpandAction="click"
202+
showSearch
203+
/>
204+
);
205+
};
206+
const wrapper = mount(<Demo />);
207+
208+
function search(value) {
209+
wrapper.find('input').first().simulate('change', { target: { value } });
210+
wrapper.update();
211+
}
212+
213+
search('ex');
214+
const nodes = wrapper.find(`[title="${'Expand to load'}"]`).hostNodes();
215+
nodes.first().simulate('click');
216+
expect(called).toBe(1);
168217
});
169218
});

0 commit comments

Comments
 (0)