-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
test.js
174 lines (139 loc) · 4.82 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import test from 'ava';
import sinon from 'sinon';
import {JSDOM} from 'jsdom';
import delegate from './index.js';
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.AbortController = window.AbortController;
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();
const controller = new AbortController();
delegate(container, 'a', 'click', spy, {signal: controller.signal});
controller.abort();
const anchor = document.querySelector('a');
anchor.click();
t.true(spy.notCalled);
});
test.serial('should pass an AbortSignal to an event listener', t => {
const spy = sinon.spy();
const controller = new AbortController();
delegate(container, 'a', 'click', spy, {signal: controller.signal});
controller.abort();
const anchor = document.querySelector('a');
anchor.click();
t.true(spy.notCalled);
});
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 spy = sinon.spy();
const controller = new AbortController();
delegate('li', 'a', 'click', spy, {signal: controller.signal});
controller.abort();
for (const anchor of document.querySelectorAll('a')) {
anchor.click();
}
t.true(spy.notCalled);
});
test.serial('should pass an AbortSignal to the event listeners on all the elements in a base selector', t => {
const spy = sinon.spy();
const controller = new AbortController();
delegate('li', 'a', 'click', spy, {signal: controller.signal});
controller.abort();
for (const anchor of document.querySelectorAll('a')) {
anchor.click();
}
t.true(spy.notCalled);
});
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 spy = sinon.spy();
const items = document.querySelectorAll('li');
const controller = new AbortController();
delegate(items, 'a', 'click', spy, {signal: controller.signal});
controller.abort();
for (const anchor of document.querySelectorAll('a')) {
anchor.click();
}
t.true(spy.notCalled);
});
test.serial('should pass an AbortSignal to the event listeners on all the elements in a base array', t => {
const spy = sinon.spy();
const items = document.querySelectorAll('li');
const controller = new AbortController();
delegate(items, 'a', 'click', spy, {signal: controller.signal});
controller.abort();
for (const anchor of document.querySelectorAll('a')) {
anchor.click();
}
t.true(spy.notCalled);
});
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);
});
test.serial('should not add an event listener when passed an already aborted signal', t => {
const spy = sinon.spy(container, 'addEventListener');
delegate(container, 'a', 'click', spy, {signal: AbortSignal.abort()});
anchor.click();
t.true(spy.notCalled);
});
test.serial('should not consider the `once` option', t => {
const spy = sinon.spy();
delegate(container, 'a', 'click', spy, {once: true});
anchor.click();
anchor.click();
t.true(spy.calledTwice);
});