1+ 'use strict' ;
2+
3+ describe ( 'uiSortable' , function ( ) {
4+
5+ beforeEach ( module ( function ( $compileProvider ) {
6+ if ( typeof $compileProvider . debugInfoEnabled === 'function' ) {
7+ $compileProvider . debugInfoEnabled ( false ) ;
8+ }
9+ } ) ) ;
10+
11+ // Ensure the sortable angular module is loaded
12+ beforeEach ( module ( 'ui.sortable' ) ) ;
13+ beforeEach ( module ( 'ui.sortable.testHelper' ) ) ;
14+
15+ var EXTRA_DY_PERCENTAGE , listContent , simulateElementDrag , hasUndefinedProperties , beforeLiElement , afterLiElement ;
16+
17+ beforeEach ( inject ( function ( sortableTestHelper ) {
18+ EXTRA_DY_PERCENTAGE = sortableTestHelper . EXTRA_DY_PERCENTAGE ;
19+ listContent = sortableTestHelper . listContent ;
20+ simulateElementDrag = sortableTestHelper . simulateElementDrag ;
21+ hasUndefinedProperties = sortableTestHelper . hasUndefinedProperties ;
22+ beforeLiElement = sortableTestHelper . extraElements && sortableTestHelper . extraElements . beforeLiElement ;
23+ afterLiElement = sortableTestHelper . extraElements && sortableTestHelper . extraElements . afterLiElement ;
24+ } ) ) ;
25+
26+ tests . description = 'Events related' ;
27+ function tests ( useExtraElements ) {
28+
29+ var host ;
30+
31+ beforeEach ( inject ( function ( ) {
32+ host = $ ( '<div id="test-host"></div>' ) ;
33+ $ ( 'body' ) . append ( host ) ;
34+
35+ if ( ! useExtraElements ) {
36+ beforeLiElement = afterLiElement = '' ;
37+ }
38+ } ) ) ;
39+
40+ afterEach ( function ( ) {
41+ host . remove ( ) ;
42+ host = null ;
43+ } ) ;
44+
45+ it ( 'should emit an event after sorting' , function ( ) {
46+ inject ( function ( $compile , $rootScope ) {
47+ var element , uiParam , emittedUiParam ;
48+ element = $compile ( '' . concat (
49+ '<ul ui-sortable ui-sortable-update="update" ng-model="items">' ,
50+ beforeLiElement ,
51+ '<li ng-repeat="item in items" id="s-{{$index}}">{{ item }}</li>' ,
52+ afterLiElement ,
53+ '</ul>' ) ) ( $rootScope ) ;
54+ $rootScope . $apply ( function ( ) {
55+ $rootScope . items = [ 'One' , 'Two' , 'Three' ] ;
56+ $rootScope . update = function ( e , ui ) {
57+ uiParam = ui ;
58+ } ;
59+ $rootScope . onSorting = function ( e , ui ) {
60+ emittedUiParam = ui ;
61+ } ;
62+ spyOn ( $rootScope , 'onSorting' ) . and . callThrough ( ) ;
63+
64+ $rootScope . $on ( 'ui-sortable:moved' , $rootScope . onSorting ) ;
65+ } ) ;
66+
67+ host . append ( element ) ;
68+
69+ var li = element . find ( '[ng-repeat]:eq(0)' ) ;
70+ var dy = ( 2 + EXTRA_DY_PERCENTAGE ) * li . outerHeight ( ) ;
71+ li . simulate ( 'drag' , { dy : dy } ) ;
72+ expect ( $rootScope . items ) . toEqual ( [ 'Two' , 'Three' , 'One' ] ) ;
73+ expect ( $rootScope . items ) . toEqual ( listContent ( element ) ) ;
74+
75+ expect ( $rootScope . onSorting ) . toHaveBeenCalled ( ) ;
76+ expect ( uiParam ) . toEqual ( emittedUiParam ) ;
77+
78+ $ ( element ) . remove ( ) ;
79+ } ) ;
80+ } ) ;
81+
82+ it ( 'should emit an event after sorting between sortables of different scopes' , function ( ) {
83+ inject ( function ( $compile , $rootScope ) {
84+ var elementTop , elementBottom ,
85+ wrapperTop , wrapperBottom ,
86+ wrapperTopScope , wrapperBottomScope ,
87+ itemsTop , itemsBottom ;
88+ wrapperTopScope = $rootScope . $new ( ) ;
89+ wrapperBottomScope = $rootScope . $new ( ) ;
90+ wrapperTop = $compile ( '' . concat (
91+ '<div ng-controller="dummyController"><ul ui-sortable="opts" class="cross-sortable" ng-model="itemsTop">' ,
92+ beforeLiElement ,
93+ '<li ng-repeat="item in itemsTop" id="s-top-{{$index}}">{{ item }}</li>' ,
94+ afterLiElement ,
95+ '</ul></div>' ) ) ( wrapperTopScope ) ;
96+ wrapperBottom = $compile ( '' . concat (
97+ '<div ng-controller="dummyController"><ul ui-sortable="opts" class="cross-sortable" ng-model="itemsBottom">' ,
98+ beforeLiElement ,
99+ '<li ng-repeat="item in itemsBottom" id="s-bottom-{{$index}}">{{ item }}</li>' ,
100+ afterLiElement ,
101+ '</ul></div>' ) ) ( wrapperBottomScope ) ;
102+
103+ host . append ( wrapperTop ) . append ( wrapperBottom ) . append ( '<div class="clear"></div>' ) ;
104+ $rootScope . $apply ( function ( ) {
105+ wrapperTopScope . itemsTop = itemsTop = [ 'Top One' , 'Top Two' , 'Top Three' ] ;
106+ wrapperTopScope . opts = {
107+ connectWith : '.cross-sortable' ,
108+ stop : function ( e , ui ) {
109+ wrapperTopScope . uiParam = ui ;
110+ }
111+ } ;
112+ wrapperTopScope . onSorting = function ( e , ui ) {
113+ wrapperTopScope . emittedUiParam = ui ;
114+ } ;
115+ spyOn ( wrapperTopScope , 'onSorting' ) . and . callThrough ( ) ;
116+ $rootScope . $on ( 'ui-sortable:moved' , wrapperTopScope . onSorting ) ;
117+
118+ wrapperBottomScope . itemsBottom = itemsBottom = [ 'Bottom One' , 'Bottom Two' , 'Bottom Three' ] ;
119+ wrapperBottomScope . opts = {
120+ connectWith : '.cross-sortable' ,
121+ update : function ( e , ui ) {
122+ wrapperBottomScope . uiParam = ui ;
123+ }
124+ } ;
125+ wrapperBottomScope . onSorting = function ( e , ui ) {
126+ wrapperBottomScope . emittedUiParam = ui ;
127+ } ;
128+ spyOn ( wrapperBottomScope , 'onSorting' ) . and . callThrough ( ) ;
129+ $rootScope . $on ( 'ui-sortable:moved' , wrapperBottomScope . onSorting ) ;
130+ } ) ;
131+
132+ elementTop = wrapperTop . find ( '> [ui-sortable]' ) ;
133+ elementBottom = wrapperBottom . find ( '> [ui-sortable]' ) ;
134+
135+ var li1 = elementTop . find ( '[ng-repeat]:eq(0)' ) ;
136+ var li2 = elementBottom . find ( '[ng-repeat]:eq(0)' ) ;
137+ simulateElementDrag ( li1 , li2 , 'below' ) ;
138+ expect ( itemsTop ) . toEqual ( [ 'Top Two' , 'Top Three' ] ) ;
139+ expect ( itemsBottom ) . toEqual ( [ 'Bottom One' , 'Top One' , 'Bottom Two' , 'Bottom Three' ] ) ;
140+ expect ( itemsTop ) . toEqual ( listContent ( elementTop ) ) ;
141+ expect ( itemsBottom ) . toEqual ( listContent ( elementBottom ) ) ;
142+
143+ expect ( wrapperTopScope . onSorting ) . toHaveBeenCalled ( ) ;
144+ expect ( wrapperTopScope . uiParam . item ) . toEqual ( wrapperTopScope . emittedUiParam . item ) ;
145+
146+ expect ( wrapperBottomScope . onSorting ) . toHaveBeenCalled ( ) ;
147+ expect ( wrapperBottomScope . uiParam . item ) . toEqual ( wrapperBottomScope . emittedUiParam . item ) ;
148+
149+ $ ( wrapperBottom ) . remove ( ) ;
150+ $ ( wrapperTop ) . remove ( ) ;
151+ } ) ;
152+ } ) ;
153+ }
154+
155+ [ 0 , 1 ] . forEach ( function ( useExtraElements ) {
156+ var testDescription = tests . description ;
157+
158+ if ( useExtraElements ) {
159+ testDescription += ' with extra elements' ;
160+ }
161+
162+ describe ( testDescription , function ( ) {
163+ tests ( useExtraElements ) ;
164+ } ) ;
165+ } ) ;
166+
167+ } ) ;
0 commit comments