Skip to content
This repository was archived by the owner on Sep 8, 2020. It is now read-only.

Commit b488648

Browse files
committed
Merge pull request #53 from thgreasi/tests
Added new section for callbacks testing.
2 parents 1171e41 + f4ac9cc commit b488648

File tree

1 file changed

+110
-39
lines changed

1 file changed

+110
-39
lines changed

test/sortable.spec.js

Lines changed: 110 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ describe('uiSortable', function() {
55

66
var EXTRA_DY_PERCENTAGE = 0.25;
77

8-
describe('simple use', function() {
8+
describe('Simple use', function() {
99

1010
it('should have a ui-sortable class', function() {
1111
inject(function($compile, $rootScope) {
@@ -30,6 +30,7 @@ describe('uiSortable', function() {
3030
});
3131

3232
});
33+
3334

3435
describe('Drag & Drop simulation', function() {
3536

@@ -69,49 +70,12 @@ describe('uiSortable', function() {
6970
});
7071
});
7172

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-
});
86-
87-
host.append(element);
88-
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-
10973
it('should not allow sorting of "locked" nodes', function() {
11074
inject(function($compile, $rootScope) {
11175
var element;
11276
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);
11377
$rootScope.$apply(function() {
114-
$rootScope.opts ={
78+
$rootScope.opts = {
11579
items:'> .sortable'
11680
};
11781
$rootScope.items = [
@@ -188,4 +152,111 @@ describe('uiSortable', function() {
188152

189153
});
190154

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 = [];
244+
});
245+
246+
host.append(element).append(logsElement);
247+
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");
254+
255+
$(element).remove();
256+
$(logsElement).remove();
257+
});
258+
});
259+
260+
});
261+
191262
});

0 commit comments

Comments
 (0)