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 <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Denys Otrishko <[email protected]>
1 parent 5c0ddbc commit 8a6cabb

File tree

3 files changed

+83
-1
lines changed

3 files changed

+83
-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+
constcommon=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+
consttype='foo';
12+
consttarget=newEventTarget();
13+
14+
target.addEventListener(type,common.mustCall((evt)=>{
15+
strictEqual(evt.type,type);
16+
}));
17+
18+
target.dispatchEvent(newEvent(type));
19+
}
20+
21+
{
22+
throws(()=>{
23+
newEvent();
24+
},TypeError);
25+
}
26+
27+
{
28+
constevent=newEvent('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: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,50 @@ const{
8383
document.dispatchEvent(newEvent('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+
constdocument=newEventTarget();
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(newEvent('test'));
99+
document.dispatchEvent(newEvent('test'));
100+
}
101+
{
102+
constdocument=newEventTarget();
103+
104+
consthandler=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(newEvent('test'));
110+
document.dispatchEvent(newEvent('test'));
111+
}
112+
{
113+
constdocument=newEventTarget();
114+
115+
consthandler=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(newEvent('test'));
123+
document.dispatchEvent(newEvent('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(newEvent('test'));
132+
}

‎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
constcommon=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
constdocument=newEventTarget();
1416
letsupportsPassive=false;

0 commit comments

Comments
(0)