From c16f63ba20d5ce648409b405d03848bc2797b141 Mon Sep 17 00:00:00 2001 From: Dan Ditomaso Date: Fri, 23 May 2025 13:06:33 -0400 Subject: [PATCH] fix: added support for i18n in testing. Fixed broken tests --- .../PageComponents/Connect/HTTP.tsx | 36 +++++++++---------- .../PageComponents/Map/NodeDetail.tsx | 2 +- .../Messages/TraceRoute.test.tsx | 20 +++++------ .../PageComponents/Messages/TraceRoute.tsx | 4 +-- src/tests/setupTests.ts | 22 ++++++++++++ vitest.config.ts | 22 ++++++------ 6 files changed, 64 insertions(+), 42 deletions(-) diff --git a/src/components/PageComponents/Connect/HTTP.tsx b/src/components/PageComponents/Connect/HTTP.tsx index 0846a4b0..c7c6250f 100644 --- a/src/components/PageComponents/Connect/HTTP.tsx +++ b/src/components/PageComponents/Connect/HTTP.tsx @@ -81,12 +81,12 @@ export const HTTP = ( disabled={connectionInProgress} >
- + @@ -98,7 +98,7 @@ export const HTTP = ( checked={isURLHTTPS || tlsValue} {...register("tls")} /> - +
{connectionError && ( @@ -110,38 +110,38 @@ export const HTTP = ( />

- {t("httpConnection.connectionFailedAlert.title")} + {t("httpConnection_connectionFailedAlert.title")}

- {t("httpConnection.connectionFailedAlert.descriptionPrefix")} + {t("httpConnection_connectionFailedAlert.descriptionPrefix")} {connectionError.secure && - t("httpConnection.connectionFailedAlert.httpsHint")} - {t("httpConnection.connectionFailedAlert.openLinkPrefix")} + t("httpConnection_connectionFailedAlert.httpsHint")} + {t("httpConnection_connectionFailedAlert.openLinkPrefix")} {`${ connectionError.secure - ? t("httpConnection.https") - : t("httpConnection.http") + ? t("httpConnection_https") + : t("httpConnection_http") }://${connectionError.host}`} {" "} - {t("httpConnection.connectionFailedAlert.openLinkSuffix")} + {t("httpConnection_connectionFailedAlert.openLinkSuffix")} {connectionError.secure ? t( - "httpConnection.connectionFailedAlert.acceptTlsWarningSuffix", + "httpConnection_connectionFailedAlert.acceptTlsWarningSuffix", ) : ""}.{" "} - {t("httpConnection.connectionFailedAlert.learnMoreLink")} + {t("httpConnection_connectionFailedAlert.learnMoreLink")}

@@ -155,8 +155,8 @@ export const HTTP = ( > {connectionInProgress - ? t("httpConnection.button_connecting") - : t("httpConnection.button_connect")} + ? t("httpConnection_button_connecting") + : t("httpConnection_button_connect")} diff --git a/src/components/PageComponents/Map/NodeDetail.tsx b/src/components/PageComponents/Map/NodeDetail.tsx index 36fd60e8..7b078296 100644 --- a/src/components/PageComponents/Map/NodeDetail.tsx +++ b/src/components/PageComponents/Map/NodeDetail.tsx @@ -218,7 +218,7 @@ export const NodeDetail = ({ node }: NodeDetailProps) => {
{t("node_detail_snr_label")}
{node.snr} - {t("common.dbUnit")} + {t("common_unit_dbm")} {Math.min(Math.max((node.snr + 10) * 5, 0), 100)}% diff --git a/src/components/PageComponents/Messages/TraceRoute.test.tsx b/src/components/PageComponents/Messages/TraceRoute.test.tsx index c3b8591c..67da8936 100644 --- a/src/components/PageComponents/Messages/TraceRoute.test.tsx +++ b/src/components/PageComponents/Messages/TraceRoute.test.tsx @@ -48,9 +48,9 @@ describe("TraceRoute", () => { expect(screen.getByText("Node B")).toBeInTheDocument(); expect(screen.getAllByText(/↓/)).toHaveLength(3); - expect(screen.getByText("↓ 10dB")).toBeInTheDocument(); - expect(screen.getByText("↓ 20dB")).toBeInTheDocument(); - expect(screen.getByText("↓ 30dB")).toBeInTheDocument(); + expect(screen.getByText("↓ 10dBm")).toBeInTheDocument(); + expect(screen.getByText("↓ 20dBm")).toBeInTheDocument(); + expect(screen.getByText("↓ 30dBm")).toBeInTheDocument(); }); it("renders the route back when provided", () => { @@ -74,11 +74,11 @@ describe("TraceRoute", () => { expect(screen.getByText("Node C")).toBeInTheDocument(); expect(screen.getByText("Node A")).toBeInTheDocument(); - expect(screen.getByText("↓ 35dB")).toBeInTheDocument(); - expect(screen.getByText("↓ 45dB")).toBeInTheDocument(); + expect(screen.getByText("↓ 35dBm")).toBeInTheDocument(); + expect(screen.getByText("↓ 45dBm")).toBeInTheDocument(); - expect(screen.getByText("↓ 15dB")).toBeInTheDocument(); - expect(screen.getByText("↓ 25dB")).toBeInTheDocument(); + expect(screen.getByText("↓ 15dBm")).toBeInTheDocument(); + expect(screen.getByText("↓ 25dBm")).toBeInTheDocument(); }); it("renders '??' for missing SNR values", () => { @@ -91,7 +91,7 @@ describe("TraceRoute", () => { ); expect(screen.getByText("Node A")).toBeInTheDocument(); - expect(screen.getAllByText("↓ ??dB")).toHaveLength(2); + expect(screen.getAllByText("↓ ??dBm")).toHaveLength(2); }); it("renders hop hex if node is not found", () => { @@ -105,7 +105,7 @@ describe("TraceRoute", () => { ); expect(screen.getByText(/^!63$/)).toBeInTheDocument(); - expect(screen.getByText("↓ 5dB")).toBeInTheDocument(); - expect(screen.getByText("↓ 15dB")).toBeInTheDocument(); + expect(screen.getByText("↓ 5dBm")).toBeInTheDocument(); + expect(screen.getByText("↓ 15dBm")).toBeInTheDocument(); }); }); diff --git a/src/components/PageComponents/Messages/TraceRoute.tsx b/src/components/PageComponents/Messages/TraceRoute.tsx index d5a9f4af..c6202504 100644 --- a/src/components/PageComponents/Messages/TraceRoute.tsx +++ b/src/components/PageComponents/Messages/TraceRoute.tsx @@ -35,7 +35,7 @@ const RoutePath = (

{startNode?.user?.longName}

↓ {snr?.[0] ?? t("traceRoute_snrUnknown")} - {t("common.dbUnit")} + {t("common_unit_dbm")}

{path.map((hop, i) => ( @@ -45,7 +45,7 @@ const RoutePath = (

↓ {snr?.[i + 1] ?? t("traceRoute_snrUnknown")} - {t("common.dbUnit")} + {t("common_unit_dbm")}

))} diff --git a/src/tests/setupTests.ts b/src/tests/setupTests.ts index e5826209..34c5fd07 100644 --- a/src/tests/setupTests.ts +++ b/src/tests/setupTests.ts @@ -3,6 +3,9 @@ import { cleanup } from "@testing-library/react"; import { enableMapSet } from "immer"; import "@testing-library/jest-dom"; import "@testing-library/user-event"; +import i18n from "i18next"; +import { initReactI18next } from "react-i18next"; +import enTranslations from "@app/i18n/locales/en.json"; enableMapSet(); @@ -14,12 +17,31 @@ vi.mock("idb-keyval", () => ({ keys: vi.fn(() => Promise.resolve([])), createStore: vi.fn(() => ({})), })); + globalThis.ResizeObserver = class { observe() {} unobserve() {} disconnect() {} }; +i18n + .use(initReactI18next) + .init({ + lng: "en", + fallbackLng: "en", + debug: false, + resources: { + en: { + translation: enTranslations, + }, + }, + interpolation: { + escapeValue: false, + }, + defaultNS: "translation", + initImmediate: true, + }); + afterEach(() => { cleanup(); }); diff --git a/vitest.config.ts b/vitest.config.ts index aaed988c..24aa0ac4 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -1,6 +1,6 @@ import path from "node:path"; -import react from '@vitejs/plugin-react'; -import { defineConfig } from 'vitest/config' +import react from "@vitejs/plugin-react"; +import { defineConfig } from "vitest/config"; import { enableMapSet } from "immer"; enableMapSet(); @@ -10,21 +10,21 @@ export default defineConfig({ ], resolve: { alias: { - '@app': path.resolve(process.cwd(), './src'), - '@core': path.resolve(process.cwd(), './src/core'), - '@pages': path.resolve(process.cwd(), './src/pages'), - '@components': path.resolve(process.cwd(), './src/components'), - '@layouts': path.resolve(process.cwd(), './src/layouts'), + "@app": path.resolve(process.cwd(), "./src"), + "@core": path.resolve(process.cwd(), "./src/core"), + "@pages": path.resolve(process.cwd(), "./src/pages"), + "@components": path.resolve(process.cwd(), "./src/components"), + "@layouts": path.resolve(process.cwd(), "./src/layouts"), }, }, test: { - environment: 'happy-dom', + environment: "happy-dom", globals: true, mockReset: true, clearMocks: true, restoreMocks: true, - root: path.resolve(process.cwd(), './src'), - include: ['**/*.{test,spec}.{ts,tsx}'], + root: path.resolve(process.cwd(), "./src"), + include: ["**/*.{test,spec}.{ts,tsx}"], setupFiles: ["./src/tests/setupTests.ts"], }, -}) \ No newline at end of file +});