@@ -595,14 +595,30 @@ describe("draggable.vue when initialized with list", () => {
595
595
} ) ;
596
596
} ) ;
597
597
598
- describe ( "when remove is called" , ( ) => {
599
- const expectedDomAfterRemove = `<div><header></header><div data-draggable="true">a</div><div data-draggable="true">c</div><footer></footer></div>` ;
600
- beforeEach ( ( ) => {
598
+ describe . each ( [
599
+ [ 1 , [ "a" , "c" ] ] ,
600
+ [ 0 , [ "b" , "c" ] ] ,
601
+ [ 2 , [ "a" , "b" ] ]
602
+ ] ) ( "when remove is called with index %p + 1" , ( oldIndex , expectedList ) => {
603
+ const expectedDomAfterRemove = `<div><header></header>${ expectedList
604
+ . map ( el => `<div data-draggable="true">${ el } </div>` )
605
+ . join ( "" ) } <footer></footer></div>`;
606
+ const domIndex = oldIndex + 1 ;
607
+ const expectedEvt = {
608
+ removed : { element : [ "a" , "b" , "c" ] [ oldIndex ] , oldIndex }
609
+ } ;
610
+
611
+ beforeEach ( async ( ) => {
612
+ item = element . children . item ( domIndex ) ;
613
+ const startEvt = { item } ;
614
+ getEvent ( "onStart" ) ( startEvt ) ;
615
+ await nextTick ( ) ;
616
+
601
617
element . removeChild ( item ) ;
602
618
const remove = getEvent ( "onRemove" ) ;
603
619
remove ( {
604
620
item,
605
- oldIndex : 2
621
+ oldIndex : domIndex
606
622
} ) ;
607
623
} ) ;
608
624
@@ -613,22 +629,89 @@ describe("draggable.vue when initialized with list", () => {
613
629
614
630
it ( "list should be updated" , async ( ) => {
615
631
await nextTick ( ) ;
616
- expect ( vm . list ) . toEqual ( [ "a" , "c" ] ) ;
632
+ expect ( vm . list ) . toEqual ( expectedList ) ;
617
633
} ) ;
618
634
619
635
it ( "sends a remove event" , async ( ) => {
620
636
await nextTick ( ) ;
621
- const expectedEvt = { item, oldIndex : 2 } ;
637
+ const expectedEvt = { item, oldIndex : domIndex } ;
622
638
expect ( wrapper . emitted ( ) . remove ) . toEqual ( [ [ expectedEvt ] ] ) ;
623
639
} ) ;
624
640
625
641
it ( "sends a change event" , async ( ) => {
626
642
await nextTick ( ) ;
627
- const expectedEvt = { removed : { element : "b" , oldIndex : 1 } } ;
628
643
expect ( wrapper . emitted ( ) . change ) . toEqual ( [ [ expectedEvt ] ] ) ;
629
644
} ) ;
630
645
} ) ;
631
646
647
+ describe . each ( [
648
+ [ 1 , [ "a" , "c" ] ] ,
649
+ [ 0 , [ "b" , "c" ] ] ,
650
+ [ 2 , [ "a" , "b" ] ]
651
+ ] ) (
652
+ "when remove is called with index %p on component without extra slots" ,
653
+ ( oldIndex , expectedList ) => {
654
+ const expectedDomAfterRemove = `<div>${ expectedList
655
+ . map ( el => `<div data-draggable="true">${ el } </div>` )
656
+ . join ( "" ) } </div>`;
657
+ const expectedEvt = {
658
+ removed : { element : [ "a" , "b" , "c" ] [ oldIndex ] , oldIndex }
659
+ } ;
660
+
661
+ beforeEach ( async ( ) => {
662
+ resetMocks ( ) ;
663
+
664
+ wrapper = mount ( draggable , {
665
+ props : {
666
+ list : [ "a" , "b" , "c" ] ,
667
+ itemKey : k => k
668
+ } ,
669
+ slots : {
670
+ item : ( { element } ) => {
671
+ return h ( "div" , null , element ) ;
672
+ }
673
+ }
674
+ } ) ;
675
+ vm = wrapper . vm ;
676
+ element = wrapper . element ;
677
+ await nextTick ( ) ;
678
+
679
+ item = element . children . item ( oldIndex ) ;
680
+ const startEvt = { item } ;
681
+ getEvent ( "onStart" ) ( startEvt ) ;
682
+ await nextTick ( ) ;
683
+
684
+ element . removeChild ( item ) ;
685
+ const remove = getEvent ( "onRemove" ) ;
686
+ remove ( {
687
+ item,
688
+ oldIndex
689
+ } ) ;
690
+ } ) ;
691
+
692
+ it ( "DOM should be updated" , async ( ) => {
693
+ await nextTick ( ) ;
694
+ expectHTML ( wrapper , expectedDomAfterRemove ) ;
695
+ } ) ;
696
+
697
+ it ( "list should be updated" , async ( ) => {
698
+ await nextTick ( ) ;
699
+ expect ( vm . list ) . toEqual ( expectedList ) ;
700
+ } ) ;
701
+
702
+ it ( "sends a remove event" , async ( ) => {
703
+ await nextTick ( ) ;
704
+ const expectedRemoveEvt = { item, oldIndex } ;
705
+ expect ( wrapper . emitted ( ) . remove ) . toEqual ( [ [ expectedRemoveEvt ] ] ) ;
706
+ } ) ;
707
+
708
+ it ( "sends a change event" , async ( ) => {
709
+ await nextTick ( ) ;
710
+ expect ( wrapper . emitted ( ) . change ) . toEqual ( [ [ expectedEvt ] ] ) ;
711
+ } ) ;
712
+ }
713
+ ) ;
714
+
632
715
describe . each ( [
633
716
[ 1 , [ "b" , "a" , "c" ] ] ,
634
717
[ 3 , [ "a" , "c" , "b" ] ]
@@ -1529,7 +1612,7 @@ describe("when using only footer slot with an none-empty list", () => {
1529
1612
1530
1613
it ( "list should be updated" , async ( ) => {
1531
1614
await nextTick ( ) ;
1532
- expect ( vm . list ) . toEqual ( [ "first" , "last" ] ) ;
1615
+ expect ( vm . list ) . toEqual ( [ "first" , "last" ] ) ;
1533
1616
} ) ;
1534
1617
1535
1618
it ( "sends a update event" , async ( ) => {
@@ -1550,7 +1633,7 @@ describe("when using only footer slot with an none-empty list", () => {
1550
1633
} ) ;
1551
1634
1552
1635
describe ( "when using a fragment component as tag" , ( ) => {
1553
- beforeEach ( async ( ) => {
1636
+ beforeEach ( ( ) => {
1554
1637
resetMocks ( ) ;
1555
1638
1556
1639
wrapper = mount ( DraggableWithFragment , {
@@ -1581,7 +1664,7 @@ describe.each([
1581
1664
[ "n" , [ 0 , 1 ] ] ,
1582
1665
[ ( { a, n } ) => `${ a } -${ n } ` , [ "a-0" , "b-1" ] ]
1583
1666
] ) ( "when using %p as item-key" , ( itemKey , expected ) => {
1584
- beforeEach ( async ( ) => {
1667
+ beforeEach ( ( ) => {
1585
1668
resetMocks ( ) ;
1586
1669
1587
1670
wrapper = mount ( draggable , {
@@ -1606,4 +1689,4 @@ describe.each([
1606
1689
. map ( el => el . __vnode . key ) ;
1607
1690
expect ( keys ) . toEqual ( expected ) ;
1608
1691
} ) ;
1609
- } )
1692
+ } ) ;
0 commit comments