@@ -3,6 +3,8 @@ describe('uiSortable', function() {
3
3
// Ensure the sortable angular module is loaded
4
4
beforeEach ( module ( 'ui.sortable' ) ) ;
5
5
6
+ var EXTRA_DY_PERCENTAGE = 0.25 ;
7
+
6
8
describe ( 'simple use' , function ( ) {
7
9
8
10
it ( 'should have a ui-sortable class' , function ( ) {
@@ -13,25 +15,146 @@ describe('uiSortable', function() {
13
15
} ) ;
14
16
} ) ;
15
17
18
+ it ( 'should log that ngModel was not provided' , function ( ) {
19
+ inject ( function ( $compile , $rootScope , $log ) {
20
+ var element ;
21
+ element = $compile ( '<ul ui-sortable><li ng-repeat="item in items" id="s-{{$index}}">{{ item }}</li></ul>' ) ( $rootScope ) ;
22
+ $rootScope . $apply ( function ( ) {
23
+ $rootScope . items = [ "One" , "Two" , "Three" ] ;
24
+ } ) ;
25
+
26
+ expect ( $log . info . logs . length ) . toEqual ( 1 ) ;
27
+ expect ( $log . info . logs [ 0 ] . length ) . toEqual ( 2 ) ;
28
+ expect ( $log . info . logs [ 0 ] [ 0 ] ) . toEqual ( 'ui.sortable: ngModel not provided!' ) ;
29
+ } ) ;
30
+ } ) ;
31
+
32
+ } ) ;
33
+
34
+ describe ( 'Drag & Drop simulation' , function ( ) {
35
+
36
+ var host ;
37
+
38
+ beforeEach ( inject ( function ( ) {
39
+ host = $ ( '<div id="test-host"></div>' ) ;
40
+ $ ( 'body' ) . append ( host ) ;
41
+ } ) ) ;
42
+
43
+ afterEach ( function ( ) {
44
+ host . remove ( ) ;
45
+ host = null ;
46
+ } ) ;
47
+
16
48
it ( 'should update model when order changes' , function ( ) {
17
49
inject ( function ( $compile , $rootScope ) {
18
50
var element ;
19
51
element = $compile ( '<ul ui-sortable ng-model="items"><li ng-repeat="item in items" id="s-{{$index}}">{{ item }}</li></ul>' ) ( $rootScope ) ;
20
52
$rootScope . $apply ( function ( ) {
21
- return $rootScope . items = [ "One" , "Two" , "Three" ] ;
53
+ $rootScope . items = [ "One" , "Two" , "Three" ] ;
22
54
} ) ;
23
55
24
- element . find ( 'li:eq(1)' ) . insertAfter ( element . find ( 'li:eq(2)' ) ) ;
56
+ host . append ( element ) ;
57
+
58
+ var li = element . find ( ':eq(1)' ) ;
59
+ var dy = ( 1 + EXTRA_DY_PERCENTAGE ) * li . outerHeight ( ) ;
60
+ li . simulate ( 'drag' , { dy : dy } ) ;
61
+ expect ( $rootScope . items ) . toEqual ( [ "One" , "Three" , "Two" ] ) ;
62
+
63
+ li = element . find ( ':eq(1)' ) ;
64
+ dy = - ( 1 + EXTRA_DY_PERCENTAGE ) * li . outerHeight ( ) ;
65
+ li . simulate ( 'drag' , { dy : dy } ) ;
66
+ expect ( $rootScope . items ) . toEqual ( [ "Three" , "One" , "Two" ] ) ;
67
+
68
+ $ ( element ) . remove ( ) ;
69
+ } ) ;
70
+ } ) ;
71
+
72
+ it ( 'should cancel sorting of node "Two"' , function ( ) {
73
+ inject ( function ( $compile , $rootScope ) {
74
+ var element ;
75
+ element = $compile ( '<ul ui-sortable="opts" ng-model="items"><li ng-repeat="item in items" id="s-{{$index}}">{{ item }}</li></ul>' ) ( $rootScope ) ;
76
+ $rootScope . $apply ( function ( ) {
77
+ $rootScope . opts = {
78
+ update : function ( e , ui ) {
79
+ if ( ui . item . scope ( ) . item === "Two" ) {
80
+ ui . item . parent ( ) . sortable ( 'cancel' ) ;
81
+ }
82
+ }
83
+ } ;
84
+ $rootScope . items = [ "One" , "Two" , "Three" ] ;
85
+ } ) ;
25
86
26
- // None of this work, one way is to use .bind("sortupdate")
27
- // and then use .trigger("sortupdate", e, ui) but I have no idea how to
28
- // construct ui object
29
-
30
- // element.sortable('refresh')
31
- // element.sortable('refreshPositions')
32
- // element.trigger('sortupdate')
87
+ host . append ( element ) ;
33
88
34
- // expect($rootScope.items).toEqual(["One", "Three", "Two"])
89
+ var li = element . find ( ':eq(1)' ) ;
90
+ var dy = ( 1 + EXTRA_DY_PERCENTAGE ) * li . outerHeight ( ) ;
91
+ li . simulate ( 'drag' , { dy : dy } ) ;
92
+ expect ( $rootScope . items ) . toEqual ( [ "One" , "Two" , "Three" ] ) ;
93
+
94
+ li = element . find ( ':eq(0)' ) ;
95
+ dy = ( 2 + EXTRA_DY_PERCENTAGE ) * li . outerHeight ( ) ;
96
+ li . simulate ( 'drag' , { dy : dy } ) ;
97
+ expect ( $rootScope . items ) . toEqual ( [ "Two" , "Three" , "One" ] ) ;
98
+
99
+ $ ( element ) . remove ( ) ;
100
+ } ) ;
101
+ } ) ;
102
+
103
+ it ( 'should update model from stop() callback' , function ( ) {
104
+ inject ( function ( $compile , $rootScope ) {
105
+ // TODO
106
+ } ) ;
107
+ } ) ;
108
+
109
+ it ( 'should not allow sorting of "locked" nodes' , function ( ) {
110
+ inject ( function ( $compile , $rootScope ) {
111
+ var element ;
112
+ element = $compile ( '<ul ui-sortable="opts" ng-model="items"><li ng-repeat="item in items" id="s-{{$index}}" ng-class="{ sortable: item.sortable }">{{ item.text }}</li></ul>' ) ( $rootScope ) ;
113
+ $rootScope . $apply ( function ( ) {
114
+ $rootScope . opts = {
115
+ items :'> .sortable'
116
+ } ;
117
+ $rootScope . items = [
118
+ { text : "One" , sortable : true } ,
119
+ { text : "Two" , sortable : true } ,
120
+ { text : "Three" , sortable : false } ,
121
+ { text : "Four" , sortable : true }
122
+ ] ;
123
+ } ) ;
124
+
125
+ host . append ( element ) ;
126
+
127
+ var li = element . find ( ':eq(2)' ) ;
128
+ var dy = ( 1 + EXTRA_DY_PERCENTAGE ) * li . outerHeight ( ) ;
129
+ li . simulate ( 'drag' , { dy : dy } ) ;
130
+ expect ( $rootScope . items . map ( function ( x ) { return x . text ; } ) ) . toEqual ( [ "One" , "Two" , "Three" , "Four" ] ) ;
131
+
132
+ li = element . find ( ':eq(1)' ) ;
133
+ dy = ( 2 + EXTRA_DY_PERCENTAGE ) * li . outerHeight ( ) ;
134
+ li . simulate ( 'drag' , { dy : dy } ) ;
135
+ expect ( $rootScope . items . map ( function ( x ) { return x . text ; } ) ) . toEqual ( [ "One" , "Three" , "Four" , "Two" ] ) ;
136
+
137
+ // // fails on angular 1.2
138
+ // li = element.find(':eq(2)');
139
+ // dy = -(2 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
140
+ // li.simulate('drag', { dy: dy });
141
+ // expect($rootScope.items.map(function(x){ return x.text; })).toEqual(["Four", "One", "Three", "Two"]);
142
+
143
+ // // fails on angular 1.2
144
+ // li = element.find(':eq(3)');
145
+ // dy = -(2 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
146
+ // li.simulate('drag', { dy: dy });
147
+ // expect($rootScope.items.map(function(x){ return x.text; })).toEqual(["Four", "Two", "One", "Three"]);
148
+
149
+ // also placing right above the locked node seems a bit harder !?!?
150
+
151
+ $ ( element ) . remove ( ) ;
152
+ } ) ;
153
+ } ) ;
154
+
155
+ it ( 'should update model when sorting between sortables' , function ( ) {
156
+ inject ( function ( $compile , $rootScope ) {
157
+ // TODO
35
158
} ) ;
36
159
} ) ;
37
160
0 commit comments