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 Benjamin Gruenbaum committed May 31, 2020
1 parent 2935f72 commit 8ae28ff
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
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 @@ -44,6 +45,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
11 changes: 10 additions & 1 deletion 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 All @@ -40,7 +41,15 @@ ok(EventTarget);
ev.preventDefault();
strictEqual(ev.defaultPrevented, false);
}

{
// 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

0 comments on commit 8ae28ff

Please sign in to comment.