Skip to content

Commit

Permalink
events: deal with no argument case
Browse files Browse the repository at this point in the history
PR-URL: #33611
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
  • Loading branch information
benjamingr authored and codebytere committed Jun 18, 2020
1 parent 858c6b9 commit bc2e821
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
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);
// 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]');
}

0 comments on commit bc2e821

Please sign in to comment.