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: deal with no argument case #33611

Closed
wants to merge 5 commits 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: 4 additions & 0 deletions lib/internal/event_target.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const {
ERR_INVALID_ARG_TYPE,
ERR_EVENT_RECURSION,
ERR_OUT_OF_RANGE,
ERR_MISSING_ARGS
}
} = require('internal/errors');

Expand Down Expand Up @@ -45,6 +46,9 @@ class Event {


constructor(type, options) {
if (arguments.length === 0) {
throw new ERR_MISSING_ARGS('type');
}
if (options != null && typeof options !== 'object')
throw new ERR_INVALID_ARG_TYPE('options', 'object', options);
const { cancelable, bubbles, composed } = { ...options };
Expand Down
13 changes: 11 additions & 2 deletions test/parallel/test-eventtarget.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ ok(EventTarget);
strictEqual(ev.defaultPrevented, false);
strictEqual(typeof ev.timeStamp, 'number');

// Compatibility properties with the DOM
deepStrictEqual(ev.composedPath(), []);
strictEqual(ev.returnValue, true);
strictEqual(ev.bubbles, false);
Expand Down Expand Up @@ -59,7 +60,15 @@ ok(EventTarget);
ev.cancelBubble = 'some-truthy-value';
strictEqual(ev.cancelBubble, true);
}

{
// No argument behavior - throw TypeError
throws(() => {
new Event();
}, TypeError);
BridgeAR marked this conversation as resolved.
Show resolved Hide resolved
// Too many arguments passed behavior - ignore additional arguments
const ev = new Event('foo', {}, {});
strictEqual(ev.type, 'foo');
}
{
const ev = new Event('foo', { cancelable: true });
strictEqual(ev.type, 'foo');
Expand Down Expand Up @@ -419,6 +428,6 @@ ok(EventTarget);
{
const target = new EventTarget();
strictEqual(target.toString(), '[object EventTarget]');
const event = new Event();
const event = new Event('');
strictEqual(event.toString(), '[object Event]');
}