@@ -16,12 +16,13 @@ let items;
16
16
let element ;
17
17
let input ;
18
18
const initialRender = "<div><header></header><div>a</div><div>b</div><div>c</div><footer></footer></div>" ;
19
+ const initialRenderRaw = "<div><div>a</div><div>b</div><div>c</div></div>" ;
19
20
20
21
function getEvent ( name ) {
21
22
return Sortable . mock . calls [ 0 ] [ 1 ] [ name ] ;
22
23
}
23
24
24
- describe ( "draggable.vue" , ( ) => {
25
+ describe ( "draggable.vue when initialized with list " , ( ) => {
25
26
beforeEach ( ( ) => {
26
27
Sortable . mockClear ( ) ;
27
28
items = [ "a" , "b" , "c" ] ;
@@ -311,6 +312,144 @@ describe("draggable.vue", () => {
311
312
} )
312
313
} ) ;
313
314
315
+ describe ( "when sending DragEnd" , ( ) => {
316
+ let endEvt ;
317
+ beforeEach ( ( ) => {
318
+ endEvt = {
319
+ data : "data"
320
+ } ;
321
+ const onEnd = getEvent ( "onEnd" ) ;
322
+ onEnd ( endEvt ) ;
323
+ } )
324
+
325
+ it ( "sends a update event" , async ( ) => {
326
+ await Vue . nextTick ( ) ;
327
+ expect ( wrapper . emitted ( ) . end ) . toEqual ( [ [ endEvt ] ] ) ;
328
+ } )
329
+ } )
330
+ } ) ;
331
+ } ) ;
332
+
333
+ describe ( "draggable.vue when initialized with value" , ( ) => {
334
+ beforeEach ( ( ) => {
335
+ Sortable . mockClear ( ) ;
336
+ items = [ "a" , "b" , "c" ] ;
337
+ wrapper = shallowMount ( draggable , {
338
+ attachToDocument : true ,
339
+ propsData :{
340
+ value : items
341
+ } ,
342
+ slots : {
343
+ default : items . map ( item => `<div>${ item } </div>` ) ,
344
+ }
345
+ } ) ;
346
+ vm = wrapper . vm ;
347
+ props = vm . $options . props ;
348
+ element = wrapper . element ;
349
+ } ) ;
350
+
351
+ describe ( "when initiating a drag operation" , ( ) => {
352
+ let evt ;
353
+ let item ;
354
+ beforeEach ( ( ) => {
355
+ item = element . children [ 1 ] ;
356
+ evt = { item } ;
357
+ const start = getEvent ( "onStart" ) ;
358
+ start ( evt ) ;
359
+ } ) ;
360
+
361
+ it ( "sends a start event" , async ( ) => {
362
+ await Vue . nextTick ( ) ;
363
+ expect ( wrapper . emitted ( ) ) . toEqual ( {
364
+ start : [ [ evt ] ]
365
+ } ) ;
366
+ } )
367
+
368
+ it ( "sets context" , async ( ) => {
369
+ await Vue . nextTick ( ) ;
370
+ expect ( vm . context ) . toEqual ( {
371
+ element : "b" ,
372
+ index : 1
373
+ } ) ;
374
+ } )
375
+
376
+ describe ( "when remove is called" , ( ) => {
377
+ beforeEach ( ( ) => {
378
+ element . removeChild ( item ) ;
379
+ const remove = getEvent ( "onRemove" ) ;
380
+ remove ( {
381
+ item,
382
+ oldIndex : 1
383
+ } ) ;
384
+ } )
385
+
386
+ it ( "DOM changes should be reverted" , async ( ) => {
387
+ await Vue . nextTick ( ) ;
388
+ expect ( wrapper . html ( ) ) . toEqual ( initialRenderRaw ) ;
389
+ } )
390
+
391
+ it ( "input should with updated value" , async ( ) => {
392
+ await Vue . nextTick ( ) ;
393
+ const expected = [ "a" , "c" ] ;
394
+ expect ( wrapper . emitted ( ) . input ) . toEqual ( [ [ expected ] ] ) ;
395
+ } )
396
+
397
+ it ( "sends a remove event" , async ( ) => {
398
+ await Vue . nextTick ( ) ;
399
+ const expectedEvt = { item, oldIndex : 1 } ;
400
+ expect ( wrapper . emitted ( ) . remove ) . toEqual ( [ [ expectedEvt ] ] ) ;
401
+ } )
402
+
403
+ it ( "sends a change event" , async ( ) => {
404
+ await Vue . nextTick ( ) ;
405
+ const expectedEvt = { removed : { element : "b" , oldIndex : 1 } } ;
406
+ expect ( wrapper . emitted ( ) . change ) . toEqual ( [ [ expectedEvt ] ] ) ;
407
+ } )
408
+ } )
409
+
410
+ describe ( "when update is called" , ( ) => {
411
+ beforeEach ( ( ) => {
412
+ const firstDraggable = element . children [ 0 ] ;
413
+ element . removeChild ( item ) ;
414
+ element . insertBefore ( item , firstDraggable ) ;
415
+ const update = getEvent ( "onUpdate" ) ;
416
+ update ( {
417
+ item,
418
+ oldIndex : 1 ,
419
+ newIndex : 0 ,
420
+ from : element
421
+ } ) ;
422
+ } )
423
+
424
+ it ( "DOM changes should be reverted" , async ( ) => {
425
+ await Vue . nextTick ( ) ;
426
+ expect ( wrapper . html ( ) ) . toEqual ( initialRenderRaw ) ;
427
+ } )
428
+
429
+ it ( "send an input event" , async ( ) => {
430
+ await Vue . nextTick ( ) ;
431
+ const expected = [ "b" , "a" , "c" ] ;
432
+ expect ( wrapper . emitted ( ) . input ) . toEqual ( [ [ expected ] ] ) ;
433
+ } )
434
+
435
+ it ( "sends a update event" , async ( ) => {
436
+ await Vue . nextTick ( ) ;
437
+ const expectedEvt = {
438
+ item,
439
+ oldIndex : 1 ,
440
+ newIndex : 0 ,
441
+ from : element
442
+ } ;
443
+ expect ( wrapper . emitted ( ) . update ) . toEqual ( [ [ expectedEvt ] ] ) ;
444
+ } )
445
+
446
+ it ( "sends a change event" , async ( ) => {
447
+ await Vue . nextTick ( ) ;
448
+ const expectedEvt = { moved : { element : "b" , oldIndex : 1 , newIndex : 0 } } ;
449
+ expect ( wrapper . emitted ( ) . change ) . toEqual ( [ [ expectedEvt ] ] ) ;
450
+ } )
451
+ } ) ;
452
+
314
453
describe ( "when sending DragEnd" , ( ) => {
315
454
let endEvt ;
316
455
beforeEach ( ( ) => {
0 commit comments