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

events: add a few tests #35806

Closed
wants to merge 1 commit into from
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
4 changes: 2 additions & 2 deletions lib/internal/event_target.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,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();
benjamingr marked this conversation as resolved.
Show resolved Hide resolved
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'));
}