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

Commit b506cb0

Browse files
committed
test: add feat(sortable): attr callback implementation tests
1 parent ef5862c commit b506cb0

File tree

1 file changed

+199
-0
lines changed

1 file changed

+199
-0
lines changed
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
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, hasUndefinedProperties, beforeLiElement, afterLiElement;
16+
17+
beforeEach(inject(function (sortableTestHelper) {
18+
EXTRA_DY_PERCENTAGE = sortableTestHelper.EXTRA_DY_PERCENTAGE;
19+
listContent = sortableTestHelper.listContent;
20+
hasUndefinedProperties = sortableTestHelper.hasUndefinedProperties;
21+
beforeLiElement = sortableTestHelper.extraElements && sortableTestHelper.extraElements.beforeLiElement;
22+
afterLiElement = sortableTestHelper.extraElements && sortableTestHelper.extraElements.afterLiElement;
23+
}));
24+
25+
tests.description = 'Attribute Callbacks related';
26+
function tests (useExtraElements) {
27+
28+
var host;
29+
30+
beforeEach(inject(function() {
31+
host = $('<div id="test-host"></div>');
32+
$('body').append(host);
33+
34+
if (!useExtraElements) {
35+
beforeLiElement = afterLiElement = '';
36+
}
37+
}));
38+
39+
afterEach(function() {
40+
host.remove();
41+
host = null;
42+
});
43+
44+
it('should cancel sorting of node "Two"', function() {
45+
inject(function($compile, $rootScope) {
46+
var element;
47+
element = $compile(''.concat(
48+
'<ul ui-sortable ui-sortable-update="update" ng-model="items">',
49+
beforeLiElement,
50+
'<li ng-repeat="item in items" id="s-{{$index}}">{{ item }}</li>',
51+
afterLiElement,
52+
'</ul>'))($rootScope);
53+
$rootScope.$apply(function() {
54+
$rootScope.update = function(e, ui) {
55+
if (ui.item.sortable.model === 'Two') {
56+
ui.item.sortable.cancel();
57+
}
58+
};
59+
$rootScope.items = ['One', 'Two', 'Three'];
60+
});
61+
62+
host.append(element);
63+
64+
var li = element.find('[ng-repeat]:eq(1)');
65+
var dy = (1 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
66+
li.simulate('drag', { dy: dy });
67+
expect($rootScope.items).toEqual(['One', 'Two', 'Three']);
68+
expect($rootScope.items).toEqual(listContent(element));
69+
// try again
70+
li = element.find('[ng-repeat]:eq(1)');
71+
dy = (1 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
72+
li.simulate('drag', { dy: dy });
73+
expect($rootScope.items).toEqual(['One', 'Two', 'Three']);
74+
expect($rootScope.items).toEqual(listContent(element));
75+
// try again
76+
li = element.find('[ng-repeat]:eq(1)');
77+
dy = (1 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
78+
li.simulate('drag', { dy: dy });
79+
expect($rootScope.items).toEqual(['One', 'Two', 'Three']);
80+
expect($rootScope.items).toEqual(listContent(element));
81+
82+
li = element.find('[ng-repeat]:eq(0)');
83+
dy = (2 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
84+
li.simulate('drag', { dy: dy });
85+
expect($rootScope.items).toEqual(['Two', 'Three', 'One']);
86+
expect($rootScope.items).toEqual(listContent(element));
87+
88+
li = element.find('[ng-repeat]:eq(2)');
89+
dy = -(2 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
90+
li.simulate('drag', { dy: dy });
91+
expect($rootScope.items).toEqual(['One', 'Two', 'Three']);
92+
expect($rootScope.items).toEqual(listContent(element));
93+
94+
$(element).remove();
95+
});
96+
});
97+
98+
it('should call all callbacks with the proper context', function() {
99+
inject(function($compile, $rootScope) {
100+
var element, callbackContexts = {};
101+
$rootScope.$apply(function() {
102+
$rootScope.create = function() {
103+
callbackContexts.create = this;
104+
};
105+
// $rootScope.helper = function(e, item) {
106+
// callbackContexts.helper = this;
107+
// return item;
108+
// };
109+
$rootScope.start = function() {
110+
callbackContexts.start = this;
111+
};
112+
$rootScope.activate = function() {
113+
callbackContexts.activate = this;
114+
};
115+
$rootScope.beforeStop = function() {
116+
callbackContexts.beforeStop = this;
117+
};
118+
$rootScope.update = function() {
119+
callbackContexts.update = this;
120+
};
121+
$rootScope.deactivate = function() {
122+
callbackContexts.deactivate = this;
123+
};
124+
$rootScope.stop = function() {
125+
callbackContexts.stop = this;
126+
};
127+
128+
// spyOn($rootScope, 'helper').and.callThrough();
129+
spyOn($rootScope, 'create').and.callThrough();
130+
spyOn($rootScope, 'start').and.callThrough();
131+
spyOn($rootScope, 'activate').and.callThrough();
132+
spyOn($rootScope, 'beforeStop').and.callThrough();
133+
spyOn($rootScope, 'update').and.callThrough();
134+
spyOn($rootScope, 'deactivate').and.callThrough();
135+
spyOn($rootScope, 'stop').and.callThrough();
136+
$rootScope.items = ['One', 'Two', 'Three'];
137+
element = $compile(''.concat(
138+
'<ul ui-sortable ' +
139+
'ui-sortable-create="create" ' +
140+
// 'ui-sortable-helper="helper" ' +
141+
'ui-sortable-start="start" ' +
142+
'ui-sortable-activate="activate" ' +
143+
'ui-sortable-update="update" ' +
144+
'ui-sortable-before-stop="beforeStop" ' +
145+
'ui-sortable-deactivate="deactivate" ' +
146+
'ui-sortable-stop="stop" ' +
147+
'ng-model="items">',
148+
beforeLiElement,
149+
'<li ng-repeat="item in items" id="s-{{$index}}">{{ item }}</li>',
150+
afterLiElement +
151+
'</ul>'))($rootScope);
152+
});
153+
154+
host.append(element);
155+
156+
$rootScope.$apply(function() {
157+
});
158+
var li = element.find('[ng-repeat]:eq(0)');
159+
var dy = (2 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
160+
li.simulate('drag', { dy: dy });
161+
expect($rootScope.items).toEqual(['Two', 'Three', 'One']);
162+
expect($rootScope.items).toEqual(listContent(element));
163+
164+
expect($rootScope.create).toHaveBeenCalled();
165+
// expect($rootScope.helper).toHaveBeenCalled();
166+
expect($rootScope.start).toHaveBeenCalled();
167+
expect($rootScope.activate).toHaveBeenCalled();
168+
expect($rootScope.beforeStop).toHaveBeenCalled();
169+
expect($rootScope.update).toHaveBeenCalled();
170+
expect($rootScope.deactivate).toHaveBeenCalled();
171+
expect($rootScope.stop).toHaveBeenCalled();
172+
173+
expect(callbackContexts.create).toEqual(element[0]);
174+
// expect(callbackContexts.helper).toEqual(element[0]);
175+
expect(callbackContexts.start).toEqual(element[0]);
176+
expect(callbackContexts.activate).toEqual(element[0]);
177+
expect(callbackContexts.beforeStop).toEqual(element[0]);
178+
expect(callbackContexts.update).toEqual(element[0]);
179+
expect(callbackContexts.deactivate).toEqual(element[0]);
180+
expect(callbackContexts.stop).toEqual(element[0]);
181+
182+
$(element).remove();
183+
});
184+
});
185+
}
186+
187+
[0, 1].forEach(function(useExtraElements){
188+
var testDescription = tests.description;
189+
190+
if (useExtraElements) {
191+
testDescription += ' with extra elements';
192+
}
193+
194+
describe(testDescription, function(){
195+
tests(useExtraElements);
196+
});
197+
});
198+
199+
});

0 commit comments

Comments
 (0)