@@ -3,7 +3,9 @@ describe('uiSortable', function() {
3
3
// Ensure the sortable angular module is loaded
4
4
beforeEach ( module ( 'ui.sortable' ) ) ;
5
5
6
- describe ( 'simple use' , function ( ) {
6
+ var EXTRA_DY_PERCENTAGE = 0.25 ;
7
+
8
+ describe ( 'Simple use' , function ( ) {
7
9
8
10
it ( 'should have a ui-sortable class' , function ( ) {
9
11
inject ( function ( $compile , $rootScope ) {
@@ -13,25 +15,245 @@ 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
+
35
+ describe ( 'Drag & Drop simulation' , function ( ) {
36
+
37
+ var host ;
38
+
39
+ beforeEach ( inject ( function ( ) {
40
+ host = $ ( '<div id="test-host"></div>' ) ;
41
+ $ ( 'body' ) . append ( host ) ;
42
+ } ) ) ;
43
+
44
+ afterEach ( function ( ) {
45
+ host . remove ( ) ;
46
+ host = null ;
47
+ } ) ;
48
+
16
49
it ( 'should update model when order changes' , function ( ) {
17
50
inject ( function ( $compile , $rootScope ) {
18
51
var element ;
19
52
element = $compile ( '<ul ui-sortable ng-model="items"><li ng-repeat="item in items" id="s-{{$index}}">{{ item }}</li></ul>' ) ( $rootScope ) ;
20
53
$rootScope . $apply ( function ( ) {
21
- return $rootScope . items = [ "One" , "Two" , "Three" ] ;
54
+ $rootScope . items = [ "One" , "Two" , "Three" ] ;
55
+ } ) ;
56
+
57
+ host . append ( element ) ;
58
+
59
+ var li = element . find ( ':eq(1)' ) ;
60
+ var dy = ( 1 + EXTRA_DY_PERCENTAGE ) * li . outerHeight ( ) ;
61
+ li . simulate ( 'drag' , { dy : dy } ) ;
62
+ expect ( $rootScope . items ) . toEqual ( [ "One" , "Three" , "Two" ] ) ;
63
+
64
+ li = element . find ( ':eq(1)' ) ;
65
+ dy = - ( 1 + EXTRA_DY_PERCENTAGE ) * li . outerHeight ( ) ;
66
+ li . simulate ( 'drag' , { dy : dy } ) ;
67
+ expect ( $rootScope . items ) . toEqual ( [ "Three" , "One" , "Two" ] ) ;
68
+
69
+ $ ( element ) . remove ( ) ;
70
+ } ) ;
71
+ } ) ;
72
+
73
+ it ( 'should not allow sorting of "locked" nodes' , function ( ) {
74
+ inject ( function ( $compile , $rootScope ) {
75
+ var element ;
76
+ 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 ) ;
77
+ $rootScope . $apply ( function ( ) {
78
+ $rootScope . opts = {
79
+ items :'> .sortable'
80
+ } ;
81
+ $rootScope . items = [
82
+ { text : "One" , sortable : true } ,
83
+ { text : "Two" , sortable : true } ,
84
+ { text : "Three" , sortable : false } ,
85
+ { text : "Four" , sortable : true }
86
+ ] ;
87
+ } ) ;
88
+
89
+ host . append ( element ) ;
90
+
91
+ var li = element . find ( ':eq(2)' ) ;
92
+ var dy = ( 1 + EXTRA_DY_PERCENTAGE ) * li . outerHeight ( ) ;
93
+ li . simulate ( 'drag' , { dy : dy } ) ;
94
+ expect ( $rootScope . items . map ( function ( x ) { return x . text ; } ) ) . toEqual ( [ "One" , "Two" , "Three" , "Four" ] ) ;
95
+
96
+ li = element . find ( ':eq(1)' ) ;
97
+ dy = ( 2 + EXTRA_DY_PERCENTAGE ) * li . outerHeight ( ) ;
98
+ li . simulate ( 'drag' , { dy : dy } ) ;
99
+ expect ( $rootScope . items . map ( function ( x ) { return x . text ; } ) ) . toEqual ( [ "One" , "Three" , "Four" , "Two" ] ) ;
100
+
101
+ // fails on angular 1.2
102
+ li = element . find ( ':eq(2)' ) ;
103
+ dy = - ( 2 + EXTRA_DY_PERCENTAGE ) * li . outerHeight ( ) ;
104
+ li . simulate ( 'drag' , { dy : dy } ) ;
105
+ expect ( $rootScope . items . map ( function ( x ) { return x . text ; } ) ) . toEqual ( [ "Four" , "One" , "Three" , "Two" ] ) ;
106
+
107
+ // fails on angular 1.2
108
+ li = element . find ( ':eq(3)' ) ;
109
+ dy = - ( 2 + EXTRA_DY_PERCENTAGE ) * li . outerHeight ( ) ;
110
+ li . simulate ( 'drag' , { dy : dy } ) ;
111
+ expect ( $rootScope . items . map ( function ( x ) { return x . text ; } ) ) . toEqual ( [ "Four" , "Two" , "One" , "Three" ] ) ;
112
+
113
+ // also placing right above the locked node seems a bit harder !?!?
114
+
115
+ $ ( element ) . remove ( ) ;
116
+ } ) ;
117
+ } ) ;
118
+
119
+ it ( 'should update model when sorting between sortables' , function ( ) {
120
+ inject ( function ( $compile , $rootScope ) {
121
+ var elementTop , elementBottom ;
122
+ elementTop = $compile ( '<ul ui-sortable="opts" class="cross-sortable" ng-model="itemsTop"><li ng-repeat="item in itemsTop" id="s-top-{{$index}}">{{ item }}</li></ul>' ) ( $rootScope ) ;
123
+ elementBottom = $compile ( '<ul ui-sortable="opts" class="cross-sortable" ng-model="itemsBottom"><li ng-repeat="item in itemsBottom" id="s-bottom-{{$index}}">{{ item }}</li></ul>' ) ( $rootScope ) ;
124
+ $rootScope . $apply ( function ( ) {
125
+ $rootScope . itemsTop = [ "Top One" , "Top Two" , "Top Three" ] ;
126
+ $rootScope . itemsBottom = [ "Bottom One" , "Bottom Two" , "Bottom Three" ] ;
127
+ $rootScope . opts = { connectWith : ".cross-sortable" } ;
128
+ } ) ;
129
+
130
+ host . append ( elementTop ) . append ( elementBottom ) ;
131
+
132
+ // fails on angular 1.2
133
+ var li1 = elementTop . find ( ':eq(0)' ) ;
134
+ var li2 = elementBottom . find ( ':eq(0)' ) ;
135
+ var dy = EXTRA_DY_PERCENTAGE * li1 . outerHeight ( ) + ( li2 . position ( ) . top - li1 . position ( ) . top ) ;
136
+ li1 . simulate ( 'drag' , { dy : dy } ) ;
137
+ expect ( $rootScope . itemsTop ) . toEqual ( [ "Top Two" , "Top Three" ] ) ;
138
+ expect ( $rootScope . itemsBottom ) . toEqual ( [ "Bottom One" , "Top One" , "Bottom Two" , "Bottom Three" ] ) ;
139
+
140
+ // fails on angular 1.2
141
+ li1 = elementBottom . find ( ':eq(1)' ) ;
142
+ li2 = elementTop . find ( ':eq(1)' ) ;
143
+ dy = - EXTRA_DY_PERCENTAGE * li1 . outerHeight ( ) - ( li1 . position ( ) . top - li2 . position ( ) . top ) ;
144
+ li1 . simulate ( 'drag' , { dy : dy } ) ;
145
+ expect ( $rootScope . itemsTop ) . toEqual ( [ "Top Two" , "Top One" , "Top Three" ] ) ;
146
+ expect ( $rootScope . itemsBottom ) . toEqual ( [ "Bottom One" , "Bottom Two" , "Bottom Three" ] ) ;
147
+
148
+ $ ( elementTop ) . remove ( ) ;
149
+ $ ( elementBottom ) . remove ( ) ;
150
+ } ) ;
151
+ } ) ;
152
+
153
+ } ) ;
154
+
155
+
156
+ describe ( 'Callbacks related' , function ( ) {
157
+
158
+ var host ;
159
+
160
+ beforeEach ( inject ( function ( ) {
161
+ host = $ ( '<div id="test-host"></div>' ) ;
162
+ $ ( 'body' ) . append ( host ) ;
163
+ } ) ) ;
164
+
165
+ afterEach ( function ( ) {
166
+ host . remove ( ) ;
167
+ host = null ;
168
+ } ) ;
169
+
170
+ it ( 'should cancel sorting of node "Two"' , function ( ) {
171
+ inject ( function ( $compile , $rootScope ) {
172
+ var element ;
173
+ element = $compile ( '<ul ui-sortable="opts" ng-model="items"><li ng-repeat="item in items" id="s-{{$index}}">{{ item }}</li></ul>' ) ( $rootScope ) ;
174
+ $rootScope . $apply ( function ( ) {
175
+ $rootScope . opts = {
176
+ update : function ( e , ui ) {
177
+ if ( ui . item . scope ( ) . item === "Two" ) {
178
+ ui . item . parent ( ) . sortable ( 'cancel' ) ;
179
+ }
180
+ }
181
+ } ;
182
+ $rootScope . items = [ "One" , "Two" , "Three" ] ;
183
+ } ) ;
184
+
185
+ host . append ( element ) ;
186
+
187
+ var li = element . find ( ':eq(1)' ) ;
188
+ var dy = ( 1 + EXTRA_DY_PERCENTAGE ) * li . outerHeight ( ) ;
189
+ li . simulate ( 'drag' , { dy : dy } ) ;
190
+ expect ( $rootScope . items ) . toEqual ( [ "One" , "Two" , "Three" ] ) ;
191
+
192
+ li = element . find ( ':eq(0)' ) ;
193
+ dy = ( 2 + EXTRA_DY_PERCENTAGE ) * li . outerHeight ( ) ;
194
+ li . simulate ( 'drag' , { dy : dy } ) ;
195
+ expect ( $rootScope . items ) . toEqual ( [ "Two" , "Three" , "One" ] ) ;
196
+
197
+ $ ( element ) . remove ( ) ;
198
+ } ) ;
199
+ } ) ;
200
+
201
+ it ( 'should update model from update() callback' , function ( ) {
202
+ inject ( function ( $compile , $rootScope ) {
203
+ var element , logsElement ;
204
+ element = $compile ( '<ul ui-sortable="opts" ng-model="items"><li ng-repeat="item in items" id="s-{{$index}}">{{ item }}</li></ul>' ) ( $rootScope ) ;
205
+ logsElement = $compile ( '<ul ng-model="logs"><li ng-repeat="log in logs" id="l-{{$index}}">{{ log }}</li></ul>' ) ( $rootScope ) ;
206
+ $rootScope . $apply ( function ( ) {
207
+ $rootScope . opts = {
208
+ update : function ( e , ui ) {
209
+ $rootScope . logs . push ( "Moved element " + ui . item . scope ( ) . item ) ;
210
+ }
211
+ } ;
212
+ $rootScope . items = [ "One" , "Two" , "Three" ] ;
213
+ $rootScope . logs = [ ] ;
214
+ } ) ;
215
+
216
+ host . append ( element ) . append ( logsElement ) ;
217
+
218
+ var li = element . find ( ':eq(1)' ) ;
219
+ var dy = ( 1 + EXTRA_DY_PERCENTAGE ) * li . outerHeight ( ) ;
220
+ li . simulate ( 'drag' , { dy : dy } ) ;
221
+ expect ( $rootScope . items ) . toEqual ( [ "One" , "Three" , "Two" ] ) ;
222
+ expect ( $rootScope . logs ) . toEqual ( [ "Moved element Two" ] ) ;
223
+ expect ( logsElement . find ( 'li' ) . html ( ) ) . toEqual ( "Moved element Two" ) ;
224
+
225
+ $ ( element ) . remove ( ) ;
226
+ $ ( logsElement ) . remove ( ) ;
227
+ } ) ;
228
+ } ) ;
229
+
230
+ // ensure scope.apply() is called after a stop() callback
231
+ it ( 'should update model from stop() callback' , function ( ) {
232
+ inject ( function ( $compile , $rootScope ) {
233
+ var element , logsElement ;
234
+ element = $compile ( '<ul ui-sortable="opts" ng-model="items"><li ng-repeat="item in items" id="s-{{$index}}">{{ item }}</li></ul>' ) ( $rootScope ) ;
235
+ logsElement = $compile ( '<ul ng-model="logs"><li ng-repeat="log in logs" id="l-{{$index}}">{{ log }}</li></ul>' ) ( $rootScope ) ;
236
+ $rootScope . $apply ( function ( ) {
237
+ $rootScope . opts = {
238
+ stop : function ( e , ui ) {
239
+ $rootScope . logs . push ( "Moved element " + ui . item . scope ( ) . item ) ;
240
+ }
241
+ } ;
242
+ $rootScope . items = [ "One" , "Two" , "Three" ] ;
243
+ $rootScope . logs = [ ] ;
22
244
} ) ;
23
245
24
- element . find ( 'li:eq(1)' ) . insertAfter ( element . find ( 'li:eq(2)' ) ) ;
246
+ host . append ( element ) . append ( logsElement ) ;
25
247
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')
248
+ var li = element . find ( ':eq(1)' ) ;
249
+ var dy = ( 1 + EXTRA_DY_PERCENTAGE ) * li . outerHeight ( ) ;
250
+ li . simulate ( 'drag' , { dy : dy } ) ;
251
+ expect ( $rootScope . items ) . toEqual ( [ "One" , "Three" , "Two" ] ) ;
252
+ expect ( $rootScope . logs ) . toEqual ( [ "Moved element Two" ] ) ;
253
+ expect ( logsElement . find ( 'li' ) . html ( ) ) . toEqual ( "Moved element Two" ) ;
33
254
34
- // expect($rootScope.items).toEqual(["One", "Three", "Two"])
255
+ $ ( element ) . remove ( ) ;
256
+ $ ( logsElement ) . remove ( ) ;
35
257
} ) ;
36
258
} ) ;
37
259
0 commit comments