@@ -6,11 +6,12 @@ describe('uiSortable', function() {
6
6
beforeEach ( module ( 'ui.sortable' ) ) ;
7
7
beforeEach ( module ( 'ui.sortable.testHelper' ) ) ;
8
8
9
- var EXTRA_DY_PERCENTAGE , listContent ;
9
+ var EXTRA_DY_PERCENTAGE , listContent , hasUndefinedProperties ;
10
10
11
11
beforeEach ( inject ( function ( sortableTestHelper ) {
12
12
EXTRA_DY_PERCENTAGE = sortableTestHelper . EXTRA_DY_PERCENTAGE ;
13
13
listContent = sortableTestHelper . listContent ;
14
+ hasUndefinedProperties = sortableTestHelper . hasUndefinedProperties ;
14
15
} ) ) ;
15
16
16
17
describe ( 'Callbacks related' , function ( ) {
@@ -188,6 +189,133 @@ describe('uiSortable', function() {
188
189
} ) ;
189
190
} ) ;
190
191
192
+ it ( 'should properly set ui.item.sortable properties' , function ( ) {
193
+ inject ( function ( $compile , $rootScope ) {
194
+ var element , updateCallbackExpectations ;
195
+ element = $compile ( '<ul ui-sortable="opts" ng-model="items"><li ng-repeat="item in items" id="s-{{$index}}">{{ item }}</li></ul>' ) ( $rootScope ) ;
196
+ $rootScope . $apply ( function ( ) {
197
+ $rootScope . opts = {
198
+ update : function ( e , ui ) {
199
+ if ( ui . item . scope ( ) . item === 'Two' ) {
200
+ ui . item . sortable . cancel ( ) ;
201
+ }
202
+ updateCallbackExpectations ( ui . item . sortable ) ;
203
+ }
204
+ } ;
205
+ $rootScope . items = [ 'One' , 'Two' , 'Three' ] ;
206
+ } ) ;
207
+
208
+ host . append ( element ) ;
209
+
210
+ $rootScope . $apply ( function ( ) {
211
+ } ) ;
212
+ var li = element . find ( ':eq(1)' ) ;
213
+ var dy = ( 1 + EXTRA_DY_PERCENTAGE ) * li . outerHeight ( ) ;
214
+ updateCallbackExpectations = function ( uiItemSortable ) {
215
+ expect ( uiItemSortable . model ) . toEqual ( 'Two' ) ;
216
+ expect ( uiItemSortable . index ) . toEqual ( 1 ) ;
217
+ expect ( uiItemSortable . source . length ) . toEqual ( 1 ) ;
218
+ expect ( uiItemSortable . source [ 0 ] ) . toBe ( host . children ( ) [ 0 ] ) ;
219
+ expect ( uiItemSortable . sourceModel ) . toBe ( $rootScope . items ) ;
220
+ expect ( uiItemSortable . isCanceled ( ) ) . toBe ( true ) ;
221
+ expect ( uiItemSortable . isCustomHelperUsed ( ) ) . toBe ( false ) ;
222
+ } ;
223
+ li . simulate ( 'drag' , { dy : dy } ) ;
224
+ expect ( $rootScope . items ) . toEqual ( [ 'One' , 'Two' , 'Three' ] ) ;
225
+ expect ( $rootScope . items ) . toEqual ( listContent ( element ) ) ;
226
+ updateCallbackExpectations = undefined ;
227
+
228
+ li = element . find ( ':eq(0)' ) ;
229
+ dy = ( 2 + EXTRA_DY_PERCENTAGE ) * li . outerHeight ( ) ;
230
+ updateCallbackExpectations = function ( uiItemSortable ) {
231
+ expect ( uiItemSortable . model ) . toEqual ( 'One' ) ;
232
+ expect ( uiItemSortable . index ) . toEqual ( 0 ) ;
233
+ expect ( uiItemSortable . source . length ) . toEqual ( 1 ) ;
234
+ expect ( uiItemSortable . source [ 0 ] ) . toBe ( host . children ( ) [ 0 ] ) ;
235
+ expect ( uiItemSortable . sourceModel ) . toBe ( $rootScope . items ) ;
236
+ expect ( uiItemSortable . isCanceled ( ) ) . toBe ( false ) ;
237
+ expect ( uiItemSortable . isCustomHelperUsed ( ) ) . toBe ( false ) ;
238
+ } ;
239
+ li . simulate ( 'drag' , { dy : dy } ) ;
240
+ expect ( $rootScope . items ) . toEqual ( [ 'Two' , 'Three' , 'One' ] ) ;
241
+ expect ( $rootScope . items ) . toEqual ( listContent ( element ) ) ;
242
+ updateCallbackExpectations = undefined ;
243
+
244
+ li = element . find ( ':eq(2)' ) ;
245
+ dy = - ( 2 + EXTRA_DY_PERCENTAGE ) * li . outerHeight ( ) ;
246
+ updateCallbackExpectations = function ( uiItemSortable ) {
247
+ expect ( uiItemSortable . model ) . toEqual ( 'One' ) ;
248
+ expect ( uiItemSortable . index ) . toEqual ( 2 ) ;
249
+ expect ( uiItemSortable . source . length ) . toEqual ( 1 ) ;
250
+ expect ( uiItemSortable . source [ 0 ] ) . toBe ( host . children ( ) [ 0 ] ) ;
251
+ expect ( uiItemSortable . sourceModel ) . toBe ( $rootScope . items ) ;
252
+ expect ( uiItemSortable . isCanceled ( ) ) . toBe ( false ) ;
253
+ expect ( uiItemSortable . isCustomHelperUsed ( ) ) . toBe ( false ) ;
254
+ } ;
255
+ li . simulate ( 'drag' , { dy : dy } ) ;
256
+ expect ( $rootScope . items ) . toEqual ( [ 'One' , 'Two' , 'Three' ] ) ;
257
+ expect ( $rootScope . items ) . toEqual ( listContent ( element ) ) ;
258
+ updateCallbackExpectations = undefined ;
259
+
260
+ $ ( element ) . remove ( ) ;
261
+ } ) ;
262
+ } ) ;
263
+
264
+ it ( 'should properly free ui.item.sortable object' , function ( ) {
265
+ inject ( function ( $compile , $rootScope ) {
266
+ var element , uiItem , uiItemSortable_Destroy ;
267
+ element = $compile ( '<ul ui-sortable="opts" ng-model="items"><li ng-repeat="item in items" id="s-{{$index}}">{{ item }}</li></ul>' ) ( $rootScope ) ;
268
+ $rootScope . $apply ( function ( ) {
269
+ $rootScope . opts = {
270
+ start : function ( e , ui ) {
271
+ uiItem = ui . item ;
272
+ spyOn ( ui . item . sortable , '_destroy' ) . andCallThrough ( ) ;
273
+ uiItemSortable_Destroy = ui . item . sortable . _destroy ;
274
+ } ,
275
+ update : function ( e , ui ) {
276
+ if ( ui . item . scope ( ) . item === 'Two' ) {
277
+ ui . item . sortable . cancel ( ) ;
278
+ }
279
+ }
280
+ } ;
281
+ $rootScope . items = [ 'One' , 'Two' , 'Three' ] ;
282
+ } ) ;
283
+
284
+ host . append ( element ) ;
285
+
286
+ var li = element . find ( ':eq(1)' ) ;
287
+ var dy = ( 1 + EXTRA_DY_PERCENTAGE ) * li . outerHeight ( ) ;
288
+ li . simulate ( 'drag' , { dy : dy } ) ;
289
+ expect ( $rootScope . items ) . toEqual ( [ 'One' , 'Two' , 'Three' ] ) ;
290
+ expect ( $rootScope . items ) . toEqual ( listContent ( element ) ) ;
291
+ expect ( uiItemSortable_Destroy ) . toHaveBeenCalled ( ) ;
292
+ expect ( hasUndefinedProperties ( uiItem . sortable ) ) . toBe ( true ) ;
293
+ uiItem = uiItemSortable_Destroy = undefined ;
294
+
295
+
296
+ li = element . find ( ':eq(0)' ) ;
297
+ dy = ( 2 + EXTRA_DY_PERCENTAGE ) * li . outerHeight ( ) ;
298
+ li . simulate ( 'drag' , { dy : dy } ) ;
299
+ expect ( $rootScope . items ) . toEqual ( [ 'Two' , 'Three' , 'One' ] ) ;
300
+ expect ( $rootScope . items ) . toEqual ( listContent ( element ) ) ;
301
+ expect ( uiItemSortable_Destroy ) . toHaveBeenCalled ( ) ;
302
+ expect ( hasUndefinedProperties ( uiItem . sortable ) ) . toBe ( true ) ;
303
+ uiItem = uiItemSortable_Destroy = undefined ;
304
+
305
+
306
+ li = element . find ( ':eq(2)' ) ;
307
+ dy = - ( 2 + EXTRA_DY_PERCENTAGE ) * li . outerHeight ( ) ;
308
+ li . simulate ( 'drag' , { dy : dy } ) ;
309
+ expect ( $rootScope . items ) . toEqual ( [ 'One' , 'Two' , 'Three' ] ) ;
310
+ expect ( $rootScope . items ) . toEqual ( listContent ( element ) ) ;
311
+ expect ( uiItemSortable_Destroy ) . toHaveBeenCalled ( ) ;
312
+ expect ( hasUndefinedProperties ( uiItem . sortable ) ) . toBe ( true ) ;
313
+ uiItem = uiItemSortable_Destroy = undefined ;
314
+
315
+ $ ( element ) . remove ( ) ;
316
+ } ) ;
317
+ } ) ;
318
+
191
319
} ) ;
192
320
193
321
} ) ;
0 commit comments