@@ -72,60 +72,143 @@ describe('Tree Props', () => {
72
72
expect ( renderToJson ( withOpenIcon ) ) . toMatchSnapshot ( ) ;
73
73
} ) ;
74
74
75
- // selectable
76
- it ( 'selectable' , ( ) => {
77
- // With Selectable
75
+ describe ( 'selectable' , ( ) => {
76
+ // selectable - false
77
+ it ( 'without selectable' , ( ) => {
78
+ const handleOnSelect = jest . fn ( ) ;
79
+ const handleOnCheck = jest . fn ( ) ;
80
+
81
+ const withoutSelectableBase = (
82
+ < Tree onSelect = { handleOnSelect } defaultExpandedKeys = { [ '0-0' ] } selectable = { false } >
83
+ < TreeNode key = "0-0" >
84
+ < TreeNode key = "0-0-0" />
85
+ </ TreeNode >
86
+ </ Tree >
87
+ ) ;
88
+
89
+ expect ( renderToJson ( render ( withoutSelectableBase ) ) ) . toMatchSnapshot ( ) ;
90
+
91
+ const withoutSelectable = mount ( withoutSelectableBase ) ;
92
+ const parentNode = withoutSelectable . find ( TreeNode ) . first ( ) ;
93
+ const targetNode = parentNode . find ( TreeNode ) . last ( ) ;
94
+
95
+ targetNode . find ( '.rc-tree-node-content-wrapper' ) . simulate ( 'click' ) ;
96
+
97
+ expect ( handleOnSelect ) . not . toBeCalled ( ) ;
98
+ expect ( handleOnCheck ) . not . toBeCalled ( ) ; // Will test in checkable
99
+ } ) ;
100
+
101
+ // selectable - true
102
+ it ( 'with selectable' , ( ) => {
103
+ const handleOnSelect = jest . fn ( ) ;
104
+
105
+ const withSelectableBase = (
106
+ < Tree onSelect = { handleOnSelect } defaultExpandedKeys = { [ '0-0' ] } >
107
+ < TreeNode key = "0-0" >
108
+ < TreeNode key = "0-0-0" />
109
+ </ TreeNode >
110
+ </ Tree >
111
+ ) ;
112
+
113
+ expect ( renderToJson ( render ( withSelectableBase ) ) ) . toMatchSnapshot ( ) ;
114
+
115
+ const withSelectable = mount ( withSelectableBase ) ;
116
+ const parentNode = withSelectable . find ( TreeNode ) . first ( ) ;
117
+ const targetNode = parentNode . find ( TreeNode ) . last ( ) ;
118
+
119
+ // Select leaf
120
+ targetNode . find ( '.rc-tree-node-content-wrapper' ) . simulate ( 'click' ) ;
121
+
122
+ // traverseTreeNodes loops origin TreeNode and
123
+ // onSelect trigger on `cloneElement` which is not the same instance
124
+ // TODO: use context instead of `cloneElement` to reduce
125
+ expect ( handleOnSelect ) . toBeCalledWith ( [ '0-0-0' ] , {
126
+ event : 'select' ,
127
+ selected : true ,
128
+ node : targetNode . instance ( ) ,
129
+ selectedNodes : [ parentNode . props ( ) . children ] ,
130
+ } ) ;
131
+
132
+ // Unselect leaf
133
+ targetNode . find ( '.rc-tree-node-content-wrapper' ) . simulate ( 'click' ) ;
134
+ expect ( handleOnSelect ) . toBeCalledWith ( [ ] , {
135
+ event : 'select' ,
136
+ selected : false ,
137
+ node : targetNode . instance ( ) ,
138
+ selectedNodes : [ ] ,
139
+ } ) ;
140
+
141
+ // Select leaf and then parent
142
+ targetNode . find ( '.rc-tree-node-content-wrapper' ) . simulate ( 'click' ) ;
143
+ expect ( handleOnSelect ) . toBeCalledWith ( [ '0-0-0' ] , {
144
+ event : 'select' ,
145
+ selected : true ,
146
+ node : targetNode . instance ( ) ,
147
+ selectedNodes : [ parentNode . props ( ) . children ] ,
148
+ } ) ;
149
+
150
+ parentNode . find ( '.rc-tree-node-content-wrapper' ) . first ( ) . simulate ( 'click' ) ;
151
+ expect ( handleOnSelect ) . toBeCalledWith ( [ '0-0' ] , {
152
+ event : 'select' ,
153
+ selected : true ,
154
+ node : parentNode . instance ( ) ,
155
+ selectedNodes : [ withSelectable . find ( Tree ) . props ( ) . children ] ,
156
+ } ) ;
157
+ } ) ;
158
+ } ) ;
159
+
160
+ // multiple
161
+ it ( 'multiple' , ( ) => {
162
+ // TODO: Placeholder
163
+ } ) ;
164
+
165
+ // checkable
166
+ it ( 'checkable' , ( ) => {
78
167
const handleOnSelect = jest . fn ( ) ;
79
- const withSelectableBase = (
80
- < Tree onSelect = { handleOnSelect } defaultExpandedKeys = { [ '0-0' ] } >
168
+ const handleOnCheck = jest . fn ( ) ;
169
+
170
+ const withCheckableBase = (
171
+ < Tree
172
+ onSelect = { handleOnSelect }
173
+ onCheck = { handleOnCheck }
174
+ defaultExpandedKeys = { [ '0-0' ] }
175
+ checkable
176
+ >
81
177
< TreeNode key = "0-0" >
82
178
< TreeNode key = "0-0-0" />
83
179
</ TreeNode >
84
180
</ Tree >
85
181
) ;
86
182
87
- expect ( renderToJson ( render ( withSelectableBase ) ) ) . toMatchSnapshot ( ) ;
183
+ expect ( renderToJson ( render ( withCheckableBase ) ) ) . toMatchSnapshot ( ) ;
88
184
89
- const withSelectable = mount ( withSelectableBase ) ;
90
- const parentNode = withSelectable . find ( TreeNode ) . first ( ) ;
185
+ const withCheckable = mount ( withCheckableBase ) ;
186
+ const parentNode = withCheckable . find ( TreeNode ) . first ( ) ;
91
187
const targetNode = parentNode . find ( TreeNode ) . last ( ) ;
92
188
93
- // Check leaf
189
+ // Click leaf
94
190
targetNode . find ( '.rc-tree-node-content-wrapper' ) . simulate ( 'click' ) ;
95
-
96
- // traverseTreeNodes loops origin TreeNode and
97
- // onSelect trigger on `cloneElement` which is not the same instance
98
191
expect ( handleOnSelect ) . toBeCalledWith ( [ '0-0-0' ] , {
99
192
event : 'select' ,
100
193
selected : true ,
101
194
node : targetNode . instance ( ) ,
102
195
selectedNodes : [ parentNode . props ( ) . children ] ,
103
196
} ) ;
197
+ expect ( handleOnCheck ) . not . toBeCalled ( ) ;
198
+ expect ( handleOnSelect ) . toBeCalled ( ) ;
104
199
105
- // Uncheck leaf
106
- targetNode . find ( '.rc-tree-node-content-wrapper' ) . simulate ( 'click' ) ;
107
- expect ( handleOnSelect ) . toBeCalledWith ( [ ] , {
108
- event : 'select' ,
109
- selected : false ,
110
- node : targetNode . instance ( ) ,
111
- selectedNodes : [ ] ,
112
- } ) ;
200
+ handleOnCheck . mockReset ( ) ;
201
+ handleOnSelect . mockReset ( ) ;
113
202
114
- // Check leaf with parent
115
- targetNode . find ( '.rc-tree-node-content-wrapper' ) . simulate ( 'click' ) ;
116
- expect ( handleOnSelect ) . toBeCalledWith ( [ '0-0-0' ] , {
117
- event : 'select' ,
118
- selected : true ,
119
- node : targetNode . instance ( ) ,
120
- selectedNodes : [ parentNode . props ( ) . children ] ,
121
- } ) ;
203
+ // Click checkbox
204
+ targetNode . find ( '.rc-tree-checkbox' ) . simulate ( 'click' ) ;
122
205
123
- parentNode . find ( '.rc-tree-node-content-wrapper' ) . first ( ) . simulate ( 'click' ) ;
124
- expect ( handleOnSelect ) . toBeCalledWith ( [ '0-0' ] , {
125
- event : 'select' ,
126
- selected : true ,
127
- node : parentNode . instance ( ) ,
128
- selectedNodes : [ withSelectable . find ( Tree ) . props ( ) . children ] ,
129
- } ) ;
206
+ expect ( handleOnCheck ) . toBeCalledWith ( [ '0-0-0' , '0-0' ] , expect . objectContaining ( {
207
+ event : 'check' ,
208
+ checked : true ,
209
+ node : targetNode . instance ( ) ,
210
+ checkedNodes : [ parentNode . props ( ) . children , withCheckable . find ( Tree ) . props ( ) . children ] ,
211
+ } ) ) ;
212
+ expect ( handleOnSelect ) . not . toBeCalled ( ) ;
130
213
} ) ;
131
214
} ) ;
0 commit comments