1
+ import { mount } from "@vue/test-utils" ;
2
+ import Sortable from "sortablejs" ;
3
+ jest . genMockFromModule ( 'sortablejs' ) ;
4
+ jest . mock ( 'sortablejs' ) ;
5
+ const SortableFake = {
6
+ destroy : jest . fn ( ) ,
7
+ option : jest . fn ( )
8
+ } ;
9
+ const SortableFake2 = {
10
+ destroy : jest . fn ( ) ,
11
+ option : jest . fn ( )
12
+ } ;
13
+ Sortable . mockImplementationOnce ( ( ) => SortableFake )
14
+ . mockImplementationOnce ( ( ) => SortableFake2 ) ;
15
+
16
+ import Vue from "vue" ;
17
+ import DraggableWithList from "./helper/DraggableWithList"
18
+ import draggable from "@/vuedraggable" ;
19
+
20
+ let wrapper ;
21
+ let element ;
22
+ let vm ;
23
+
24
+ function getEvent ( name ) {
25
+ return Sortable . mock . calls [ 0 ] [ 1 ] [ name ] ;
26
+ }
27
+
28
+ describe ( "draggable.vue when working list" , ( ) => {
29
+ beforeEach ( ( ) => {
30
+ jest . resetAllMocks ( ) ;
31
+ wrapper = mount ( DraggableWithList , {
32
+ attachToDocument : true
33
+ } ) ;
34
+ vm = wrapper . vm ;
35
+ element = wrapper . element ;
36
+ } ) ;
37
+
38
+ describe ( "when handling sort" , ( ) => {
39
+ let item ;
40
+
41
+ beforeEach ( async ( ) => {
42
+ item = element . children [ 2 ] ;
43
+ const startEvt = { item } ;
44
+ getEvent ( "onStart" ) ( startEvt ) ;
45
+ await Vue . nextTick ( ) ;
46
+
47
+ const firstDraggable = element . children [ 1 ] ;
48
+ element . removeChild ( item ) ;
49
+ element . insertBefore ( item , firstDraggable ) ;
50
+ getEvent ( "onUpdate" ) ( {
51
+ item,
52
+ oldIndex : 2 ,
53
+ newIndex : 7 ,
54
+ from : element
55
+ } ) ;
56
+ await Vue . nextTick ( ) ;
57
+ } )
58
+
59
+ it ( "sends a change event" , async ( ) => {
60
+ const draggableWrapper = wrapper . find ( draggable ) ;
61
+ const expectedEvt = { moved : { element : 2 , oldIndex : 2 , newIndex : 7 } } ;
62
+ expect ( draggableWrapper . emitted ( ) . change ) . toEqual ( [ [ expectedEvt ] ] ) ;
63
+ } ) ;
64
+
65
+ it ( "update list" , async ( ) => {
66
+ expect ( wrapper . vm . array ) . toEqual ( [ 0 , 1 , 3 , 4 , 5 , 6 , 7 , 2 , 8 , 9 ] ) ;
67
+ } ) ;
68
+
69
+ it ( "updates DOM" , async ( ) => {
70
+ const expectedArray = [ 0 , 1 , 3 , 4 , 5 , 6 , 7 , 2 , 8 , 9 ] ;
71
+ const expectedDom = `<div>${ expectedArray . map ( nu => `<div>${ nu } </div>` ) . join ( '' ) } </div>` ;
72
+ expect ( wrapper . html ( ) ) . toEqual ( expectedDom ) ;
73
+ } ) ;
74
+ } ) ;
75
+ } ) ;
0 commit comments