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

Commit b03735d

Browse files
committed
test(sortable): make all tests run with&without extra elements
test(sortable): reverting sortings on connected lists with helper refactor(extraElements.spec): ease merging with the rest test cases refactor(e2e.specs): change all tests to run with&without extra elements
1 parent 758a1e2 commit b03735d

7 files changed

+780
-996
lines changed

test/sortable.e2e.callbacks.spec.js

Lines changed: 96 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,28 @@ describe('uiSortable', function() {
1212
beforeEach(module('ui.sortable'));
1313
beforeEach(module('ui.sortable.testHelper'));
1414

15-
var EXTRA_DY_PERCENTAGE, listContent, hasUndefinedProperties;
15+
var EXTRA_DY_PERCENTAGE, listContent, hasUndefinedProperties, beforeLiElement, afterLiElement;
1616

1717
beforeEach(inject(function (sortableTestHelper) {
1818
EXTRA_DY_PERCENTAGE = sortableTestHelper.EXTRA_DY_PERCENTAGE;
1919
listContent = sortableTestHelper.listContent;
2020
hasUndefinedProperties = sortableTestHelper.hasUndefinedProperties;
21+
beforeLiElement = sortableTestHelper.extraElements && sortableTestHelper.extraElements.beforeLiElement;
22+
afterLiElement = sortableTestHelper.extraElements && sortableTestHelper.extraElements.afterLiElement;
2123
}));
2224

23-
describe('Callbacks related', function() {
25+
tests.description = 'Callbacks related';
26+
function tests (useExtraElements) {
2427

2528
var host;
2629

2730
beforeEach(inject(function() {
2831
host = $('<div id="test-host"></div>');
2932
$('body').append(host);
33+
34+
if (!useExtraElements) {
35+
beforeLiElement = afterLiElement = '';
36+
}
3037
}));
3138

3239
afterEach(function() {
@@ -37,7 +44,12 @@ describe('uiSortable', function() {
3744
it('should cancel sorting of node "Two"', function() {
3845
inject(function($compile, $rootScope) {
3946
var element;
40-
element = $compile('<ul ui-sortable="opts" ng-model="items"><li ng-repeat="item in items" id="s-{{$index}}">{{ item }}</li></ul>')($rootScope);
47+
element = $compile(''.concat(
48+
'<ul ui-sortable="opts" ng-model="items">',
49+
beforeLiElement,
50+
'<li ng-repeat="item in items" id="s-{{$index}}">{{ item }}</li>',
51+
afterLiElement,
52+
'</ul>'))($rootScope);
4153
$rootScope.$apply(function() {
4254
$rootScope.opts = {
4355
update: function(e, ui) {
@@ -51,19 +63,19 @@ describe('uiSortable', function() {
5163

5264
host.append(element);
5365

54-
var li = element.find(':eq(1)');
66+
var li = element.find('[ng-repeat]:eq(1)');
5567
var dy = (1 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
5668
li.simulate('drag', { dy: dy });
5769
expect($rootScope.items).toEqual(['One', 'Two', 'Three']);
5870
expect($rootScope.items).toEqual(listContent(element));
5971

60-
li = element.find(':eq(0)');
72+
li = element.find('[ng-repeat]:eq(0)');
6173
dy = (2 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
6274
li.simulate('drag', { dy: dy });
6375
expect($rootScope.items).toEqual(['Two', 'Three', 'One']);
6476
expect($rootScope.items).toEqual(listContent(element));
6577

66-
li = element.find(':eq(2)');
78+
li = element.find('[ng-repeat]:eq(2)');
6779
dy = -(2 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
6880
li.simulate('drag', { dy: dy });
6981
expect($rootScope.items).toEqual(['One', 'Two', 'Three']);
@@ -76,7 +88,12 @@ describe('uiSortable', function() {
7688
it('should cancel sorting of node "Two" and "helper: function" that returns a list element is used', function() {
7789
inject(function($compile, $rootScope) {
7890
var element;
79-
element = $compile('<ul ui-sortable="opts" ng-model="items"><li ng-repeat="item in items" id="s-{{$index}}">{{ item }}</li></ul>')($rootScope);
91+
element = $compile(''.concat(
92+
'<ul ui-sortable="opts" ng-model="items">',
93+
beforeLiElement,
94+
'<li ng-repeat="item in items" id="s-{{$index}}">{{ item }}</li>',
95+
afterLiElement +
96+
'</ul>'))($rootScope);
8097
$rootScope.$apply(function() {
8198
$rootScope.opts = {
8299
helper: function (e, item) {
@@ -93,19 +110,19 @@ describe('uiSortable', function() {
93110

94111
host.append(element);
95112

96-
var li = element.find(':eq(1)');
113+
var li = element.find('[ng-repeat]:eq(1)');
97114
var dy = (1 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
98115
li.simulate('drag', { dy: dy });
99116
expect($rootScope.items).toEqual(['One', 'Two', 'Three']);
100117
expect($rootScope.items).toEqual(listContent(element));
101118

102-
li = element.find(':eq(0)');
119+
li = element.find('[ng-repeat]:eq(0)');
103120
dy = (2 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
104121
li.simulate('drag', { dy: dy });
105122
expect($rootScope.items).toEqual(['Two', 'Three', 'One']);
106123
expect($rootScope.items).toEqual(listContent(element));
107124

108-
li = element.find(':eq(2)');
125+
li = element.find('[ng-repeat]:eq(2)');
109126
dy = -(2 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
110127
li.simulate('drag', { dy: dy });
111128
expect($rootScope.items).toEqual(['One', 'Two', 'Three']);
@@ -118,8 +135,18 @@ describe('uiSortable', function() {
118135
it('should update model from update() callback', function() {
119136
inject(function($compile, $rootScope) {
120137
var element, logsElement;
121-
element = $compile('<ul ui-sortable="opts" ng-model="items"><li ng-repeat="item in items" id="s-{{$index}}">{{ item }}</li></ul>')($rootScope);
122-
logsElement = $compile('<ul ng-model="logs"><li ng-repeat="log in logs" id="l-{{$index}}">{{ log }}</li></ul>')($rootScope);
138+
element = $compile(''.concat(
139+
'<ul ui-sortable="opts" ng-model="items">',
140+
beforeLiElement,
141+
'<li ng-repeat="item in items" id="s-{{$index}}">{{ item }}</li>',
142+
afterLiElement +
143+
'</ul>'))($rootScope);
144+
logsElement = $compile(''.concat(
145+
'<ul ng-model="logs">',
146+
beforeLiElement,
147+
'<li ng-repeat="log in logs" id="l-{{$index}}">{{ log }}</li>',
148+
afterLiElement +
149+
'</ul>'))($rootScope);
123150
$rootScope.$apply(function() {
124151
$rootScope.opts = {
125152
update: function(e, ui) {
@@ -132,7 +159,7 @@ describe('uiSortable', function() {
132159

133160
host.append(element).append(logsElement);
134161

135-
var li = element.find(':eq(1)');
162+
var li = element.find('[ng-repeat]:eq(1)');
136163
var dy = (1 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
137164
li.simulate('drag', { dy: dy });
138165
expect($rootScope.items).toEqual(['One', 'Three', 'Two']);
@@ -149,8 +176,18 @@ describe('uiSortable', function() {
149176
it('should update model from stop() callback', function() {
150177
inject(function($compile, $rootScope) {
151178
var element, logsElement;
152-
element = $compile('<ul ui-sortable="opts" ng-model="items"><li ng-repeat="item in items" id="s-{{$index}}">{{ item }}</li></ul>')($rootScope);
153-
logsElement = $compile('<ul ng-model="logs"><li ng-repeat="log in logs" id="l-{{$index}}">{{ log }}</li></ul>')($rootScope);
179+
element = $compile(''.concat(
180+
'<ul ui-sortable="opts" ng-model="items">',
181+
beforeLiElement,
182+
'<li ng-repeat="item in items" id="s-{{$index}}">{{ item }}</li>',
183+
afterLiElement +
184+
'</ul>'))($rootScope);
185+
logsElement = $compile(''.concat(
186+
'<ul ng-model="logs">',
187+
beforeLiElement,
188+
'<li ng-repeat="log in logs" id="l-{{$index}}">{{ log }}</li>',
189+
afterLiElement +
190+
'</ul>'))($rootScope);
154191
$rootScope.$apply(function() {
155192
$rootScope.opts = {
156193
stop: function(e, ui) {
@@ -163,7 +200,7 @@ describe('uiSortable', function() {
163200

164201
host.append(element).append(logsElement);
165202

166-
var li = element.find(':eq(1)');
203+
var li = element.find('[ng-repeat]:eq(1)');
167204
var dy = (1 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
168205
li.simulate('drag', { dy: dy });
169206
expect($rootScope.items).toEqual(['One', 'Three', 'Two']);
@@ -187,7 +224,12 @@ describe('uiSortable', function() {
187224
}
188225
};
189226
spyOn($rootScope.opts, 'create');
190-
element = $compile('<ul ui-sortable="opts" ng-model="items"><li ng-repeat="item in items" id="s-{{$index}}">{{ item }}</li></ul>')($rootScope);
227+
element = $compile(''.concat(
228+
'<ul ui-sortable="opts" ng-model="items">',
229+
beforeLiElement,
230+
'<li ng-repeat="item in items" id="s-{{$index}}">{{ item }}</li>',
231+
afterLiElement +
232+
'</ul>'))($rootScope);
191233
});
192234

193235
host.append(element);
@@ -201,7 +243,12 @@ describe('uiSortable', function() {
201243
it('should properly set ui.item.sortable properties', function() {
202244
inject(function($compile, $rootScope) {
203245
var element, updateCallbackExpectations;
204-
element = $compile('<ul ui-sortable="opts" ng-model="items"><li ng-repeat="item in items" id="s-{{$index}}">{{ item }}</li></ul>')($rootScope);
246+
element = $compile(''.concat(
247+
'<ul ui-sortable="opts" ng-model="items">',
248+
beforeLiElement,
249+
'<li ng-repeat="item in items" id="s-{{$index}}">{{ item }}</li>',
250+
afterLiElement +
251+
'</ul>'))($rootScope);
205252
$rootScope.$apply(function() {
206253
$rootScope.opts = {
207254
update: function(e, ui) {
@@ -218,7 +265,7 @@ describe('uiSortable', function() {
218265

219266
$rootScope.$apply(function() {
220267
});
221-
var li = element.find(':eq(1)');
268+
var li = element.find('[ng-repeat]:eq(1)');
222269
var dy = (1 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
223270
updateCallbackExpectations = function(uiItemSortable) {
224271
expect(uiItemSortable.model).toEqual('Two');
@@ -234,7 +281,7 @@ describe('uiSortable', function() {
234281
expect($rootScope.items).toEqual(listContent(element));
235282
updateCallbackExpectations = undefined;
236283

237-
li = element.find(':eq(0)');
284+
li = element.find('[ng-repeat]:eq(0)');
238285
dy = (2 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
239286
updateCallbackExpectations = function(uiItemSortable) {
240287
expect(uiItemSortable.model).toEqual('One');
@@ -250,7 +297,7 @@ describe('uiSortable', function() {
250297
expect($rootScope.items).toEqual(listContent(element));
251298
updateCallbackExpectations = undefined;
252299

253-
li = element.find(':eq(2)');
300+
li = element.find('[ng-repeat]:eq(2)');
254301
dy = -(2 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
255302
updateCallbackExpectations = function(uiItemSortable) {
256303
expect(uiItemSortable.model).toEqual('One');
@@ -310,14 +357,19 @@ describe('uiSortable', function() {
310357
spyOn($rootScope.opts, 'deactivate').andCallThrough();
311358
spyOn($rootScope.opts, 'stop').andCallThrough();
312359
$rootScope.items = ['One', 'Two', 'Three'];
313-
element = $compile('<ul ui-sortable="opts" ng-model="items"><li ng-repeat="item in items" id="s-{{$index}}">{{ item }}</li></ul>')($rootScope);
360+
element = $compile(''.concat(
361+
'<ul ui-sortable="opts" ng-model="items">',
362+
beforeLiElement,
363+
'<li ng-repeat="item in items" id="s-{{$index}}">{{ item }}</li>',
364+
afterLiElement +
365+
'</ul>'))($rootScope);
314366
});
315367

316368
host.append(element);
317369

318370
$rootScope.$apply(function() {
319371
});
320-
var li = element.find(':eq(0)');
372+
var li = element.find('[ng-repeat]:eq(0)');
321373
var dy = (2 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
322374
li.simulate('drag', { dy: dy });
323375
expect($rootScope.items).toEqual(['Two', 'Three', 'One']);
@@ -348,7 +400,12 @@ describe('uiSortable', function() {
348400
it('should properly free ui.item.sortable object', function() {
349401
inject(function($compile, $rootScope) {
350402
var element, uiItem, uiItemSortable_Destroy;
351-
element = $compile('<ul ui-sortable="opts" ng-model="items"><li ng-repeat="item in items" id="s-{{$index}}">{{ item }}</li></ul>')($rootScope);
403+
element = $compile(''.concat(
404+
'<ul ui-sortable="opts" ng-model="items">',
405+
beforeLiElement,
406+
'<li ng-repeat="item in items" id="s-{{$index}}">{{ item }}</li>',
407+
afterLiElement +
408+
'</ul>'))($rootScope);
352409
$rootScope.$apply(function() {
353410
$rootScope.opts = {
354411
start: function (e, ui) {
@@ -367,7 +424,7 @@ describe('uiSortable', function() {
367424

368425
host.append(element);
369426

370-
var li = element.find(':eq(1)');
427+
var li = element.find('[ng-repeat]:eq(1)');
371428
var dy = (1 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
372429
li.simulate('drag', { dy: dy });
373430
expect($rootScope.items).toEqual(['One', 'Two', 'Three']);
@@ -377,7 +434,7 @@ describe('uiSortable', function() {
377434
uiItem = uiItemSortable_Destroy = undefined;
378435

379436

380-
li = element.find(':eq(0)');
437+
li = element.find('[ng-repeat]:eq(0)');
381438
dy = (2 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
382439
li.simulate('drag', { dy: dy });
383440
expect($rootScope.items).toEqual(['Two', 'Three', 'One']);
@@ -387,7 +444,7 @@ describe('uiSortable', function() {
387444
uiItem = uiItemSortable_Destroy = undefined;
388445

389446

390-
li = element.find(':eq(2)');
447+
li = element.find('[ng-repeat]:eq(2)');
391448
dy = -(2 + EXTRA_DY_PERCENTAGE) * li.outerHeight();
392449
li.simulate('drag', { dy: dy });
393450
expect($rootScope.items).toEqual(['One', 'Two', 'Three']);
@@ -400,6 +457,18 @@ describe('uiSortable', function() {
400457
});
401458
});
402459

460+
}
461+
462+
[0, 1].forEach(function(useExtraElements){
463+
var testDescription = tests.description;
464+
465+
if (useExtraElements) {
466+
testDescription += ' with extra elements';
467+
}
468+
469+
describe(testDescription, function(){
470+
tests(useExtraElements);
471+
});
403472
});
404473

405474
});

0 commit comments

Comments
 (0)