Skip to content

Commit 5ce1533

Browse files
jasnellcodebytere
authored andcommitted
events: add event-target tests
PR-URL: #34015 Reviewed-By: Denys Otrishko <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent 2724514 commit 5ce1533

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

test/parallel/test-eventtarget.js

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

19-
const { once } = require('events');
19+
const { once, on } = require('events');
2020

2121
// The globals are defined.
2222
ok(Event);
@@ -71,7 +71,7 @@ ok(EventTarget);
7171
strictEqual(ev.type, 'foo');
7272
}
7373
{
74-
const ev = new Event('foo');
74+
const ev = new Event('foo');
7575
strictEqual(ev.cancelBubble, false);
7676
ev.cancelBubble = true;
7777
strictEqual(ev.cancelBubble, true);
@@ -528,3 +528,35 @@ const ev = new Event('foo');
528528
}), { once: true });
529529
target.dispatchEvent(new Event('foo'));
530530
}
531+
532+
{
533+
const target = new EventTarget();
534+
const ev = new Event('toString');
535+
const fn = common.mustCall((event) => strictEqual(event.type, 'toString'));
536+
target.addEventListener('toString', fn);
537+
target.dispatchEvent(ev);
538+
}
539+
{
540+
const target = new EventTarget();
541+
const ev = new Event('__proto__');
542+
const fn = common.mustCall((event) => strictEqual(event.type, '__proto__'));
543+
target.addEventListener('__proto__', fn);
544+
target.dispatchEvent(ev);
545+
}
546+
547+
(async () => {
548+
// test NodeEventTarget async-iterability
549+
const emitter = new NodeEventTarget();
550+
const interval = setInterval(() => {
551+
emitter.dispatchEvent(new Event('foo'));
552+
}, 0);
553+
let count = 0;
554+
for await (const [ item ] of on(emitter, 'foo')) {
555+
count++;
556+
strictEqual(item.type, 'foo');
557+
if (count > 5) {
558+
break;
559+
}
560+
}
561+
clearInterval(interval);
562+
})().then(common.mustCall());

0 commit comments

Comments
 (0)