diff --git a/test/parallel/test-eventtarget-whatwg-customevent.js b/test/parallel/test-eventtarget-whatwg-customevent.js new file mode 100644 index 00000000000000..e21ea1783f8998 --- /dev/null +++ b/test/parallel/test-eventtarget-whatwg-customevent.js @@ -0,0 +1,33 @@ +'use strict'; + +const common = require('../common'); + +const { strictEqual, throws, equal } = require('assert'); + +// Manually converted from https://github.com/web-platform-tests/wpt/blob/master/dom/events/CustomEvent.html +// in order to define the `document` ourselves + +{ + const type = 'foo'; + const target = new EventTarget(); + + target.addEventListener(type, common.mustCall((evt) => { + strictEqual(evt.type, type); + })); + + target.dispatchEvent(new Event(type)); +} + +{ + throws(() => { + new Event(); + }, TypeError); +} + +{ + const event = new Event('foo'); + equal(event.type, 'foo'); + equal(event.bubbles, false); + equal(event.cancelable, false); + equal(event.detail, null); +} diff --git a/test/parallel/test-eventtarget-whatwg-once.js b/test/parallel/test-eventtarget-whatwg-once.js index 95cbbbcf2ccaeb..9c74829d761306 100644 --- a/test/parallel/test-eventtarget-whatwg-once.js +++ b/test/parallel/test-eventtarget-whatwg-once.js @@ -83,3 +83,50 @@ const { document.dispatchEvent(new Event('test')); strictEqual(invoked_count, 2, 'Once handler should only be invoked once'); } + +// Manually converted from https://github.com/web-platform-tests/wpt/blob/master/dom/events/AddEventListenerOptions-once.html +// in order to define the `document` ourselves + +{ + const document = new EventTarget(); + + // Should only fire for first event + document.addEventListener('test', common.mustCall(1), { once: true }); + // Should fire for both events + document.addEventListener('test', common.mustCall(2)); + // Fire events + document.dispatchEvent(new Event('test')); + document.dispatchEvent(new Event('test')); +} +{ + const document = new EventTarget(); + + const handler = common.mustCall(2); + // Both should only fire on first event + document.addEventListener('test', handler.bind(), { once: true }); + document.addEventListener('test', handler.bind(), { once: true }); + // Fire events + document.dispatchEvent(new Event('test')); + document.dispatchEvent(new Event('test')); +} +{ + const document = new EventTarget(); + + const handler = common.mustCall(2); + + // Should only fire once on first event + document.addEventListener('test', common.mustCall(1), { once: true }); + // Should fire twice until removed + document.addEventListener('test', handler); + // Fire two events + document.dispatchEvent(new Event('test')); + document.dispatchEvent(new Event('test')); + + // Should only fire once on the next event + document.addEventListener('test', common.mustCall(1), { once: true }); + // The previous handler should no longer fire + document.removeEventListener('test', handler); + + // Fire final event triggering + document.dispatchEvent(new Event('test')); +} diff --git a/test/parallel/test-eventtarget-whatwg-passive.js b/test/parallel/test-eventtarget-whatwg-passive.js index 5399d19da45943..97984bd9aff828 100644 --- a/test/parallel/test-eventtarget-whatwg-passive.js +++ b/test/parallel/test-eventtarget-whatwg-passive.js @@ -2,13 +2,15 @@ const common = require('../common'); +// Manually converted from https://github.com/web-platform-tests/wpt/blob/master/dom/events/AddEventListenerOptions-passive.html +// in order to define the `document` ourselves + const { fail, ok, strictEqual } = require('assert'); -// Manually ported from WPT AddEventListenerOptions-passive.html { const document = new EventTarget(); let supportsPassive = false;