From cbeb453849bfde4bbeb3b30f37c0f6e5b0fbb195 Mon Sep 17 00:00:00 2001 From: lorenzofox3 Date: Thu, 21 Dec 2023 16:46:09 +0100 Subject: [PATCH] refacto(test-lib): some cleanup --- .../html-reporter/diagnostic.component.js | 61 ++++++++++++++++++ .../{ => html-reporter}/html-repoter.js | 3 +- .../test-result.component.js} | 62 ------------------- packages/test-lib/src/client/index.js | 2 +- packages/test-lib/src/vite/index.js | 20 +++--- 5 files changed, 72 insertions(+), 76 deletions(-) create mode 100644 packages/test-lib/src/client/html-reporter/diagnostic.component.js rename packages/test-lib/src/client/{ => html-reporter}/html-repoter.js (88%) rename packages/test-lib/src/client/{components.js => html-reporter/test-result.component.js} (61%) diff --git a/packages/test-lib/src/client/html-reporter/diagnostic.component.js b/packages/test-lib/src/client/html-reporter/diagnostic.component.js new file mode 100644 index 0000000..aadc35a --- /dev/null +++ b/packages/test-lib/src/client/html-reporter/diagnostic.component.js @@ -0,0 +1,61 @@ +const prettyPrint = (value) => JSON.stringify(value, null, 2); + +const diagnosticTemplate = document.createElement('template'); +diagnosticTemplate.innerHTML = ` +
+ + +
+ at +
+
+
+
+ actual +

+  
+
+ expected +

+  
+
+
+`; +export class Diagnostic extends HTMLElement { + #_diagnostic; + + get diagnostic() { + return this.#_diagnostic; + } + + set diagnostic(value) { + this.#_diagnostic = value; + this.render(); + } + + connectedCallback() { + this.replaceChildren(diagnosticTemplate.content.cloneNode(true)); + } + + render() { + if (!this.#_diagnostic) { + return; + } + + const { at, operator, description, expected, actual } = this.#_diagnostic; + + this.querySelector( + '.description', + ).textContent = `[${operator}] ${description}`; + + const locationElement = this.querySelector('.location'); + const locationURL = new URL(at); + const [_, row, column] = locationURL.search.split(':'); + locationElement.textContent = `${locationURL.pathname}:${row}:${column}`; + locationElement.setAttribute('href', at); + + const [actualEl, expectedEl] = this.querySelectorAll('pre'); + actualEl.textContent = prettyPrint(actual); + expectedEl.textContent = prettyPrint(expected); + } +} diff --git a/packages/test-lib/src/client/html-repoter.js b/packages/test-lib/src/client/html-reporter/html-repoter.js similarity index 88% rename from packages/test-lib/src/client/html-repoter.js rename to packages/test-lib/src/client/html-reporter/html-repoter.js index a891dc7..0fb03ee 100644 --- a/packages/test-lib/src/client/html-repoter.js +++ b/packages/test-lib/src/client/html-reporter/html-repoter.js @@ -1,4 +1,5 @@ -import { Diagnostic, TestResult } from './components.js'; +import { Diagnostic } from './diagnostic.component.js'; +import { TestResult } from './test-result.component.js'; customElements.define('zora-diagnostic', Diagnostic); customElements.define('zora-test-result', TestResult); diff --git a/packages/test-lib/src/client/components.js b/packages/test-lib/src/client/html-reporter/test-result.component.js similarity index 61% rename from packages/test-lib/src/client/components.js rename to packages/test-lib/src/client/html-reporter/test-result.component.js index 55f0045..dd092a9 100644 --- a/packages/test-lib/src/client/components.js +++ b/packages/test-lib/src/client/html-reporter/test-result.component.js @@ -75,65 +75,3 @@ export class TestResult extends HTMLElement { } } } - -const diagnosticTemplate = document.createElement('template'); -diagnosticTemplate.innerHTML = ` -
- - -
- at -
-
-
-
- actual -

-  
-
- expected -

-  
-
-
-`; -export class Diagnostic extends HTMLElement { - #_diagnostic; - - get diagnostic() { - return this.#_diagnostic; - } - - set diagnostic(value) { - this.#_diagnostic = value; - this.render(); - } - - connectedCallback() { - this.replaceChildren(diagnosticTemplate.content.cloneNode(true)); - } - - render() { - if (!this.#_diagnostic) { - return; - } - - const { at, operator, description, expected, actual } = this.#_diagnostic; - - this.querySelector( - '.description', - ).textContent = `[${operator}] ${description}`; - - const locationElement = this.querySelector('.location'); - const locationURL = new URL(at); - const [_, row, column] = locationURL.search.split(':'); - locationElement.textContent = `${locationURL.pathname}:${row}:${column}`; - locationElement.setAttribute('href', at); - - const [actualEl, expectedEl] = this.querySelectorAll('pre'); - actualEl.textContent = prettyPrint(actual); - expectedEl.textContent = prettyPrint(expected); - } -} - -const prettyPrint = (value) => JSON.stringify(value, null, 2); diff --git a/packages/test-lib/src/client/index.js b/packages/test-lib/src/client/index.js index c11d4ba..9132d88 100644 --- a/packages/test-lib/src/client/index.js +++ b/packages/test-lib/src/client/index.js @@ -1,3 +1,3 @@ export * from './test.js'; export * from './socket-sink.js'; -export * from './html-repoter.js'; +export * from './html-reporter/html-repoter.js'; diff --git a/packages/test-lib/src/vite/index.js b/packages/test-lib/src/vite/index.js index 7184719..32dde46 100644 --- a/packages/test-lib/src/vite/index.js +++ b/packages/test-lib/src/vite/index.js @@ -1,18 +1,16 @@ import { ReadableStream } from 'node:stream/web'; import { createDiffReporter } from 'zora-reporters'; -const createStream = ({ ws, client: _client }) => { +const createStream = ({ ws }) => { let listener; return new ReadableStream({ start(controller) { listener = (data, client) => { - if (client === _client) { - if (data.type === 'STREAM_ENDED') { - controller.close(); - ws.off('zora', listener); - } else { - controller.enqueue(data); - } + if (data.type === 'STREAM_ENDED') { + controller.close(); + ws.off('zora', listener); + } else { + controller.enqueue(data); } }; @@ -24,11 +22,9 @@ export default () => ({ name: 'zora-dev', async configureServer(server) { const report = createDiffReporter(); - const socketStreams = new WeakMap(); - - server.ws.on('zora', async ({ type }, client) => { + server.ws.on('zora', async ({ type }) => { if (type === 'STREAM_STARTED') { - const readableStream = createStream({ ws: server.ws, client }); + const readableStream = createStream({ ws: server.ws }); await report(readableStream); } });