From 1cdfaa80f806a7732d8afbfdc86533de1e0352d4 Mon Sep 17 00:00:00 2001 From: Benjamin Gruenbaum Date: Mon, 26 Oct 2020 11:11:13 +0200 Subject: [PATCH] events: add a few tests PR-URL: https://github.com/nodejs/node/pull/35806 Reviewed-By: Rich Trott Reviewed-By: James M Snell Reviewed-By: Antoine du Hamel --- lib/internal/event_target.js | 4 ++-- test/parallel/test-eventtarget.js | 32 +++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/lib/internal/event_target.js b/lib/internal/event_target.js index e05ccd4b11f9ac..8c3ca67337baea 100644 --- a/lib/internal/event_target.js +++ b/lib/internal/event_target.js @@ -72,10 +72,10 @@ const isTrusted = ObjectGetOwnPropertyDescriptor({ }, 'isTrusted').get; class Event { - constructor(type, options) { + constructor(type, options = null) { if (arguments.length === 0) throw new ERR_MISSING_ARGS('type'); - if (options != null) + if (options !== null) validateObject(options, 'options'); const { cancelable, bubbles, composed } = { ...options }; this[kCancelable] = !!cancelable; diff --git a/test/parallel/test-eventtarget.js b/test/parallel/test-eventtarget.js index 786cbc65f3371b..accacb8a328c03 100644 --- a/test/parallel/test-eventtarget.js +++ b/test/parallel/test-eventtarget.js @@ -414,6 +414,7 @@ let asyncTest = Promise.resolve(); } { + // Event Statics strictEqual(Event.NONE, 0); strictEqual(Event.CAPTURING_PHASE, 1); strictEqual(Event.AT_TARGET, 2); @@ -424,6 +425,8 @@ let asyncTest = Promise.resolve(); strictEqual(e.eventPhase, Event.AT_TARGET); }), { once: true }); target.dispatchEvent(new Event('foo')); + // Event is a function + strictEqual(Event.length, 1); } { @@ -485,3 +488,32 @@ let asyncTest = Promise.resolve(); eventTarget.dispatchEvent(event); strictEqual(event.target, eventTarget); } +{ + // Event target exported keys + const eventTarget = new EventTarget(); + deepStrictEqual(Object.keys(eventTarget), []); + deepStrictEqual(Object.getOwnPropertyNames(eventTarget), []); + const parentKeys = Object.keys(Object.getPrototypeOf(eventTarget)).sort(); + const keys = ['addEventListener', 'dispatchEvent', 'removeEventListener']; + deepStrictEqual(parentKeys, keys); +} +{ + // Subclassing + class SubTarget extends EventTarget {} + const target = new SubTarget(); + target.addEventListener('foo', common.mustCall()); + target.dispatchEvent(new Event('foo')); +} +{ + // Test event order + const target = new EventTarget(); + let state = 0; + target.addEventListener('foo', common.mustCall(() => { + strictEqual(state, 0); + state++; + })); + target.addEventListener('foo', common.mustCall(() => { + strictEqual(state, 1); + })); + target.dispatchEvent(new Event('foo')); +}