/
drag_sort_spec.js
127 lines (99 loc) · 3.67 KB
/
drag_sort_spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
describe('DragSort', function() {
var $ = require('component-jquery');
var expect = chai.expect;
var dragSort = require('drag_sort');
var list, originalEvent;
beforeEach(function() {
var html = '<ul id="list" style="position: absolute; left: 0; top: 0; width: 120px; list-style-type: 0; padding: 0; margin: 0">' +
'<li class="list_item" style="float:left; width: 30px; height: 30px; list-style-type: none; padding: 0; margin: 0;">Item 2</li>' +
'<li class="list_item" style="float:left; width: 30px; height: 30px; list-style-type: none; padding: 0; margin: 0;">Item 1</li>' +
'<li class="list_item" style="float:left; width: 30px; height: 30px; list-style-type: none; padding: 0; margin: 0;">Item 3</li>' +
'<li class="list_item" style="float:left; width: 30px; height: 30px; list-style-type: none; padding: 0; margin: 0;">Item 4</li>' +
'</ul>';
$(document.body).append(html);
list = $('#list');
});
afterEach(function() {
list.remove();
});
function dragStart(element) {
var e = $.Event('dragstart');
e.originalEvent = { dataTransfer: {} };
element.trigger(e);
}
function dragOver(target, clientX) {
var e = $.Event('dragover');
e.originalEvent = { preventDefault: sinon.spy(), clientX: clientX };
target.trigger(e);
}
function dragEnd(element) {
element.trigger('dragend');
}
describe('when the last item is draggedStarted', function() {
var dragSpy, dragSource, watcher;
beforeEach(function() {
dragSpy = sinon.spy();
watcher = dragSort.init(list, '.list_item', dragSpy);
dragSource = $('.list_item:eq(3)');
dragStart(dragSource);
});
it('adds the "dragging" class to the dragSource', function() {
expect(dragSource.hasClass('dragging')).to.equal(true);
});
describe('when destroying the dragSort', function() {
beforeEach(function() {
watcher.destroy();
var dragTarget = $('.list_item:eq(0)');
dragOver(dragTarget, 0);
dragEnd(dragSource);
});
it('does not respond to drag events anymore', function() {
expect(dragSpy.called).to.equal(false);
});
});
describe('when it is dragged to the left of the first item', function() {
var dragTarget;
beforeEach(function() {
dragTarget = $('.list_item:eq(0)');
dragOver(dragTarget, 0);
});
it('puts the dragSource in the first position', function() {
expect(dragSource.index()).to.equal(0);
});
it('pushes the dragTarget over one', function() {
expect(dragTarget.index()).to.equal(1);
});
describe('when dragging ends', function() {
beforeEach(function() {
dragEnd(dragSource);
});
it('removes dragging class', function() {
expect(dragSource.hasClass('dragging')).to.equal(false);
});
it('executes the callback passing the oldIndex and newIndex', function() {
expect(dragSpy.calledWith(3, 0)).to.equal(true);
});
});
});
describe('when it is dragged to the right of the first item', function() {
var dragTarget;
beforeEach(function() {
dragTarget = $('.list_item:eq(0)');
dragOver(dragTarget, 20);
});
it('puts the dragSource to the right of the first item', function() {
expect(dragSource.index()).to.equal(1);
});
});
describe('when it is dragged over itself', function() {
var dragTarget;
beforeEach(function() {
dragTarget = dragSource;
dragOver(dragTarget, 130);
});
it('keeps the same index', function() {
expect(dragSource.index()).to.equal(3);
});
});
});
});