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

Commit 6f92022

Browse files
committed
Added extra test cases.
1 parent 8117b08 commit 6f92022

File tree

1 file changed

+131
-6
lines changed

1 file changed

+131
-6
lines changed

test/sortable.spec.js

Lines changed: 131 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ describe('uiSortable', function() {
33
// Ensure the sortable angular module is loaded
44
beforeEach(module('ui.sortable'));
55

6+
var EXTRA_DY_PERCENTAGE = 0.25;
7+
68
describe('simple use', function() {
79

810
it('should have a ui-sortable class', function() {
@@ -13,26 +15,149 @@ describe('uiSortable', function() {
1315
});
1416
});
1517

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+
describe('Drag & Drop simulation', function() {
35+
36+
var host;
37+
38+
beforeEach(inject(function() {
39+
host = $('<div id="test-host"></div>');
40+
$('body').append(host);
41+
}));
42+
43+
afterEach(function() {
44+
host.remove();
45+
host = null;
46+
});
47+
1648
it('should update model when order changes', function() {
1749
inject(function($compile, $rootScope) {
1850
var element;
1951
element = $compile('<ul ui-sortable ng-model="items"><li ng-repeat="item in items" id="s-{{$index}}">{{ item }}</li></ul>')($rootScope);
2052
$rootScope.$apply(function() {
21-
return $rootScope.items = ["One", "Two", "Three"];
53+
$rootScope.items = ["One", "Two", "Three"];
2254
});
2355

24-
element.find('li:eq(1)').insertAfter(element.find('li:eq(2)'));
56+
host.append(element);
2557

26-
$('body').append(element);
2758
var li = element.find(':eq(1)');
28-
var dy = 1.25 * li.outerHeight();
29-
li.simulate('drag', { dx: 0, dy: dy });
30-
59+
var dy = (1 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
60+
li.simulate('drag', { dy: dy });
3161
expect($rootScope.items).toEqual(["One", "Three", "Two"]);
62+
63+
li = element.find(':eq(1)');
64+
dy = -(1 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
65+
li.simulate('drag', { dy: dy });
66+
expect($rootScope.items).toEqual(["Three", "One", "Two"]);
67+
68+
$(element).remove();
69+
});
70+
});
71+
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+
3299
$(element).remove();
33100
});
34101
});
35102

103+
it('should update model from stop() callback', function() {
104+
inject(function($compile, $rootScope) {
105+
// TODO
106+
});
107+
});
108+
109+
it('should not allow sorting of "locked" nodes', function() {
110+
inject(function($compile, $rootScope) {
111+
var element;
112+
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);
113+
$rootScope.$apply(function() {
114+
$rootScope.opts ={
115+
items:'> .sortable'
116+
};
117+
$rootScope.items = [
118+
{ text: "One", sortable: true },
119+
{ text: "Two", sortable: true },
120+
{ text: "Three", sortable: false },
121+
{ text: "Four", sortable: true }
122+
];
123+
});
124+
125+
host.append(element);
126+
127+
var li = element.find(':eq(2)');
128+
var dy = (1 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
129+
li.simulate('drag', { dy: dy });
130+
expect($rootScope.items.map(function(x){ return x.text; })).toEqual(["One", "Two", "Three", "Four"]);
131+
132+
li = element.find(':eq(1)');
133+
dy = (2 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
134+
li.simulate('drag', { dy: dy });
135+
expect($rootScope.items.map(function(x){ return x.text; })).toEqual(["One", "Three", "Four", "Two"]);
136+
137+
// // fails on angular 1.2
138+
// li = element.find(':eq(2)');
139+
// dy = -(2 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
140+
// li.simulate('drag', { dy: dy });
141+
// expect($rootScope.items.map(function(x){ return x.text; })).toEqual(["Four", "One", "Three", "Two"]);
142+
143+
// // fails on angular 1.2
144+
// li = element.find(':eq(3)');
145+
// dy = -(2 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
146+
// li.simulate('drag', { dy: dy });
147+
// expect($rootScope.items.map(function(x){ return x.text; })).toEqual(["Four", "Two", "One", "Three"]);
148+
149+
// also placing right above the locked node seems a bit harder !?!?
150+
151+
$(element).remove();
152+
});
153+
});
154+
155+
it('should update model when sorting between sortables', function() {
156+
inject(function($compile, $rootScope) {
157+
// TODO
158+
});
159+
});
160+
36161
});
37162

38163
});

0 commit comments

Comments
 (0)