Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add EventTarget WPT test AddEventListenerOptions-once #34169

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions test/parallel/test-eventtarget-whatwg-customevent.js
Original file line number Diff line number Diff line change
@@ -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
Ethan-Arrowood marked this conversation as resolved.
Show resolved Hide resolved
// 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);
}
47 changes: 47 additions & 0 deletions test/parallel/test-eventtarget-whatwg-once.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}
4 changes: 3 additions & 1 deletion test/parallel/test-eventtarget-whatwg-passive.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down