From 02c177fe28b25baf77202abd7e5ae922de3c6734 Mon Sep 17 00:00:00 2001 From: Karl Tarvas Date: Tue, 14 Nov 2023 20:01:39 +0100 Subject: [PATCH] Fix frontend unit tests (#7864) * Fix unit tests * Fix incorrect timezone * Revert lock --- web/html/src/core/intl/index.test.tsx | 10 ++++++++-- web/html/src/core/log/loggerhead.ts | 2 +- web/html/src/utils/datetime/localizedMoment.test.ts | 3 ++- web/html/src/utils/test-utils/server.ts | 2 +- web/html/src/utils/test-utils/setup/index.ts | 8 ++++---- 5 files changed, 16 insertions(+), 9 deletions(-) diff --git a/web/html/src/core/intl/index.test.tsx b/web/html/src/core/intl/index.test.tsx index 5efa1ad632da..12321b2b80fa 100644 --- a/web/html/src/core/intl/index.test.tsx +++ b/web/html/src/core/intl/index.test.tsx @@ -1,3 +1,5 @@ +import React from "react"; + import ReactDOMServer from "react-dom/server"; import { t } from "./index"; @@ -24,7 +26,9 @@ describe("new t()", () => { }; const expected = 'foo bar'; - expect(ReactDOMServer.renderToStaticMarkup(<>{t(input, inputArgs)})).toEqual(expected); + expect( + ReactDOMServer.renderToStaticMarkup({t(input, inputArgs)}) + ).toEqual(expected); }); test("tags with named placeholders", () => { @@ -35,7 +39,9 @@ describe("new t()", () => { }; const expected = 'foo something bar'; - expect(ReactDOMServer.renderToStaticMarkup(<>{t(input, inputArgs)})).toEqual(expected); + expect( + ReactDOMServer.renderToStaticMarkup({t(input, inputArgs)}) + ).toEqual(expected); }); // This behavior allows existing `handleResponseError` implementations to pass `{ arg: undefined }` even when there is no arg diff --git a/web/html/src/core/log/loggerhead.ts b/web/html/src/core/log/loggerhead.ts index c4d27f738841..4cab0d570f38 100644 --- a/web/html/src/core/log/loggerhead.ts +++ b/web/html/src/core/log/loggerhead.ts @@ -21,7 +21,7 @@ export default class Loggerhead { // We hijack the global console to ensure errors thrown in third-party code get logged too // If we're running unit tests in a Node env, skip this - if (typeof window !== "undefined") { + if (window.location.hostname !== "localhost" && typeof window !== "undefined" && process.env.NODE_ENV !== "test") { console.log = this.log; console.info = this.info; console.debug = this.debug; diff --git a/web/html/src/utils/datetime/localizedMoment.test.ts b/web/html/src/utils/datetime/localizedMoment.test.ts index d5c3bca1569d..1ae1e460e855 100644 --- a/web/html/src/utils/datetime/localizedMoment.test.ts +++ b/web/html/src/utils/datetime/localizedMoment.test.ts @@ -59,11 +59,12 @@ describe("localizedMoment", () => { }); test("full server string keeps offset", () => { + // Japan doesn't observe daylight saving time expect(localizedMoment().toServerString()).toContain("JST"); }); test("full user string keeps offset", () => { - expect(localizedMoment().toUserString()).toContain("PDT"); + expect(localizedMoment().toUserString()).toMatch(/PST|PDT/); }); test("calendar output uses config formats", () => { diff --git a/web/html/src/utils/test-utils/server.ts b/web/html/src/utils/test-utils/server.ts index 137a6725ec57..33488c0d22c6 100644 --- a/web/html/src/utils/test-utils/server.ts +++ b/web/html/src/utils/test-utils/server.ts @@ -4,7 +4,7 @@ import { setupServer } from "msw/node"; const baseServer = setupServer(); const serverAddons = { /** Mock a GET request to `url` with a successful JSON response containing `response` */ - mockGetJson(url, response) { + mockGetJson(url: string, response: T) { return server.use( rest.get(url, (req, res, ctx) => { return res(ctx.json(response)); diff --git a/web/html/src/utils/test-utils/setup/index.ts b/web/html/src/utils/test-utils/setup/index.ts index 1a2944e1a084..fa0f9f16d0cb 100644 --- a/web/html/src/utils/test-utils/setup/index.ts +++ b/web/html/src/utils/test-utils/setup/index.ts @@ -17,9 +17,9 @@ global.t = t; const loggerHead = new Loggerhead("", (headers) => headers); -loggerHead.info = (message: string) => console.info(`[Loggerhead] INFO : ${message}`); -loggerHead.debug = (message: string) => console.debug(`[Loggerhead] DEBUG : ${message}`); -loggerHead.warn = (message: string) => console.warn(`[Loggerhead] WARN : ${message}`); -loggerHead.error = (message: string) => console.error(`[Loggerhead] ERROR : ${message}`); +loggerHead.info = console.info.bind(console, "[Loggerhead] INFO:"); +loggerHead.debug = console.debug.bind(console, "[Loggerhead] DEBUG:"); +loggerHead.warn = console.warn.bind(console, "[Loggerhead] WARN:"); +loggerHead.error = console.error.bind(console, "[Loggerhead] ERROR:"); global.Loggerhead = loggerHead;