Skip to content

Commit 8a6cabb

Browse files
Ethan-Arrowoodcodebytere
authored andcommitted
events: port some wpt tests
Add WPT AddEventListenerOptions-once test. PR-URL: #34169 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Denys Otrishko <shishugi@gmail.com>
1 parent 5c0ddbc commit 8a6cabb

3 files changed

+83
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
5+
const { strictEqual, throws, equal } = require('assert');
6+
7+
// Manually converted from https://github.com/web-platform-tests/wpt/blob/master/dom/events/CustomEvent.html
8+
// in order to define the `document` ourselves
9+
10+
{
11+
const type = 'foo';
12+
const target = new EventTarget();
13+
14+
target.addEventListener(type, common.mustCall((evt) => {
15+
strictEqual(evt.type, type);
16+
}));
17+
18+
target.dispatchEvent(new Event(type));
19+
}
20+
21+
{
22+
throws(() => {
23+
new Event();
24+
}, TypeError);
25+
}
26+
27+
{
28+
const event = new Event('foo');
29+
equal(event.type, 'foo');
30+
equal(event.bubbles, false);
31+
equal(event.cancelable, false);
32+
equal(event.detail, null);
33+
}

test/parallel/test-eventtarget-whatwg-once.js

+47
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,50 @@ const {
8383
document.dispatchEvent(new Event('test'));
8484
strictEqual(invoked_count, 2, 'Once handler should only be invoked once');
8585
}
86+
87+
// Manually converted from https://github.com/web-platform-tests/wpt/blob/master/dom/events/AddEventListenerOptions-once.html
88+
// in order to define the `document` ourselves
89+
90+
{
91+
const document = new EventTarget();
92+
93+
// Should only fire for first event
94+
document.addEventListener('test', common.mustCall(1), { once: true });
95+
// Should fire for both events
96+
document.addEventListener('test', common.mustCall(2));
97+
// Fire events
98+
document.dispatchEvent(new Event('test'));
99+
document.dispatchEvent(new Event('test'));
100+
}
101+
{
102+
const document = new EventTarget();
103+
104+
const handler = common.mustCall(2);
105+
// Both should only fire on first event
106+
document.addEventListener('test', handler.bind(), { once: true });
107+
document.addEventListener('test', handler.bind(), { once: true });
108+
// Fire events
109+
document.dispatchEvent(new Event('test'));
110+
document.dispatchEvent(new Event('test'));
111+
}
112+
{
113+
const document = new EventTarget();
114+
115+
const handler = common.mustCall(2);
116+
117+
// Should only fire once on first event
118+
document.addEventListener('test', common.mustCall(1), { once: true });
119+
// Should fire twice until removed
120+
document.addEventListener('test', handler);
121+
// Fire two events
122+
document.dispatchEvent(new Event('test'));
123+
document.dispatchEvent(new Event('test'));
124+
125+
// Should only fire once on the next event
126+
document.addEventListener('test', common.mustCall(1), { once: true });
127+
// The previous handler should no longer fire
128+
document.removeEventListener('test', handler);
129+
130+
// Fire final event triggering
131+
document.dispatchEvent(new Event('test'));
132+
}

test/parallel/test-eventtarget-whatwg-passive.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
const common = require('../common');
44

5+
// Manually converted from https://github.com/web-platform-tests/wpt/blob/master/dom/events/AddEventListenerOptions-passive.html
6+
// in order to define the `document` ourselves
7+
58
const {
69
fail,
710
ok,
811
strictEqual
912
} = require('assert');
1013

11-
// Manually ported from WPT AddEventListenerOptions-passive.html
1214
{
1315
const document = new EventTarget();
1416
let supportsPassive = false;

0 commit comments

Comments
 (0)