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: add tests, better toString #33622

Closed
wants to merge 2 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
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]');
}