forked from bevacqua/dragula
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdestroy.js
103 lines (97 loc) · 3.03 KB
/
destroy.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
'use strict';
var test = require('tape');
var dragula = require('..');
test('destroy does not throw when not dragging, destroyed, or whatever', function (t) {
t.test('a single time', function once (st) {
var drake = dragula();
st.doesNotThrow(function () {
drake.destroy();
}, 'dragula bites into a single call to drake.destroy');
st.end();
});
t.test('multiple times', function once (st) {
var drake = dragula();
st.doesNotThrow(function () {
drake.destroy();
drake.destroy();
drake.destroy();
drake.destroy();
}, 'dragula bites into multiple calls to drake.destroy');
st.end();
});
t.end();
});
test('when dragging and destroy gets called, nothing happens', function (t) {
var div = document.createElement('div');
var item = document.createElement('div');
var drake = dragula([div]);
div.appendChild(item);
document.body.appendChild(div);
drake.start(item);
drake.destroy();
t.equal(div.children.length, 1, 'nothing happens');
t.equal(drake.dragging, false, 'drake has stopped dragging');
t.end();
});
test('when dragging and destroy gets called, dragend event is emitted gracefully', function (t) {
var div = document.createElement('div');
var item = document.createElement('div');
var drake = dragula([div]);
div.appendChild(item);
document.body.appendChild(div);
drake.start(item);
drake.on('dragend', dragend);
drake.destroy();
t.plan(1);
t.end();
function dragend () {
t.pass('dragend got called');
}
});
test('when dragging a copy and destroy gets called, default does not revert', function (t) {
var div = document.createElement('div');
var div2 = document.createElement('div');
var item = document.createElement('div');
var drake = dragula([div, div2]);
div.appendChild(item);
document.body.appendChild(div);
document.body.appendChild(div2);
drake.start(item);
div2.appendChild(item);
drake.on('drop', drop);
drake.on('dragend', dragend);
drake.destroy();
t.plan(4);
t.end();
function dragend () {
t.pass('dragend got called');
}
function drop (target, parent, source) {
t.equal(target, item, 'drop was invoked with item');
t.equal(parent, div2, 'drop was invoked with final container');
t.equal(source, div, 'drop was invoked with source container');
}
});
test('when dragging a copy and destroy gets called, revert is executed', function (t) {
var div = document.createElement('div');
var div2 = document.createElement('div');
var item = document.createElement('div');
var drake = dragula([div, div2], { revertOnSpill: true });
div.appendChild(item);
document.body.appendChild(div);
document.body.appendChild(div2);
drake.start(item);
div2.appendChild(item);
drake.on('cancel', cancel);
drake.on('dragend', dragend);
drake.destroy();
t.plan(3);
t.end();
function dragend () {
t.pass('dragend got called');
}
function cancel (target, container) {
t.equal(target, item, 'cancel was invoked with item');
t.equal(container, div, 'cancel was invoked with container');
}
});