@@ -23,13 +23,6 @@ const mockData: ITreeNodeItemProps[] = [
23
23
} ,
24
24
] ;
25
25
26
- async function dragExpect ( fn : jest . Mock , result : any ) {
27
- await waitFor ( ( ) => {
28
- expect ( fn ) . toBeCalled ( ) ;
29
- expect ( fn . mock . calls [ 0 ] [ 0 ] ) . toEqual ( result ) ;
30
- } ) ;
31
- }
32
-
33
26
describe ( 'Test the Tree component' , ( ) => {
34
27
afterEach ( cleanup ) ;
35
28
@@ -198,11 +191,11 @@ describe('Test the Tree component', () => {
198
191
expect ( await findByTitle ( 'test2' ) ) . toBeInTheDocument ( ) ;
199
192
} ) ;
200
193
201
- test ( 'Should support to sort via drag' , async ( ) => {
194
+ test ( 'Should NOT support to sort via drag' , async ( ) => {
202
195
const data = [
203
- { id : '1' , name : 'test1' } ,
204
- { id : '2' , name : 'test2' } ,
205
- { id : '3' , name : 'test3' } ,
196
+ { id : '1' , name : 'test1' , isLeaf : true } ,
197
+ { id : '2' , name : 'test2' , isLeaf : true } ,
198
+ { id : '3' , name : 'test3' , isLeaf : true } ,
206
199
] ;
207
200
const mockFn = jest . fn ( ) ;
208
201
const { findByTitle } = render (
@@ -214,24 +207,47 @@ describe('Test the Tree component', () => {
214
207
await findByTitle ( 'test1' )
215
208
) ;
216
209
217
- await dragExpect ( mockFn , [
218
- { id : '1' , name : 'test1' } ,
219
- { id : '3' , name : 'test3' } ,
220
- { id : '2' , name : 'test2' } ,
221
- ] ) ;
210
+ expect ( mockFn ) . not . toBeCalled ( ) ;
222
211
} ) ;
223
212
224
- test ( 'Should support to drag into children ' , async ( ) => {
213
+ test ( 'Should NOT darg to the its parent node ' , async ( ) => {
225
214
const data = [
226
215
{
227
216
id : '1' ,
228
217
name : 'test1' ,
229
- children : [ { id : '1-1' , name : 'test1-1' } ] ,
218
+ isLeaf : false ,
219
+ children : [ { id : '1-1' , name : 'test1-1' , isLeaf : true } ] ,
230
220
} ,
221
+ ] ;
222
+ const mockFn = jest . fn ( ) ;
223
+ const { findByTitle } = render (
224
+ < TreeView
225
+ draggable
226
+ onDropTree = { mockFn }
227
+ defaultExpandAll
228
+ data = { data }
229
+ />
230
+ ) ;
231
+
232
+ dragToTargetNode (
233
+ await findByTitle ( 'test1-1' ) ,
234
+ await findByTitle ( 'test1' )
235
+ ) ;
236
+
237
+ expect ( mockFn ) . not . toBeCalled ( ) ;
238
+ } ) ;
239
+
240
+ test ( 'Should support to drag into children' , async ( ) => {
241
+ const source = { id : '2' , name : 'test2' , isLeaf : true } ;
242
+ const target = { id : '1-1' , name : 'test1-1' , isLeaf : false } ;
243
+ const data = [
231
244
{
232
- id : '2' ,
233
- name : 'test2' ,
245
+ id : '1' ,
246
+ name : 'test1' ,
247
+ isLeaf : false ,
248
+ children : [ target ] ,
234
249
} ,
250
+ source ,
235
251
] ;
236
252
const mockFn = jest . fn ( ) ;
237
253
const { findByTitle } = render (
@@ -244,19 +260,54 @@ describe('Test the Tree component', () => {
244
260
) ;
245
261
246
262
dragToTargetNode (
247
- await findByTitle ( 'test2' ) ,
248
- await findByTitle ( 'test1-1' )
263
+ await findByTitle ( source . name ) ,
264
+ await findByTitle ( target . name )
249
265
) ;
250
266
251
- await dragExpect ( mockFn , [
267
+ expect ( mockFn ) . toBeCalled ( ) ;
268
+ expect ( mockFn . mock . calls [ 0 ] [ 0 ] ) . toEqual ( source ) ;
269
+ expect ( mockFn . mock . calls [ 0 ] [ 1 ] ) . toEqual ( target ) ;
270
+ } ) ;
271
+
272
+ test ( 'Should support to drag to the folder rather than a file' , async ( ) => {
273
+ const source = { id : '2-1' , name : 'test2-1' , isLeaf : true } ;
274
+ const target = { id : '1-1' , name : 'test1-1' , isLeaf : true } ;
275
+ const data = [
252
276
{
253
277
id : '1' ,
254
278
name : 'test1' ,
255
- children : [
256
- { id : '1-1' , name : 'test1-1' } ,
257
- { id : '2' , name : 'test2' } ,
258
- ] ,
279
+ isLeaf : false ,
280
+ children : [ target ] ,
259
281
} ,
260
- ] ) ;
282
+ {
283
+ id : '2' ,
284
+ name : 'test2' ,
285
+ isLeaf : false ,
286
+ children : [ source ] ,
287
+ } ,
288
+ ] ;
289
+ const mockFn = jest . fn ( ) ;
290
+ const { findByTitle } = render (
291
+ < TreeView
292
+ draggable
293
+ onDropTree = { mockFn }
294
+ defaultExpandAll
295
+ data = { data }
296
+ />
297
+ ) ;
298
+
299
+ dragToTargetNode (
300
+ await findByTitle ( source . name ) ,
301
+ await findByTitle ( target . name )
302
+ ) ;
303
+
304
+ expect ( mockFn ) . toBeCalled ( ) ;
305
+ expect ( mockFn . mock . calls [ 0 ] [ 0 ] ) . toEqual ( source ) ;
306
+ expect ( mockFn . mock . calls [ 0 ] [ 1 ] ) . toEqual ( {
307
+ id : '1' ,
308
+ name : 'test1' ,
309
+ isLeaf : false ,
310
+ children : [ target ] ,
311
+ } ) ;
261
312
} ) ;
262
313
} ) ;
0 commit comments