-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathtest.js
90 lines (76 loc) · 2.54 KB
/
test.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
const test = require('tape');
const { SimpleDropzone } = require('./');
const INPUT_FILES = [
{name: 'a.png', webkitRelativePath: 'path/to/a.png'},
{name: 'b.png', webkitRelativePath: 'path/to/b.png'},
]
test('construct', (t) => {
const inputEl = new MockHTMLElement();
const dropEl = new MockHTMLElement();
const ctrl = new SimpleDropzone(dropEl, inputEl);
t.ok(ctrl, 'creates dropzone');
t.equal(inputEl.listeners.length, 1, 'adds input listeners');
t.equal(dropEl.listeners.length, 2, 'adds dropzone listeners');
t.end();
});
test('select', async (t) => {
const inputEl = new MockHTMLElement();
const dropEl = new MockHTMLElement();
const ctrl = new SimpleDropzone(dropEl, inputEl);
inputEl.files = INPUT_FILES;
let receivedDropstart = false;
let receivedDroperror = false;
let receivedDrop = false;
ctrl.on('dropstart', () => (receivedDropstart = true));
ctrl.on('droperror', () => (receivedDroperror = true));
ctrl.on('drop', () => (receivedDrop = true));
const dropEvent = await new Promise((resolve, reject) => {
ctrl.on('drop', resolve);
ctrl.on('droperror', reject);
inputEl.dispatchEvent(new CustomEvent('change', {}));
});
t.ok(receivedDropstart, 'dropstart');
t.notOk(receivedDroperror, 'droperror');
t.ok(receivedDrop, 'drop');
t.deepEqual(Array.from(dropEvent.files), [
[INPUT_FILES[0].webkitRelativePath, INPUT_FILES[0]],
[INPUT_FILES[1].webkitRelativePath, INPUT_FILES[1]],
], 'content');
t.end();
});
test('destroy', (t) => {
const inputEl = new MockHTMLElement();
const dropEl = new MockHTMLElement();
const ctrl = new SimpleDropzone(dropEl, inputEl);
t.equal(inputEl.listeners.length, 1, 'adds input listeners');
t.equal(dropEl.listeners.length, 2, 'adds dropzone listeners');
ctrl.destroy();
t.equal(inputEl.listeners.length, 0, 'removes input listeners');
t.equal(dropEl.listeners.length, 0, 'removes dropzone listeners');
t.end();
});
/**
* Mock HTMLElement.
*
* Using an actual HTMLInputElement, it's difficult to modify the .files property
* without user interaction (https://stackoverflow.com/q/1696877/1314762). So
* instead, we use a simple mock element here.
*/
class MockHTMLElement {
constructor () {
this.listeners = [];
}
addEventListener (type, fn, options) {
this.listeners.push([type, fn, options]);
}
removeEventListener (type, fn) {
this.listeners = this.listeners.filter((listener) => {
return !(listener[0] === type && listener[1] === fn);
});
}
dispatchEvent (event) {
for (const listener of this.listeners) {
if (listener[0] === event.type) listener[1](event);
}
}
}