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

Commit 05d44b7

Browse files
committed
Merge pull request #51 from thgreasi/tests
Initial testing implementation.
2 parents ae252a8 + 6f92022 commit 05d44b7

File tree

3 files changed

+136
-11
lines changed

3 files changed

+136
-11
lines changed

bower.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
],
1818
"dependencies": {
1919
"angular": "~1.x",
20-
"jquery-ui": ">= 1.9"
20+
"jquery-ui": ">= 1.9",
21+
"jquery-simulate": "latest"
2122
},
2223
"devDependencies": {
2324
"angular-mocks": "~1.x"

test/sortable.spec.js

Lines changed: 133 additions & 10 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,25 +15,146 @@ 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);
57+
58+
var li = element.find(':eq(1)');
59+
var dy = (1 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
60+
li.simulate('drag', { dy: dy });
61+
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+
});
2586

26-
// None of this work, one way is to use .bind("sortupdate")
27-
// and then use .trigger("sortupdate", e, ui) but I have no idea how to
28-
// construct ui object
29-
30-
// element.sortable('refresh')
31-
// element.sortable('refreshPositions')
32-
// element.trigger('sortupdate')
87+
host.append(element);
3388

34-
// expect($rootScope.items).toEqual(["One", "Three", "Two"])
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+
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
35158
});
36159
});
37160

test/test.conf.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ files = [
33
JASMINE,
44
JASMINE_ADAPTER,
55
'bower_components/jquery/jquery.js',
6+
'bower_components/jquery-simulate/jquery.simulate.js',
67
'bower_components/jquery-ui/ui/jquery-ui.js',
78
'bower_components/angular/angular.js',
89
'bower_components/angular-mocks/angular-mocks.js',

0 commit comments

Comments
 (0)