Skip to content

Commit

Permalink
events: port some wpt tests
Browse files Browse the repository at this point in the history
Add WPT AddEventListenerOptions-once test.

PR-URL: #34169
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Denys Otrishko <shishugi@gmail.com>
  • Loading branch information
Ethan-Arrowood authored and codebytere committed Nov 22, 2020
1 parent 5c0ddbc commit 8a6cabb
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
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
// 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

0 comments on commit 8a6cabb

Please sign in to comment.