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

Commit 70e3b8e

Browse files
committed
refactor(tests): abstract connected lists dx&dy dragging
1 parent 54d442c commit 70e3b8e

File tree

2 files changed

+94
-46
lines changed

2 files changed

+94
-46
lines changed

test/sortable.e2e.multi.spec.js

Lines changed: 68 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ describe('uiSortable', function() {
66
beforeEach(module('ui.sortable'));
77
beforeEach(module('ui.sortable.testHelper'));
88

9-
var EXTRA_DY_PERCENTAGE, listContent, listInnerContent;
9+
var EXTRA_DY_PERCENTAGE, listContent, listInnerContent, simulateElementDrag;
1010

1111
beforeEach(inject(function (sortableTestHelper) {
1212
EXTRA_DY_PERCENTAGE = sortableTestHelper.EXTRA_DY_PERCENTAGE;
1313
listContent = sortableTestHelper.listContent;
1414
listInnerContent = sortableTestHelper.listInnerContent;
15+
simulateElementDrag = sortableTestHelper.simulateElementDrag;
1516
}));
1617

1718
describe('Multiple sortables related', function() {
@@ -43,17 +44,19 @@ describe('uiSortable', function() {
4344

4445
var li1 = elementTop.find(':eq(0)');
4546
var li2 = elementBottom.find(':eq(0)');
46-
var dy = EXTRA_DY_PERCENTAGE * li1.outerHeight() + (li2.position().top - li1.position().top);
47-
li1.simulate('drag', { dy: dy });
47+
simulateElementDrag(li1, li2, 'below');
48+
// var dy = EXTRA_DY_PERCENTAGE * li1.outerHeight() + (li2.position().top - li1.position().top);
49+
// li1.simulate('drag', { dy: dy });
4850
expect($rootScope.itemsTop).toEqual(['Top Two', 'Top Three']);
4951
expect($rootScope.itemsBottom).toEqual(['Bottom One', 'Top One', 'Bottom Two', 'Bottom Three']);
5052
expect($rootScope.itemsTop).toEqual(listContent(elementTop));
5153
expect($rootScope.itemsBottom).toEqual(listContent(elementBottom));
5254

5355
li1 = elementBottom.find(':eq(1)');
5456
li2 = elementTop.find(':eq(1)');
55-
dy = -EXTRA_DY_PERCENTAGE * li1.outerHeight() - (li1.position().top - li2.position().top);
56-
li1.simulate('drag', { dy: dy });
57+
simulateElementDrag(li1, li2, 'above');
58+
// dy = -EXTRA_DY_PERCENTAGE * li1.outerHeight() - (li1.position().top - li2.position().top);
59+
// li1.simulate('drag', { dy: dy });
5760
expect($rootScope.itemsTop).toEqual(['Top Two', 'Top One', 'Top Three']);
5861
expect($rootScope.itemsBottom).toEqual(['Bottom One', 'Bottom Two', 'Bottom Three']);
5962
expect($rootScope.itemsTop).toEqual(listContent(elementTop));
@@ -86,17 +89,19 @@ describe('uiSortable', function() {
8689

8790
var li1 = elementTop.find(':eq(0)');
8891
var li2 = elementBottom.find(':eq(0)');
89-
var dy = EXTRA_DY_PERCENTAGE * li1.outerHeight() + (li2.position().top - li1.position().top);
90-
li1.simulate('drag', { dy: dy });
92+
simulateElementDrag(li1, li2, 'below');
93+
// var dy = EXTRA_DY_PERCENTAGE * li1.outerHeight() + (li2.position().top - li1.position().top);
94+
// li1.simulate('drag', { dy: dy });
9195
expect($rootScope.itemsTop).toEqual(['Top Two', 'Top Three']);
9296
expect($rootScope.itemsBottom).toEqual(['Bottom One', 0, 'Bottom Two', 'Bottom Three']);
9397
expect($rootScope.itemsTop).toEqual(listContent(elementTop).map(parseFalsyValue));
9498
expect($rootScope.itemsBottom).toEqual(listContent(elementBottom).map(parseFalsyValue));
9599

96100
li1 = elementBottom.find(':eq(1)');
97101
li2 = elementTop.find(':eq(1)');
98-
dy = -EXTRA_DY_PERCENTAGE * li1.outerHeight() - (li1.position().top - li2.position().top);
99-
li1.simulate('drag', { dy: dy });
102+
simulateElementDrag(li1, li2, 'above');
103+
// dy = -EXTRA_DY_PERCENTAGE * li1.outerHeight() - (li1.position().top - li2.position().top);
104+
// li1.simulate('drag', { dy: dy });
100105
expect($rootScope.itemsTop).toEqual(['Top Two', 0, 'Top Three']);
101106
expect($rootScope.itemsBottom).toEqual(['Bottom One', 'Bottom Two', 'Bottom Three']);
102107
expect($rootScope.itemsTop).toEqual(listContent(elementTop).map(parseFalsyValue));
@@ -125,17 +130,19 @@ describe('uiSortable', function() {
125130

126131
var li1 = elementTop.find(':eq(0)');
127132
var li2 = elementBottom.find(':eq(0)');
128-
var dy = EXTRA_DY_PERCENTAGE * li1.outerHeight() + (li2.position().top - li1.position().top);
129-
li1.simulate('drag', { dy: dy });
133+
simulateElementDrag(li1, li2, 'below');
134+
// var dy = EXTRA_DY_PERCENTAGE * li1.outerHeight() + (li2.position().top - li1.position().top);
135+
// li1.simulate('drag', { dy: dy });
130136
expect($rootScope.itemsTop).toEqual(['Top Two', 'Top Three']);
131137
expect($rootScope.itemsBottom).toEqual(['Bottom One', 'Top One', 'Bottom Two', 'Bottom Three']);
132138
expect($rootScope.itemsTop).toEqual(listContent(elementTop));
133139
expect($rootScope.itemsBottom).toEqual(listContent(elementBottom));
134140

135141
li1 = elementBottom.find(':eq(1)');
136142
li2 = elementTop.find(':eq(1)');
137-
dy = -EXTRA_DY_PERCENTAGE * li1.outerHeight() - (li1.position().top - li2.position().top);
138-
li1.simulate('drag', { dy: dy });
143+
simulateElementDrag(li1, li2, 'above');
144+
// dy = -EXTRA_DY_PERCENTAGE * li1.outerHeight() - (li1.position().top - li2.position().top);
145+
// li1.simulate('drag', { dy: dy });
139146
expect($rootScope.itemsTop).toEqual(['Top Two', 'Top One', 'Top Three']);
140147
expect($rootScope.itemsBottom).toEqual(['Bottom One', 'Bottom Two', 'Bottom Three']);
141148
expect($rootScope.itemsTop).toEqual(listContent(elementTop));
@@ -164,17 +171,19 @@ describe('uiSortable', function() {
164171

165172
var li1 = elementTop.find(':eq(0)');
166173
var li2 = elementBottom.find(':eq(0)');
167-
var dy = EXTRA_DY_PERCENTAGE * li1.outerHeight() + (li2.position().top - li1.position().top);
168-
li1.simulate('drag', { dy: dy });
174+
simulateElementDrag(li1, li2, 'below');
175+
// var dy = EXTRA_DY_PERCENTAGE * li1.outerHeight() + (li2.position().top - li1.position().top);
176+
// li1.simulate('drag', { dy: dy });
169177
expect($rootScope.itemsTop).toEqual(['Top Two', 'Top Three']);
170178
expect($rootScope.itemsBottom).toEqual(['Bottom One', 'Top One', 'Bottom Two', 'Bottom Three']);
171179
expect($rootScope.itemsTop).toEqual(listContent(elementTop));
172180
expect($rootScope.itemsBottom).toEqual(listContent(elementBottom));
173181

174182
li1 = elementBottom.find(':eq(1)');
175183
li2 = elementTop.find(':eq(1)');
176-
dy = -EXTRA_DY_PERCENTAGE * li1.outerHeight() - (li1.position().top - li2.position().top);
177-
li1.simulate('drag', { dy: dy });
184+
simulateElementDrag(li1, li2, 'above');
185+
// dy = -EXTRA_DY_PERCENTAGE * li1.outerHeight() - (li1.position().top - li2.position().top);
186+
// li1.simulate('drag', { dy: dy });
178187
expect($rootScope.itemsTop).toEqual(['Top Two', 'Top One', 'Top Three']);
179188
expect($rootScope.itemsBottom).toEqual(['Bottom One', 'Bottom Two', 'Bottom Three']);
180189
expect($rootScope.itemsTop).toEqual(listContent(elementTop));
@@ -203,17 +212,19 @@ describe('uiSortable', function() {
203212

204213
var li1 = elementTop.find(':eq(0)');
205214
var li2 = elementBottom.find(':eq(0)');
206-
var dy = EXTRA_DY_PERCENTAGE * li1.outerHeight() + (li2.position().top - li1.position().top);
207-
li1.simulate('drag', { dy: dy });
215+
simulateElementDrag(li1, li2, 'below');
216+
// var dy = EXTRA_DY_PERCENTAGE * li1.outerHeight() + (li2.position().top - li1.position().top);
217+
// li1.simulate('drag', { dy: dy });
208218
expect($rootScope.itemsTop).toEqual(['Top Two', 'Top Three']);
209219
expect($rootScope.itemsBottom).toEqual(['Bottom One', 'Top One', 'Bottom Two', 'Bottom Three']);
210220
expect($rootScope.itemsTop).toEqual(listContent(elementTop));
211221
expect($rootScope.itemsBottom).toEqual(listContent(elementBottom));
212222

213223
li1 = elementBottom.find(':eq(1)');
214224
li2 = elementTop.find(':eq(1)');
215-
dy = -EXTRA_DY_PERCENTAGE * li1.outerHeight() - (li1.position().top - li2.position().top);
216-
li1.simulate('drag', { dy: dy });
225+
simulateElementDrag(li1, li2, 'above');
226+
// dy = -EXTRA_DY_PERCENTAGE * li1.outerHeight() - (li1.position().top - li2.position().top);
227+
// li1.simulate('drag', { dy: dy });
217228
expect($rootScope.itemsTop).toEqual(['Top Two', 'Top One', 'Top Three']);
218229
expect($rootScope.itemsBottom).toEqual(['Bottom One', 'Bottom Two', 'Bottom Three']);
219230
expect($rootScope.itemsTop).toEqual(listContent(elementTop));
@@ -243,17 +254,19 @@ describe('uiSortable', function() {
243254

244255
var li1 = elementTop.find(':eq(0)');
245256
var li2 = elementBottom.find(':eq(0)');
246-
var dy = EXTRA_DY_PERCENTAGE * li1.outerHeight() + (li2.position().top - li1.position().top);
247-
li1.simulate('drag', { dy: dy });
257+
simulateElementDrag(li1, li2, 'below');
258+
// var dy = EXTRA_DY_PERCENTAGE * li1.outerHeight() + (li2.position().top - li1.position().top);
259+
// li1.simulate('drag', { dy: dy });
248260
expect($rootScope.itemsTop).toEqual(['Top Two', 'Top Three']);
249261
expect($rootScope.itemsBottom).toEqual(['Bottom One', 'Top One', 'Bottom Two', 'Bottom Three']);
250262
expect($rootScope.itemsTop).toEqual(listContent(elementTop));
251263
expect($rootScope.itemsBottom).toEqual(listContent(elementBottom));
252264

253265
li1 = elementBottom.find(':eq(1)');
254266
li2 = elementTop.find(':eq(1)');
255-
dy = -EXTRA_DY_PERCENTAGE * li1.outerHeight() - (li1.position().top - li2.position().top);
256-
li1.simulate('drag', { dy: dy });
267+
simulateElementDrag(li1, li2, 'above');
268+
// dy = -EXTRA_DY_PERCENTAGE * li1.outerHeight() - (li1.position().top - li2.position().top);
269+
// li1.simulate('drag', { dy: dy });
257270
expect($rootScope.itemsTop).toEqual(['Top Two', 'Top One', 'Top Three']);
258271
expect($rootScope.itemsBottom).toEqual(['Bottom One', 'Bottom Two', 'Bottom Three']);
259272
expect($rootScope.itemsTop).toEqual(listContent(elementTop));
@@ -284,17 +297,19 @@ describe('uiSortable', function() {
284297

285298
var li1 = elementTop.find(':eq(0)');
286299
var li2 = elementBottom.find(':eq(0)');
287-
var dy = EXTRA_DY_PERCENTAGE * li1.outerHeight() + (li2.position().top - li1.position().top);
288-
li1.simulate('drag', { dy: dy });
300+
simulateElementDrag(li1, li2, 'below');
301+
// var dy = EXTRA_DY_PERCENTAGE * li1.outerHeight() + (li2.position().top - li1.position().top);
302+
// li1.simulate('drag', { dy: dy });
289303
expect($rootScope.itemsTop).toEqual(['Top Two', 'Top Three']);
290304
expect($rootScope.itemsBottom).toEqual(['Bottom One', 'Top One', 'Bottom Two', 'Bottom Three']);
291305
expect($rootScope.itemsTop).toEqual(listContent(elementTop));
292306
expect($rootScope.itemsBottom).toEqual(listContent(elementBottom));
293307

294308
li1 = elementBottom.find(':eq(1)');
295309
li2 = elementTop.find(':eq(1)');
296-
dy = -EXTRA_DY_PERCENTAGE * li1.outerHeight() - (li1.position().top - li2.position().top);
297-
li1.simulate('drag', { dy: dy });
310+
simulateElementDrag(li1, li2, 'above');
311+
// dy = -EXTRA_DY_PERCENTAGE * li1.outerHeight() - (li1.position().top - li2.position().top);
312+
// li1.simulate('drag', { dy: dy });
298313
expect($rootScope.itemsTop).toEqual(['Top Two', 'Top One', 'Top Three']);
299314
expect($rootScope.itemsBottom).toEqual(['Bottom One', 'Bottom Two', 'Bottom Three']);
300315
expect($rootScope.itemsTop).toEqual(listContent(elementTop));
@@ -326,17 +341,19 @@ describe('uiSortable', function() {
326341

327342
var li1 = elementTop.find(':eq(0)');
328343
var li2 = elementBottom.find(':eq(0)');
329-
var dy = EXTRA_DY_PERCENTAGE * li1.outerHeight() + (li2.position().top - li1.position().top);
330-
li1.simulate('drag', { dy: dy });
344+
simulateElementDrag(li1, li2, 'below');
345+
// var dy = EXTRA_DY_PERCENTAGE * li1.outerHeight() + (li2.position().top - li1.position().top);
346+
// li1.simulate('drag', { dy: dy });
331347
expect($rootScope.itemsTop).toEqual(['Top Two', 'Top Three']);
332348
expect($rootScope.itemsBottom).toEqual(['Bottom One', 'Top One', 'Bottom Two', 'Bottom Three']);
333349
expect($rootScope.itemsTop).toEqual(listContent(elementTop));
334350
expect($rootScope.itemsBottom).toEqual(listContent(elementBottom));
335351

336352
li1 = elementBottom.find(':eq(1)');
337353
li2 = elementTop.find(':eq(1)');
338-
dy = -EXTRA_DY_PERCENTAGE * li1.outerHeight() - (li1.position().top - li2.position().top);
339-
li1.simulate('drag', { dy: dy });
354+
simulateElementDrag(li1, li2, 'above');
355+
// dy = -EXTRA_DY_PERCENTAGE * li1.outerHeight() - (li1.position().top - li2.position().top);
356+
// li1.simulate('drag', { dy: dy });
340357
expect($rootScope.itemsTop).toEqual(['Top Two', 'Top One', 'Top Three']);
341358
expect($rootScope.itemsBottom).toEqual(['Bottom One', 'Bottom Two', 'Bottom Three']);
342359
expect($rootScope.itemsTop).toEqual(listContent(elementTop));
@@ -367,17 +384,19 @@ describe('uiSortable', function() {
367384

368385
var li1 = elementTop.find(':eq(0)');
369386
var li2 = elementBottom.find(':eq(0)');
370-
var dy = EXTRA_DY_PERCENTAGE * li1.outerHeight() + (li2.position().top - li1.position().top);
371-
li1.simulate('drag', { dy: dy });
387+
simulateElementDrag(li1, li2, 'below');
388+
// var dy = EXTRA_DY_PERCENTAGE * li1.outerHeight() + (li2.position().top - li1.position().top);
389+
// li1.simulate('drag', { dy: dy });
372390
expect($rootScope.itemsTop).toEqual(['Top Two', 'Top Three']);
373391
expect($rootScope.itemsBottom).toEqual(['Bottom One', 'Top One', 'Bottom Two', 'Bottom Three']);
374392
expect($rootScope.itemsTop).toEqual(listContent(elementTop));
375393
expect($rootScope.itemsBottom).toEqual(listContent(elementBottom));
376394

377395
li1 = elementBottom.find(':eq(1)');
378396
li2 = elementTop.find(':eq(1)');
379-
dy = -EXTRA_DY_PERCENTAGE * li1.outerHeight() - (li1.position().top - li2.position().top);
380-
li1.simulate('drag', { dy: dy });
397+
simulateElementDrag(li1, li2, 'above');
398+
// dy = -EXTRA_DY_PERCENTAGE * li1.outerHeight() - (li1.position().top - li2.position().top);
399+
// li1.simulate('drag', { dy: dy });
381400
expect($rootScope.itemsTop).toEqual(['Top Two', 'Top One', 'Top Three']);
382401
expect($rootScope.itemsBottom).toEqual(['Bottom One', 'Bottom Two', 'Bottom Three']);
383402
expect($rootScope.itemsTop).toEqual(listContent(elementTop));
@@ -418,8 +437,9 @@ describe('uiSortable', function() {
418437

419438
li1 = elementBottom.find(':eq(1)');
420439
li2 = elementTop.find(':eq(1)');
421-
dy = -EXTRA_DY_PERCENTAGE * li1.outerHeight() - (li1.position().top - li2.position().top);
422-
li1.simulate('drag', { dy: dy });
440+
simulateElementDrag(li1, li2, 'above');
441+
// dy = -EXTRA_DY_PERCENTAGE * li1.outerHeight() - (li1.position().top - li2.position().top);
442+
// li1.simulate('drag', { dy: dy });
423443
expect($rootScope.itemsTop).toEqual(['Top Two', 'Top One', 'Top Three']);
424444
expect($rootScope.itemsBottom).toEqual(['Bottom One', 'Bottom Two', 'Bottom Three']);
425445
expect($rootScope.itemsTop).toEqual(listContent(elementTop));
@@ -567,26 +587,29 @@ describe('uiSortable', function() {
567587

568588
li1 = elementBottom.find(':eq(1)');
569589
li2 = elementTop.find(':eq(1)');
570-
dy = -EXTRA_DY_PERCENTAGE * li1.outerHeight() - (li1.position().top - li2.position().top);
571-
li1.simulate('drag', { dy: dy });
590+
simulateElementDrag(li1, li2, 'above');
591+
// dy = -EXTRA_DY_PERCENTAGE * li1.outerHeight() - (li1.position().top - li2.position().top);
592+
// li1.simulate('drag', { dy: dy });
572593
expect($rootScope.itemsTop).toEqual(['Top One', 'Top Two', 'Top Three']);
573594
expect($rootScope.itemsBottom).toEqual(['Bottom One', 'Bottom Two', 'Bottom Three']);
574595
expect($rootScope.itemsTop).toEqual(listContent(elementTop));
575596
expect($rootScope.itemsBottom).toEqual(listContent(elementBottom));
576597

577598
li1 = elementTop.find(':eq(0)');
578599
li2 = elementBottom.find(':eq(0)');
579-
dy = EXTRA_DY_PERCENTAGE * li1.outerHeight() + (li2.position().top - li1.position().top);
580-
li1.simulate('drag', { dy: dy });
600+
simulateElementDrag(li1, li2, 'below');
601+
// dy = EXTRA_DY_PERCENTAGE * li1.outerHeight() + (li2.position().top - li1.position().top);
602+
// li1.simulate('drag', { dy: dy });
581603
expect($rootScope.itemsTop).toEqual(['Top Two', 'Top Three']);
582604
expect($rootScope.itemsBottom).toEqual(['Bottom One', 'Top One', 'Bottom Two', 'Bottom Three']);
583605
expect($rootScope.itemsTop).toEqual(listContent(elementTop));
584606
expect($rootScope.itemsBottom).toEqual(listContent(elementBottom));
585607

586608
li1 = elementBottom.find(':eq(1)');
587609
li2 = elementTop.find(':eq(1)');
588-
dy = -EXTRA_DY_PERCENTAGE * li1.outerHeight() - (li1.position().top - li2.position().top);
589-
li1.simulate('drag', { dy: dy });
610+
simulateElementDrag(li1, li2, 'above');
611+
// dy = -EXTRA_DY_PERCENTAGE * li1.outerHeight() - (li1.position().top - li2.position().top);
612+
// li1.simulate('drag', { dy: dy });
590613
expect($rootScope.itemsTop).toEqual(['Top Two', 'Top One', 'Top Three']);
591614
expect($rootScope.itemsBottom).toEqual(['Bottom One', 'Bottom Two', 'Bottom Three']);
592615
expect($rootScope.itemsTop).toEqual(listContent(elementTop));

test/sortable.test-helper.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,34 @@ angular.module('ui.sortable.testHelper', [])
2222
return [];
2323
}
2424

25+
function simulateElementDrag(draggedElement, dropTarget, options) {
26+
var dragOptions = {
27+
dx: dropTarget.position().left - draggedElement.position().left,
28+
dy: dropTarget.position().top - draggedElement.position().top
29+
};
30+
31+
if (options === 'above') {
32+
dragOptions.dy -= EXTRA_DY_PERCENTAGE * draggedElement.outerHeight();
33+
} else if (options === 'below') {
34+
dragOptions.dy += EXTRA_DY_PERCENTAGE * draggedElement.outerHeight();
35+
} else if (typeof options === 'object') {
36+
37+
if (isFinite(options.dy)) {
38+
dragOptions.dy += options.dy;
39+
}
40+
41+
if (isFinite(options.dx)) {
42+
dragOptions.dx += options.dx;
43+
}
44+
}
45+
46+
draggedElement.simulate('drag', dragOptions);
47+
}
48+
2549
return {
2650
EXTRA_DY_PERCENTAGE: EXTRA_DY_PERCENTAGE,
2751
listContent: listContent,
28-
listInnerContent: listInnerContent
52+
listInnerContent: listInnerContent,
53+
simulateElementDrag: simulateElementDrag
2954
};
3055
});

0 commit comments

Comments
 (0)