From e94efeb1589fa5ee4e58cd398272b65dd5051748 Mon Sep 17 00:00:00 2001 From: Benjamin Gruenbaum Date: Thu, 28 May 2020 21:46:18 +0300 Subject: [PATCH] events: add event-target tests PR-URL: https://github.com/nodejs/node/pull/33623 Reviewed-By: James M Snell Reviewed-By: Ruben Bridgewater --- test/parallel/test-eventtarget.js | 32 ++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/test/parallel/test-eventtarget.js b/test/parallel/test-eventtarget.js index e47dac6fc2f48c..9203a6e803b88f 100644 --- a/test/parallel/test-eventtarget.js +++ b/test/parallel/test-eventtarget.js @@ -15,7 +15,7 @@ const { throws, } = require('assert'); -const { once } = require('events'); +const { once, on } = require('events'); // The globals are defined. ok(Event); @@ -438,3 +438,33 @@ const ev = new Event('foo'); const event = new Event(); strictEqual(event.toString(), '[object Event]'); } +{ + const target = new EventTarget(); + const ev = new Event('toString'); + const fn = common.mustCall((event) => strictEqual(event.type, 'toString')); + target.addEventListener('toString', fn); + target.dispatchEvent(ev); +} +{ + const target = new EventTarget(); + const ev = new Event('__proto__'); + const fn = common.mustCall((event) => strictEqual(event.type, '__proto__')); + target.addEventListener('__proto__', fn); + target.dispatchEvent(ev); +} + +(async () => { + // test NodeEventTarget async-iterability + const emitter = new NodeEventTarget(); + const event = new Event('foo'); + const interval = setInterval(() => emitter.dispatchEvent(event), 0); + let count = 0; + for await (const [ item ] of on(emitter, 'foo')) { + count++; + strictEqual(item.type, 'foo'); + if (count > 5) { + break; + } + } + clearInterval(interval); +})().then(common.mustCall());