Skip to content

Commit

Permalink
add WPT AddEventListenerOptions-once test
Browse files Browse the repository at this point in the history
add reference comments for WPT tests

convert to common mustCall

Update test/parallel/test-eventtarget-whatwg-once.js

Co-authored-by: James M Snell <[email protected]>

Update test/parallel/test-eventtarget-whatwg-passive.js

Co-authored-by: James M Snell <[email protected]>

convert other tests to utilize common mustcall

improve test with bind

add customevent wpt

add no-unused-vars comment

reorder header

utilize common.mustcall

remove internal and use global EventTarget
  • Loading branch information
Ethan-Arrowood committed Nov 9, 2020
1 parent 36fbbe0 commit 7f6ada3
Show file tree
Hide file tree
Showing 3 changed files with 85 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);
}
49 changes: 49 additions & 0 deletions test/parallel/test-eventtarget-whatwg-once.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Flags: --expose-internals

'use strict';

const common = require('../common');
Expand Down Expand Up @@ -83,3 +85,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 7f6ada3

Please sign in to comment.