-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
delegate.test.ts
174 lines (138 loc) · 5.29 KB
/
delegate.test.ts
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, vi, expect} from 'vitest';
import {container, anchor} from './vitest.setup.js';
import delegate from './delegate.js';
test('should add an event listener', () => {
const spy = vi.fn();
delegate(container, 'a', 'click', spy);
anchor.click();
expect(spy).toHaveBeenCalledTimes(1);
});
test('should handle events on text nodes', () => {
const spy = vi.fn();
delegate(container, 'a', 'click', spy);
anchor.firstChild!.dispatchEvent(new MouseEvent('click', {bubbles: true}));
expect(spy).toHaveBeenCalledTimes(1);
});
test('should remove an event listener', () => {
const spy = vi.fn();
const controller = new AbortController();
delegate(container, 'a', 'click', spy, {signal: controller.signal});
controller.abort();
anchor.click();
expect(spy).toHaveBeenCalledTimes(0);
});
test('should not add an event listener of the controller has already aborted', () => {
const spy = vi.fn();
delegate(container, 'a', 'click', spy, {signal: AbortSignal.abort()});
anchor.click();
expect(spy).toHaveBeenCalledTimes(0);
});
test('should add event listeners to all the elements in a base selector', () => {
const spy = vi.fn();
delegate('li', 'a', 'click', spy);
const anchors = document.querySelectorAll('a');
anchors[0].click();
anchors[1].click();
expect(spy).toHaveBeenCalledTimes(2);
});
test('should remove the event listeners from all the elements in a base selector', () => {
const spy = vi.fn();
const controller = new AbortController();
delegate('li', 'a', 'click', spy, {signal: controller.signal});
controller.abort();
for (const anchor of document.querySelectorAll('a')) {
anchor.click();
}
expect(spy).toHaveBeenCalledTimes(0);
});
test('should pass an AbortSignal to the event listeners on all the elements in a base selector', () => {
const spy = vi.fn();
const controller = new AbortController();
delegate('li', 'a', 'click', spy, {signal: controller.signal});
controller.abort();
for (const anchor of document.querySelectorAll('a')) {
anchor.click();
}
expect(spy).toHaveBeenCalledTimes(0);
});
test('should add event listeners to all the elements in a base array', () => {
const spy = vi.fn();
const items = document.querySelectorAll('li');
delegate(items, 'a', 'click', spy);
const anchors = document.querySelectorAll('a');
anchors[0].click();
anchors[1].click();
expect(spy).toHaveBeenCalledTimes(2);
});
test('should remove the event listeners from all the elements in a base array', () => {
const spy = vi.fn();
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();
}
expect(spy).toHaveBeenCalledTimes(0);
});
test('should pass an AbortSignal to the event listeners on all the elements in a base array', () => {
const spy = vi.fn();
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();
}
expect(spy).toHaveBeenCalledTimes(0);
});
test('should not fire when the selector matches an ancestor of the base element', () => {
const spy = vi.fn();
delegate(container, 'body', 'click', spy);
anchor.click();
expect(spy).toHaveBeenCalledTimes(0);
});
test('should not add an event listener when passed an already aborted signal', () => {
const spy = vi.spyOn(container, 'addEventListener');
delegate(container, 'a', 'click', () => ({}), {signal: AbortSignal.abort()});
anchor.click();
expect(spy).toHaveBeenCalledTimes(0);
});
test('should call the listener once with the `once` option', () => {
const spy = vi.fn();
delegate(container, 'a', 'click', spy, {once: true});
container.click();
expect(spy).toHaveBeenCalledTimes(0); // It should not be called on the container
anchor.click();
expect(spy).toHaveBeenCalledTimes(1); // It should be called on the delegate target
anchor.click();
expect(spy).toHaveBeenCalledTimes(1); // It should not be called again on the delegate target
});
test('should add a specific event listener only once', () => {
const spy = vi.fn();
// Only deduplicates the `capture` flag
// https://github.com/fregante/delegate-it/pull/11#discussion_r285481625
// Capture: false
delegate(container, 'a', 'click', spy);
delegate(container, 'a', 'click', spy, {passive: true});
delegate(container, 'a', 'click', spy, {capture: false});
// Capture: true
delegate(container, 'a', 'click', spy, true);
delegate(container, 'a', 'click', spy, {capture: true});
// Once
delegate(container, 'a', 'click', spy, {once: true});
delegate(container, 'a', 'click', spy, {once: false});
anchor.click();
expect(spy).toHaveBeenCalledTimes(2);
});
test('should deduplicate identical listeners added after `once:true`', () => {
const spy = vi.fn();
delegate(container, 'a', 'click', spy, {once: true});
delegate(container, 'a', 'click', spy, {once: false});
container.click();
expect(spy).toHaveBeenCalledTimes(0); // It should not be called on the container
anchor.click();
expect(spy).toHaveBeenCalledTimes(1); // It should be called on the delegate target
anchor.click();
expect(spy).toHaveBeenCalledTimes(1); // It should not be called again on the delegate target
});