Skip to content

Commit 1cdfaa8

Browse files
benjamingrtargos
authored andcommitted
events: add a few tests
PR-URL: #35806 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
1 parent d3f1cde commit 1cdfaa8

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

lib/internal/event_target.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ const isTrusted = ObjectGetOwnPropertyDescriptor({
7272
}, 'isTrusted').get;
7373

7474
class Event {
75-
constructor(type, options) {
75+
constructor(type, options = null) {
7676
if (arguments.length === 0)
7777
throw new ERR_MISSING_ARGS('type');
78-
if (options != null)
78+
if (options !== null)
7979
validateObject(options, 'options');
8080
const { cancelable, bubbles, composed } = { ...options };
8181
this[kCancelable] = !!cancelable;

test/parallel/test-eventtarget.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ let asyncTest = Promise.resolve();
414414
}
415415

416416
{
417+
// Event Statics
417418
strictEqual(Event.NONE, 0);
418419
strictEqual(Event.CAPTURING_PHASE, 1);
419420
strictEqual(Event.AT_TARGET, 2);
@@ -424,6 +425,8 @@ let asyncTest = Promise.resolve();
424425
strictEqual(e.eventPhase, Event.AT_TARGET);
425426
}), { once: true });
426427
target.dispatchEvent(new Event('foo'));
428+
// Event is a function
429+
strictEqual(Event.length, 1);
427430
}
428431

429432
{
@@ -485,3 +488,32 @@ let asyncTest = Promise.resolve();
485488
eventTarget.dispatchEvent(event);
486489
strictEqual(event.target, eventTarget);
487490
}
491+
{
492+
// Event target exported keys
493+
const eventTarget = new EventTarget();
494+
deepStrictEqual(Object.keys(eventTarget), []);
495+
deepStrictEqual(Object.getOwnPropertyNames(eventTarget), []);
496+
const parentKeys = Object.keys(Object.getPrototypeOf(eventTarget)).sort();
497+
const keys = ['addEventListener', 'dispatchEvent', 'removeEventListener'];
498+
deepStrictEqual(parentKeys, keys);
499+
}
500+
{
501+
// Subclassing
502+
class SubTarget extends EventTarget {}
503+
const target = new SubTarget();
504+
target.addEventListener('foo', common.mustCall());
505+
target.dispatchEvent(new Event('foo'));
506+
}
507+
{
508+
// Test event order
509+
const target = new EventTarget();
510+
let state = 0;
511+
target.addEventListener('foo', common.mustCall(() => {
512+
strictEqual(state, 0);
513+
state++;
514+
}));
515+
target.addEventListener('foo', common.mustCall(() => {
516+
strictEqual(state, 1);
517+
}));
518+
target.dispatchEvent(new Event('foo'));
519+
}

0 commit comments

Comments
 (0)