From f3d0d3089dd4afd587df102c12f8418eb3a16df0 Mon Sep 17 00:00:00 2001 From: Benjamin Gruenbaum Date: Thu, 28 May 2020 21:19:13 +0300 Subject: [PATCH] events: add tests, better toString PR-URL: https://github.com/nodejs/node/pull/33622 Reviewed-By: James M Snell Reviewed-By: Ruben Bridgewater Reviewed-By: Anna Henningsen --- lib/internal/event_target.js | 1 + test/parallel/test-eventtarget.js | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/internal/event_target.js b/lib/internal/event_target.js index 4cc561f95c0bba..a898a3c653f36e 100644 --- a/lib/internal/event_target.js +++ b/lib/internal/event_target.js @@ -282,6 +282,7 @@ class EventTarget { return `${name} ${inspect({}, opts)}`; } + get [Symbol.toStringTag]() { return 'EventTarget'; } } Object.defineProperties(EventTarget.prototype, { diff --git a/test/parallel/test-eventtarget.js b/test/parallel/test-eventtarget.js index 3b44714cfbe2dc..f27f96803e2a93 100644 --- a/test/parallel/test-eventtarget.js +++ b/test/parallel/test-eventtarget.js @@ -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); @@ -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]'); +}