Skip to content

Commit 7f6ada3

Browse files
add WPT AddEventListenerOptions-once test
add reference comments for WPT tests convert to common mustCall Update test/parallel/test-eventtarget-whatwg-once.js Co-authored-by: James M Snell <[email protected]> Update test/parallel/test-eventtarget-whatwg-passive.js Co-authored-by: James M Snell <[email protected]> convert other tests to utilize common mustcall improve test with bind add customevent wpt add no-unused-vars comment reorder header utilize common.mustcall remove internal and use global EventTarget
1 parent 36fbbe0 commit 7f6ada3

File tree

3 files changed

+85
-1
lines changed

3 files changed

+85
-1
lines changed
Lines changed: 33 additions & 0 deletions
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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Flags: --expose-internals
2+
13
'use strict';
24

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

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

Lines changed: 3 additions & 1 deletion
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)