Skip to content

Commit

Permalink
events: add tests, better toString
Browse files Browse the repository at this point in the history
PR-URL: #33622
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
  • Loading branch information
benjamingr authored and codebytere committed Jun 18, 2020
1 parent d194d20 commit f3d0d30
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/internal/event_target.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ class EventTarget {

return `${name} ${inspect({}, opts)}`;
}
get [Symbol.toStringTag]() { return 'EventTarget'; }
}

Object.defineProperties(EventTarget.prototype, {
Expand Down
17 changes: 16 additions & 1 deletion test/parallel/test-eventtarget.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,15 @@ ok(EventTarget);
eventTarget.removeEventListener('foo', ev1);
eventTarget.dispatchEvent(new Event('foo'));
}

{
// event subclassing
const SubEvent = class extends Event {};
const ev = new SubEvent('foo');
const eventTarget = new EventTarget();
const fn = common.mustCall((event) => strictEqual(event, ev));
eventTarget.addEventListener('foo', fn, { once: true });
eventTarget.dispatchEvent(ev);
}
{
const eventTarget = new NodeEventTarget();
strictEqual(eventTarget.listenerCount('foo'), 0);
Expand Down Expand Up @@ -384,3 +392,10 @@ ok(EventTarget);
target.removeEventListener('foo', a, { capture: false });
target.dispatchEvent(new Event('foo'));
}

{
const target = new EventTarget();
strictEqual(target.toString(), '[object EventTarget]');
const event = new Event();
strictEqual(event.toString(), '[object Event]');
}

0 comments on commit f3d0d30

Please sign in to comment.