-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
test.js
125 lines (100 loc) · 3.42 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
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
import test from 'ava';
import sinon from 'sinon';
import {createRequire} from 'module';
import delegate from './index.js';
const require = createRequire(import.meta.url);
export const {JSDOM} = require('jsdom');
const {window} = new JSDOM(`
<ul>
<li><a>Item 1</a></li>
<li><a>Item 2</a></li>
<li><a>Item 3</a></li>
<li><a>Item 4</a></li>
<li><a>Item 5</a></li>
</ul>
`);
global.Text = window.Text;
global.Event = window.Event;
global.Element = window.Element;
global.Document = window.Document;
global.MouseEvent = window.MouseEvent;
global.document = window.document;
const container = window.document.querySelector('ul');
const anchor = window.document.querySelector('a');
test.serial('should add an event listener', t => {
delegate(container, 'a', 'click', t.pass);
anchor.click();
});
test.serial('should add an event listener only once', t => {
t.plan(2);
// Only deduplicates the `capture` flag
// https://github.com/fregante/delegate-it/pull/11#discussion_r285481625
// Capture: false
delegate(container, 'a', 'click', t.pass);
delegate(container, 'a', 'click', t.pass, {passive: true});
delegate(container, 'a', 'click', t.pass, {capture: false});
// Capture: true
delegate(container, 'a', 'click', t.pass, true);
delegate(container, 'a', 'click', t.pass, {capture: true});
anchor.click();
});
test.serial('should handle events on text nodes', t => {
delegate(container, 'a', 'click', t.pass);
anchor.firstChild.dispatchEvent(new MouseEvent('click', {bubbles: true}));
});
test.serial('should remove an event listener', t => {
const spy = sinon.spy(container, 'removeEventListener');
const delegation = delegate(container, 'a', 'click', () => {});
delegation.destroy();
t.true(spy.calledOnce);
spy.restore();
});
test.serial('should add event listeners to all the elements in a base selector', t => {
const spy = sinon.spy();
delegate('li', 'a', 'click', spy);
const anchors = document.querySelectorAll('a');
anchors[0].click();
anchors[1].click();
t.true(spy.calledTwice);
});
test.serial('should remove the event listeners from all the elements in a base selector', t => {
const items = document.querySelectorAll('li');
const spies = Array.prototype.map.call(items, li => {
return sinon.spy(li, 'removeEventListener');
});
const delegation = delegate('li', 'a', 'click', () => {});
delegation.destroy();
t.true(spies.every(spy => {
const success = spy.calledOnce;
spy.restore();
return success;
}));
});
test.serial('should add event listeners to all the elements in a base array', t => {
const spy = sinon.spy();
const items = document.querySelectorAll('li');
delegate(items, 'a', 'click', spy);
const anchors = document.querySelectorAll('a');
anchors[0].click();
anchors[1].click();
t.true(spy.calledTwice);
});
test.serial('should remove the event listeners from all the elements in a base array', t => {
const items = document.querySelectorAll('li');
const spies = Array.prototype.map.call(items, li => {
return sinon.spy(li, 'removeEventListener');
});
const delegation = delegate(items, 'a', 'click', () => {});
delegation.destroy();
t.true(spies.every(spy => {
const success = spy.calledOnce;
spy.restore();
return success;
}));
});
test.serial('should not fire when the selector matches an ancestor of the base element', t => {
const spy = sinon.spy();
delegate(container, 'body', 'click', spy);
anchor.click();
t.true(spy.notCalled);
});