From 1f9e025e623209f52ec17e7f233889f2c78709c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9e=20Kooi?= Date: Sat, 27 Feb 2021 17:32:42 +0100 Subject: [PATCH] add tests for events.once(eventTarget) using browser built-in EventTarget --- tests/events-once.js | 46 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 2 deletions(-) diff --git a/tests/events-once.js b/tests/events-once.js index c3672c2..691192c 100644 --- a/tests/events-once.js +++ b/tests/events-once.js @@ -180,7 +180,7 @@ function prioritizesEventEmitter() { return once(ee, 'foo'); } -module.exports = Promise.all([ +var allTests = [ onceAnEvent(), onceAnEventWithTwoArgs(), catchesErrors(), @@ -189,4 +189,46 @@ module.exports = Promise.all([ onceWithEventTarget(), onceWithEventTargetError(), prioritizesEventEmitter() -]); +]; + +var hasBrowserEventTarget = false; +try { + hasBrowserEventTarget = typeof (new window.EventTarget().addEventListener) === 'function' && + new window.Event('xyz').type === 'xyz'; +} catch (err) {} + +if (hasBrowserEventTarget) { + function onceWithBrowserEventTarget() { + var et = new window.EventTarget(); + var event = new window.Event('myevent'); + process.nextTick(function () { + et.dispatchEvent(event); + }); + return once(et, 'myevent').then(function (args) { + var value = args[0]; + assert.strictEqual(value, event); + assert.strictEqual(has(et.events, 'myevent'), false); + }); + } + + function onceWithBrowserEventTargetError() { + var et = new window.EventTarget(); + var error = new window.Event('error'); + process.nextTick(function () { + et.dispatchEvent(error); + }); + return once(et, 'error').then(function (args) { + var err = args[0]; + assert.strictEqual(err, error); + assert.strictEqual(has(et.events, 'error'), false); + }); + } + + common.test.comment('Testing with browser built-in EventTarget'); + allTests.push([ + onceWithBrowserEventTarget(), + onceWithBrowserEventTargetError() + ]); +} + +module.exports = Promise.all(allTests);