Skip to content

Commit df4eda2

Browse files
benjamingrBenjamin Gruenbaum
authored and
Benjamin Gruenbaum
committed
events: add event-target tests
PR-URL: #33623 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent b2b95eb commit df4eda2

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

test/parallel/test-eventtarget.js

+32-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const {
1515
throws,
1616
} = require('assert');
1717

18-
const { once } = require('events');
18+
const { once, on } = require('events');
1919

2020
// The globals are defined.
2121
ok(Event);
@@ -52,7 +52,7 @@ ok(EventTarget);
5252
strictEqual(ev.type, 'foo');
5353
}
5454
{
55-
const ev = new Event('foo');
55+
const ev = new Event('foo');
5656
strictEqual(ev.cancelBubble, false);
5757
ev.cancelBubble = true;
5858
strictEqual(ev.cancelBubble, true);
@@ -438,3 +438,33 @@ const ev = new Event('foo');
438438
const event = new Event();
439439
strictEqual(event.toString(), '[object Event]');
440440
}
441+
{
442+
const target = new EventTarget();
443+
const ev = new Event('toString');
444+
const fn = common.mustCall((event) => strictEqual(event.type, 'toString'));
445+
target.addEventListener('toString', fn);
446+
target.dispatchEvent(ev);
447+
}
448+
{
449+
const target = new EventTarget();
450+
const ev = new Event('__proto__');
451+
const fn = common.mustCall((event) => strictEqual(event.type, '__proto__'));
452+
target.addEventListener('__proto__', fn);
453+
target.dispatchEvent(ev);
454+
}
455+
456+
(async () => {
457+
// test NodeEventTarget async-iterability
458+
const emitter = new NodeEventTarget();
459+
const event = new Event('foo');
460+
const interval = setInterval(() => emitter.dispatchEvent(event), 0);
461+
let count = 0;
462+
for await (const [ item ] of on(emitter, 'foo')) {
463+
count++;
464+
strictEqual(item.type, 'foo');
465+
if (count > 5) {
466+
break;
467+
}
468+
}
469+
clearInterval(interval);
470+
})().then(common.mustCall());

0 commit comments

Comments
 (0)