Skip to content

Commit

Permalink
add tests for events.once(eventTarget) using browser built-in EventTa…
Browse files Browse the repository at this point in the history
…rget
  • Loading branch information
goto-bus-stop committed Feb 27, 2021
1 parent 0f82983 commit 1f9e025
Showing 1 changed file with 44 additions and 2 deletions.
46 changes: 44 additions & 2 deletions tests/events-once.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ function prioritizesEventEmitter() {
return once(ee, 'foo');
}

module.exports = Promise.all([
var allTests = [
onceAnEvent(),
onceAnEventWithTwoArgs(),
catchesErrors(),
Expand All @@ -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);

0 comments on commit 1f9e025

Please sign in to comment.