diff options
-rw-r--r-- | cli/rt/27_websocket.js | 6 | ||||
-rw-r--r-- | cli/tests/unit/event_test.ts | 38 | ||||
-rw-r--r-- | op_crates/web/01_event.js | 65 |
3 files changed, 104 insertions, 5 deletions
diff --git a/cli/rt/27_websocket.js b/cli/rt/27_websocket.js index 98d3a21e6..c0790d14d 100644 --- a/cli/rt/27_websocket.js +++ b/cli/rt/27_websocket.js @@ -61,7 +61,7 @@ }).then(() => { this.#readyState = CLOSED; - const errEvent = new Event("error"); + const errEvent = new ErrorEvent("error"); errEvent.target = this; this.onerror?.(errEvent); this.dispatchEvent(errEvent); @@ -84,7 +84,7 @@ } else { this.#readyState = CLOSED; - const errEvent = new Event("error"); + const errEvent = new ErrorEvent("error"); errEvent.target = this; this.onerror?.(errEvent); this.dispatchEvent(errEvent); @@ -289,7 +289,7 @@ } else if (message.type === "error") { this.#readyState = CLOSED; - const errorEv = new Event("error"); + const errorEv = new ErrorEvent("error"); errorEv.target = this; this.onerror?.(errorEv); this.dispatchEvent(errorEv); diff --git a/cli/tests/unit/event_test.ts b/cli/tests/unit/event_test.ts index 5c8766b36..d624a54a5 100644 --- a/cli/tests/unit/event_test.ts +++ b/cli/tests/unit/event_test.ts @@ -92,3 +92,41 @@ unitTest(function eventIsTrusted(): void { assertEquals(desc1!.get, desc2!.get); }); + +unitTest(function eventInspectOutput(): void { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const cases: Array<[any, (event: any) => string]> = [ + [ + new Event("test"), + (event: Event) => + `Event {\n bubbles: false,\n cancelable: false,\n composed: false,\n currentTarget: null,\n defaultPrevented: false,\n eventPhase: 0,\n target: null,\n timeStamp: ${event.timeStamp},\n type: "test"\n}`, + ], + [ + new ErrorEvent("error"), + (event: Event) => + `ErrorEvent {\n bubbles: false,\n cancelable: false,\n composed: false,\n currentTarget: null,\n defaultPrevented: false,\n eventPhase: 0,\n target: null,\n timeStamp: ${event.timeStamp},\n type: "error",\n message: "",\n filename: "",\n lineno: 0,\n colno: 0,\n error: null\n}`, + ], + [ + new CloseEvent("close"), + (event: Event) => + `CloseEvent {\n bubbles: false,\n cancelable: false,\n composed: false,\n currentTarget: null,\n defaultPrevented: false,\n eventPhase: 0,\n target: null,\n timeStamp: ${event.timeStamp},\n type: "close",\n wasClean: false,\n code: 0,\n reason: ""\n}`, + ], + [ + new CustomEvent("custom"), + (event: Event) => + `CustomEvent {\n bubbles: false,\n cancelable: false,\n composed: false,\n currentTarget: null,\n defaultPrevented: false,\n eventPhase: 0,\n target: null,\n timeStamp: ${event.timeStamp},\n type: "custom",\n detail: undefined\n}`, + ], + [ + new ProgressEvent("progress"), + (event: Event) => + `ProgressEvent {\n bubbles: false,\n cancelable: false,\n composed: false,\n currentTarget: null,\n defaultPrevented: false,\n eventPhase: 0,\n target: null,\n timeStamp: ${event.timeStamp},\n type: "progress",\n lengthComputable: false,\n loaded: 0,\n total: 0\n}`, + ], + ]; + + for (const [event, outputProvider] of cases) { + assertEquals( + event[Symbol.for("Deno.customInspect")](), + outputProvider(event), + ); + } +}); diff --git a/op_crates/web/01_event.js b/op_crates/web/01_event.js index b0d7f2939..9067361fd 100644 --- a/op_crates/web/01_event.js +++ b/op_crates/web/01_event.js @@ -133,6 +133,10 @@ }); } + [Symbol.for("Deno.customInspect")]() { + return buildCustomInspectOutput(this, EVENT_PROPS); + } + get bubbles() { return this.#attributes.bubbles; } @@ -373,6 +377,16 @@ } } + function buildCustomInspectOutput(obj, props) { + const inspectObj = {}; + + for (const prop of props) { + inspectObj[prop] = obj[prop]; + } + + return `${obj.constructor.name} ${Deno.inspect(inspectObj)}`; + } + function defineEnumerableProps( Ctor, props, @@ -382,7 +396,7 @@ } } - defineEnumerableProps(Event, [ + const EVENT_PROPS = [ "bubbles", "cancelable", "composed", @@ -392,7 +406,9 @@ "target", "timeStamp", "type", - ]); + ]; + + defineEnumerableProps(Event, EVENT_PROPS); // This is currently the only node type we are using, so instead of implementing // the whole of the Node interface at the moment, this just gives us the one @@ -1001,6 +1017,17 @@ get [Symbol.toStringTag]() { return "ErrorEvent"; } + + [Symbol.for("Deno.customInspect")]() { + return buildCustomInspectOutput(this, [ + ...EVENT_PROPS, + "message", + "filename", + "lineno", + "colno", + "error", + ]); + } } defineEnumerableProps(ErrorEvent, [ @@ -1044,6 +1071,15 @@ this.#code = code; this.#reason = reason; } + + [Symbol.for("Deno.customInspect")]() { + return buildCustomInspectOutput(this, [ + ...EVENT_PROPS, + "wasClean", + "code", + "reason", + ]); + } } class MessageEvent extends Event { @@ -1058,6 +1094,15 @@ this.origin = eventInitDict?.origin ?? ""; this.lastEventId = eventInitDict?.lastEventId ?? ""; } + + [Symbol.for("Deno.customInspect")]() { + return buildCustomInspectOutput(this, [ + ...EVENT_PROPS, + "data", + "origin", + "lastEventId", + ]); + } } class CustomEvent extends Event { @@ -1077,6 +1122,13 @@ get [Symbol.toStringTag]() { return "CustomEvent"; } + + [Symbol.for("Deno.customInspect")]() { + return buildCustomInspectOutput(this, [ + ...EVENT_PROPS, + "detail", + ]); + } } Reflect.defineProperty(CustomEvent.prototype, "detail", { @@ -1093,6 +1145,15 @@ this.loaded = eventInitDict?.loaded ?? 0; this.total = eventInitDict?.total ?? 0; } + + [Symbol.for("Deno.customInspect")]() { + return buildCustomInspectOutput(this, [ + ...EVENT_PROPS, + "lengthComputable", + "loaded", + "total", + ]); + } } window.Event = Event; |