1
+ 'use strict' ;
2
+
3
+ describe ( 'uiSortable' , function ( ) {
4
+
5
+ // Ensure the sortable angular module is loaded
6
+ beforeEach ( module ( 'ui.sortable' ) ) ;
7
+ beforeEach ( module ( 'ui.sortable.testHelper' ) ) ;
8
+
9
+ var EXTRA_DY_PERCENTAGE , listContent ;
10
+
11
+ beforeEach ( inject ( function ( sortableTestHelper ) {
12
+ EXTRA_DY_PERCENTAGE = sortableTestHelper . EXTRA_DY_PERCENTAGE ;
13
+ listContent = sortableTestHelper . listContent ;
14
+ } ) ) ;
15
+
16
+ describe ( 'Callbacks related' , function ( ) {
17
+
18
+ var host ;
19
+
20
+ beforeEach ( inject ( function ( ) {
21
+ host = $ ( '<div id="test-host"></div>' ) ;
22
+ $ ( 'body' ) . append ( host ) ;
23
+ } ) ) ;
24
+
25
+ afterEach ( function ( ) {
26
+ host . remove ( ) ;
27
+ host = null ;
28
+ } ) ;
29
+
30
+ it ( 'should cancel sorting of node "Two"' , function ( ) {
31
+ inject ( function ( $compile , $rootScope ) {
32
+ var element ;
33
+ element = $compile ( '<ul ui-sortable="opts" ng-model="items"><li ng-repeat="item in items" id="s-{{$index}}">{{ item }}</li></ul>' ) ( $rootScope ) ;
34
+ $rootScope . $apply ( function ( ) {
35
+ $rootScope . opts = {
36
+ update : function ( e , ui ) {
37
+ if ( ui . item . scope ( ) . item === 'Two' ) {
38
+ ui . item . sortable . cancel ( ) ;
39
+ }
40
+ }
41
+ } ;
42
+ $rootScope . items = [ 'One' , 'Two' , 'Three' ] ;
43
+ } ) ;
44
+
45
+ host . append ( element ) ;
46
+
47
+ var li = element . find ( ':eq(1)' ) ;
48
+ var dy = ( 1 + EXTRA_DY_PERCENTAGE ) * li . outerHeight ( ) ;
49
+ li . simulate ( 'drag' , { dy : dy } ) ;
50
+ expect ( $rootScope . items ) . toEqual ( [ 'One' , 'Two' , 'Three' ] ) ;
51
+ expect ( $rootScope . items ) . toEqual ( listContent ( element ) ) ;
52
+
53
+ li = element . find ( ':eq(0)' ) ;
54
+ dy = ( 2 + EXTRA_DY_PERCENTAGE ) * li . outerHeight ( ) ;
55
+ li . simulate ( 'drag' , { dy : dy } ) ;
56
+ expect ( $rootScope . items ) . toEqual ( [ 'Two' , 'Three' , 'One' ] ) ;
57
+ expect ( $rootScope . items ) . toEqual ( listContent ( element ) ) ;
58
+
59
+ li = element . find ( ':eq(2)' ) ;
60
+ dy = - ( 2 + EXTRA_DY_PERCENTAGE ) * li . outerHeight ( ) ;
61
+ li . simulate ( 'drag' , { dy : dy } ) ;
62
+ expect ( $rootScope . items ) . toEqual ( [ 'One' , 'Two' , 'Three' ] ) ;
63
+ expect ( $rootScope . items ) . toEqual ( listContent ( element ) ) ;
64
+
65
+ $ ( element ) . remove ( ) ;
66
+ } ) ;
67
+ } ) ;
68
+
69
+ it ( 'should cancel sorting of node "Two" and "helper: function" that returns a list element is used' , function ( ) {
70
+ inject ( function ( $compile , $rootScope ) {
71
+ var element ;
72
+ element = $compile ( '<ul ui-sortable="opts" ng-model="items"><li ng-repeat="item in items" id="s-{{$index}}">{{ item }}</li></ul>' ) ( $rootScope ) ;
73
+ $rootScope . $apply ( function ( ) {
74
+ $rootScope . opts = {
75
+ update : function ( e , ui ) {
76
+ if ( ui . item . scope ( ) . item === 'Two' ) {
77
+ ui . item . sortable . cancel ( ) ;
78
+ }
79
+ }
80
+ } ;
81
+ $rootScope . items = [ 'One' , 'Two' , 'Three' ] ;
82
+ } ) ;
83
+
84
+ host . append ( element ) ;
85
+
86
+ var li = element . find ( ':eq(1)' ) ;
87
+ var dy = ( 1 + EXTRA_DY_PERCENTAGE ) * li . outerHeight ( ) ;
88
+ li . simulate ( 'drag' , { dy : dy } ) ;
89
+ expect ( $rootScope . items ) . toEqual ( [ 'One' , 'Two' , 'Three' ] ) ;
90
+ expect ( $rootScope . items ) . toEqual ( listContent ( element ) ) ;
91
+
92
+ li = element . find ( ':eq(0)' ) ;
93
+ dy = ( 2 + EXTRA_DY_PERCENTAGE ) * li . outerHeight ( ) ;
94
+ li . simulate ( 'drag' , { dy : dy } ) ;
95
+ expect ( $rootScope . items ) . toEqual ( [ 'Two' , 'Three' , 'One' ] ) ;
96
+ expect ( $rootScope . items ) . toEqual ( listContent ( element ) ) ;
97
+
98
+ li = element . find ( ':eq(2)' ) ;
99
+ dy = - ( 2 + EXTRA_DY_PERCENTAGE ) * li . outerHeight ( ) ;
100
+ li . simulate ( 'drag' , { dy : dy } ) ;
101
+ expect ( $rootScope . items ) . toEqual ( [ 'One' , 'Two' , 'Three' ] ) ;
102
+ expect ( $rootScope . items ) . toEqual ( listContent ( element ) ) ;
103
+
104
+ $ ( element ) . remove ( ) ;
105
+ } ) ;
106
+ } ) ;
107
+
108
+ it ( 'should update model from update() callback' , function ( ) {
109
+ inject ( function ( $compile , $rootScope ) {
110
+ var element , logsElement ;
111
+ element = $compile ( '<ul ui-sortable="opts" ng-model="items"><li ng-repeat="item in items" id="s-{{$index}}">{{ item }}</li></ul>' ) ( $rootScope ) ;
112
+ logsElement = $compile ( '<ul ng-model="logs"><li ng-repeat="log in logs" id="l-{{$index}}">{{ log }}</li></ul>' ) ( $rootScope ) ;
113
+ $rootScope . $apply ( function ( ) {
114
+ $rootScope . opts = {
115
+ update : function ( e , ui ) {
116
+ $rootScope . logs . push ( 'Moved element ' + ui . item . scope ( ) . item ) ;
117
+ }
118
+ } ;
119
+ $rootScope . items = [ 'One' , 'Two' , 'Three' ] ;
120
+ $rootScope . logs = [ ] ;
121
+ } ) ;
122
+
123
+ host . append ( element ) . append ( logsElement ) ;
124
+
125
+ var li = element . find ( ':eq(1)' ) ;
126
+ var dy = ( 1 + EXTRA_DY_PERCENTAGE ) * li . outerHeight ( ) ;
127
+ li . simulate ( 'drag' , { dy : dy } ) ;
128
+ expect ( $rootScope . items ) . toEqual ( [ 'One' , 'Three' , 'Two' ] ) ;
129
+ expect ( $rootScope . logs ) . toEqual ( [ 'Moved element Two' ] ) ;
130
+ expect ( $rootScope . items ) . toEqual ( listContent ( element ) ) ;
131
+ expect ( $rootScope . logs ) . toEqual ( listContent ( logsElement ) ) ;
132
+
133
+ $ ( element ) . remove ( ) ;
134
+ $ ( logsElement ) . remove ( ) ;
135
+ } ) ;
136
+ } ) ;
137
+
138
+ // ensure scope.apply() is called after a stop() callback
139
+ it ( 'should update model from stop() callback' , function ( ) {
140
+ inject ( function ( $compile , $rootScope ) {
141
+ var element , logsElement ;
142
+ element = $compile ( '<ul ui-sortable="opts" ng-model="items"><li ng-repeat="item in items" id="s-{{$index}}">{{ item }}</li></ul>' ) ( $rootScope ) ;
143
+ logsElement = $compile ( '<ul ng-model="logs"><li ng-repeat="log in logs" id="l-{{$index}}">{{ log }}</li></ul>' ) ( $rootScope ) ;
144
+ $rootScope . $apply ( function ( ) {
145
+ $rootScope . opts = {
146
+ stop : function ( e , ui ) {
147
+ $rootScope . logs . push ( 'Moved element ' + ui . item . scope ( ) . item ) ;
148
+ }
149
+ } ;
150
+ $rootScope . items = [ 'One' , 'Two' , 'Three' ] ;
151
+ $rootScope . logs = [ ] ;
152
+ } ) ;
153
+
154
+ host . append ( element ) . append ( logsElement ) ;
155
+
156
+ var li = element . find ( ':eq(1)' ) ;
157
+ var dy = ( 1 + EXTRA_DY_PERCENTAGE ) * li . outerHeight ( ) ;
158
+ li . simulate ( 'drag' , { dy : dy } ) ;
159
+ expect ( $rootScope . items ) . toEqual ( [ 'One' , 'Three' , 'Two' ] ) ;
160
+ expect ( $rootScope . logs ) . toEqual ( [ 'Moved element Two' ] ) ;
161
+ expect ( $rootScope . items ) . toEqual ( listContent ( element ) ) ;
162
+ expect ( $rootScope . logs ) . toEqual ( listContent ( logsElement ) ) ;
163
+
164
+ $ ( element ) . remove ( ) ;
165
+ $ ( logsElement ) . remove ( ) ;
166
+ } ) ;
167
+ } ) ;
168
+
169
+ it ( 'should call the create() callback when initialized' , function ( ) {
170
+ inject ( function ( $compile , $rootScope ) {
171
+ var element ;
172
+ $rootScope . $apply ( function ( ) {
173
+ $rootScope . items = [ 'One' , 'Two' , 'Three' ] ;
174
+ $rootScope . opts = {
175
+ create : function ( ) {
176
+
177
+ }
178
+ } ;
179
+ spyOn ( $rootScope . opts , 'create' ) ;
180
+ element = $compile ( '<ul ui-sortable="opts" ng-model="items"><li ng-repeat="item in items" id="s-{{$index}}">{{ item }}</li></ul>' ) ( $rootScope ) ;
181
+ } ) ;
182
+
183
+ host . append ( element ) ;
184
+
185
+ expect ( $rootScope . opts . create ) . toHaveBeenCalled ( ) ;
186
+
187
+ $ ( element ) . remove ( ) ;
188
+ } ) ;
189
+ } ) ;
190
+
191
+ } ) ;
192
+
193
+ } ) ;
0 commit comments