Skip to content

Commit

Permalink
Fix frontend unit tests (#7864)
Browse files Browse the repository at this point in the history
* Fix unit tests

* Fix incorrect timezone

* Revert lock
  • Loading branch information
Etheryte authored Nov 14, 2023
1 parent b484f6b commit 02c177f
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 9 deletions.
10 changes: 8 additions & 2 deletions web/html/src/core/intl/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import React from "react";

import ReactDOMServer from "react-dom/server";

import { t } from "./index";
Expand All @@ -24,7 +26,9 @@ describe("new t()", () => {
};
const expected = 'foo <a href="/">bar</a>';

expect(ReactDOMServer.renderToStaticMarkup(<>{t(input, inputArgs)}</>)).toEqual(expected);
expect(
ReactDOMServer.renderToStaticMarkup(<React.Fragment key="key">{t(input, inputArgs)}</React.Fragment>)
).toEqual(expected);
});

test("tags with named placeholders", () => {
Expand All @@ -35,7 +39,9 @@ describe("new t()", () => {
};
const expected = 'foo <a href="/">something</a> bar';

expect(ReactDOMServer.renderToStaticMarkup(<>{t(input, inputArgs)}</>)).toEqual(expected);
expect(
ReactDOMServer.renderToStaticMarkup(<React.Fragment key="key">{t(input, inputArgs)}</React.Fragment>)
).toEqual(expected);
});

// This behavior allows existing `handleResponseError` implementations to pass `{ arg: undefined }` even when there is no arg
Expand Down
2 changes: 1 addition & 1 deletion web/html/src/core/log/loggerhead.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion web/html/src/utils/datetime/localizedMoment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
2 changes: 1 addition & 1 deletion web/html/src/utils/test-utils/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(url: string, response: T) {
return server.use(
rest.get(url, (req, res, ctx) => {
return res(ctx.json(response));
Expand Down
8 changes: 4 additions & 4 deletions web/html/src/utils/test-utils/setup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 02c177f

Please sign in to comment.