1
1
/* eslint-disable no-undef */
2
2
import React , { useState } from 'react' ;
3
3
import { mount } from 'enzyme' ;
4
- import TreeSelect , { TreeNode } from '../src' ;
4
+ import TreeSelect , { TreeNode , TreeSelectProps } from '../src' ;
5
5
6
6
describe ( 'TreeSelect.SearchInput' , ( ) => {
7
7
it ( 'select item will clean searchInput' , ( ) => {
@@ -19,12 +19,7 @@ describe('TreeSelect.SearchInput', () => {
19
19
20
20
wrapper . selectNode ( ) ;
21
21
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 ( ) ;
28
23
} ) ;
29
24
30
25
it ( 'expandedKeys' , ( ) => {
@@ -51,10 +46,7 @@ describe('TreeSelect.SearchInput', () => {
51
46
expect ( wrapper . find ( 'NodeList' ) . prop ( 'expandedKeys' ) ) . toEqual ( [ 'bamboo' , 'light' ] ) ;
52
47
53
48
function search ( value ) {
54
- wrapper
55
- . find ( 'input' )
56
- . first ( )
57
- . simulate ( 'change' , { target : { value } } ) ;
49
+ wrapper . find ( 'input' ) . first ( ) . simulate ( 'change' , { target : { value } } ) ;
58
50
wrapper . update ( ) ;
59
51
}
60
52
@@ -85,8 +77,8 @@ describe('TreeSelect.SearchInput', () => {
85
77
{ id : 1 , pId : 0 , value : '1' , title : 'Expand to load' } ,
86
78
{ id : 2 , pId : 0 , value : '2' , title : 'Expand to load' } ,
87
79
{ id : 3 , pId : 0 , value : '3' , title : 'Tree Node' , isLeaf : true } ,
88
- ] )
89
- }
80
+ ] ) ;
81
+ } ;
90
82
91
83
const genTreeNode = ( parentId , isLeaf = false ) => {
92
84
const random = Math . random ( ) . toString ( 36 ) . substring ( 2 , 6 ) ;
@@ -100,22 +92,16 @@ describe('TreeSelect.SearchInput', () => {
100
92
} ;
101
93
102
94
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 ) ;
116
102
} ) ;
117
103
118
- const onChange = ( newValue ) => {
104
+ const onChange = newValue => {
119
105
setValue ( newValue ) ;
120
106
} ;
121
107
@@ -130,7 +116,6 @@ describe('TreeSelect.SearchInput', () => {
130
116
treeData = { treeData }
131
117
treeNodeFilterProp = "title"
132
118
showSearch
133
- filterTreeNode = { false }
134
119
/>
135
120
< button onClick = { addDefaultTreeData } > 设置数据</ button >
136
121
</ >
@@ -141,10 +126,7 @@ describe('TreeSelect.SearchInput', () => {
141
126
expect ( handleLoadData ) . not . toHaveBeenCalled ( ) ;
142
127
143
128
function search ( value ) {
144
- wrapper
145
- . find ( 'input' )
146
- . first ( )
147
- . simulate ( 'change' , { target : { value } } ) ;
129
+ wrapper . find ( 'input' ) . first ( ) . simulate ( 'change' , { target : { value } } ) ;
148
130
wrapper . update ( ) ;
149
131
}
150
132
search ( 'Tree Node' ) ;
@@ -165,5 +147,72 @@ describe('TreeSelect.SearchInput', () => {
165
147
search ( '' ) ;
166
148
expect ( handleLoadData ) . not . toHaveBeenCalled ( ) ;
167
149
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 ) ;
168
217
} ) ;
169
218
} ) ;
0 commit comments