Skip to content

Commit

Permalink
events: add a few tests
Browse files Browse the repository at this point in the history
PR-URL: nodejs#35806
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
  • Loading branch information
benjamingr authored and targos committed Apr 30, 2021
1 parent ceef7e3 commit 01d2b67
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/internal/event_target.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ const isTrusted = ObjectGetOwnPropertyDescriptor({
}, 'isTrusted').get;

class Event {
constructor(type, options) {
constructor(type, options = null) {
if (arguments.length === 0)
throw new ERR_MISSING_ARGS('type');
if (options != null)
if (options !== null)
validateObject(options, 'options');
const { cancelable, bubbles, composed } = { ...options };
this[kCancelable] = !!cancelable;
Expand Down
32 changes: 32 additions & 0 deletions test/parallel/test-eventtarget.js
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ let asyncTest = Promise.resolve();
}

{
// Event Statics
strictEqual(Event.NONE, 0);
strictEqual(Event.CAPTURING_PHASE, 1);
strictEqual(Event.AT_TARGET, 2);
Expand All @@ -424,6 +425,8 @@ let asyncTest = Promise.resolve();
strictEqual(e.eventPhase, Event.AT_TARGET);
}), { once: true });
target.dispatchEvent(new Event('foo'));
// Event is a function
strictEqual(Event.length, 1);
}

{
Expand Down Expand Up @@ -485,3 +488,32 @@ let asyncTest = Promise.resolve();
eventTarget.dispatchEvent(event);
strictEqual(event.target, eventTarget);
}
{
// Event target exported keys
const eventTarget = new EventTarget();
deepStrictEqual(Object.keys(eventTarget), []);
deepStrictEqual(Object.getOwnPropertyNames(eventTarget), []);
const parentKeys = Object.keys(Object.getPrototypeOf(eventTarget)).sort();
const keys = ['addEventListener', 'dispatchEvent', 'removeEventListener'];
deepStrictEqual(parentKeys, keys);
}
{
// Subclassing
class SubTarget extends EventTarget {}
const target = new SubTarget();
target.addEventListener('foo', common.mustCall());
target.dispatchEvent(new Event('foo'));
}
{
// Test event order
const target = new EventTarget();
let state = 0;
target.addEventListener('foo', common.mustCall(() => {
strictEqual(state, 0);
state++;
}));
target.addEventListener('foo', common.mustCall(() => {
strictEqual(state, 1);
}));
target.dispatchEvent(new Event('foo'));
}

0 comments on commit 01d2b67

Please sign in to comment.