TypeScript
refactor(web): test formatted timestamps instead of formatter options (#10221)
Julius Marminge585ce2c2ab39
diff --git a/apps/web/src/timestampFormat.test.ts b/apps/web/src/timestampFormat.test.tsindex 578587510db..ca73095f7fd 100644--- a/apps/web/src/timestampFormat.test.ts+++ b/apps/web/src/timestampFormat.test.ts@@ -8,37 +8,9 @@ import {formatRelativeTimeLabel,formatShortTimestamp,getRelativeTimeState,- getTimestampFormatOptions,resolveTimestampLocale,} from "./timestampFormat";-describe("getTimestampFormatOptions", () => {- it("omits hour12 when locale formatting is requested", () => {- expect(getTimestampFormatOptions("locale", true)).toEqual({- hour: "numeric",- minute: "2-digit",- second: "2-digit",- });- });-- it("builds a 12-hour formatter with seconds when requested", () => {- expect(getTimestampFormatOptions("12-hour", true)).toEqual({- hour: "numeric",- minute: "2-digit",- second: "2-digit",- hour12: true,- });- });-- it("builds a 24-hour formatter without seconds when requested", () => {- expect(getTimestampFormatOptions("24-hour", false)).toEqual({- hour: "numeric",- minute: "2-digit",- hour12: false,- });- });-});-describe("resolveTimestampLocale", () => {it("defers to the runtime default when the host reports no locale", () => {expect(resolveTimestampLocale(null)).toBeUndefined();@@ -57,19 +29,26 @@ describe("resolveTimestampLocale", () => {expect(resolveTimestampLocale("not a locale")).toBeUndefined();expect(resolveTimestampLocale("en_GB")).toBeUndefined();});+});- it("renders the host locale's hour cycle under the locale setting", () => {- const formatAt1544 = (systemLocale: string | null) =>- new Intl.DateTimeFormat(resolveTimestampLocale(systemLocale), {- ...getTimestampFormatOptions("locale", false),- timeZone: "UTC",- })- .format(new Date("2026-04-07T15:44:00.000Z"))- // ICU separates the day period with a narrow no-break space.- .replace(/[ ]/g, " ");+describe("formatShortTimestamp", () => {+ afterEach(() => {+ vi.unstubAllGlobals();+ vi.resetModules();+ });- expect(formatAt1544("en-GB")).toBe("15:44");- expect(formatAt1544("en-US")).toBe("3:44 PM");+ it.each([+ ["en-GB", "15:44"],+ ["en-US", "3:44 PM"],+ ])("honors %s and the explicit hour-cycle settings", async (locale, localTime) => {+ vi.stubGlobal("window", { desktopBridge: { getSystemLocale: () => locale } });+ vi.resetModules();+ const { formatShortTimestamp: format } = await import("./timestampFormat");+ const date = new Date(2026, 3, 7, 15, 44).toISOString();+ // ICU can separate the day period with a narrow no-break space.+ expect(format(date, "locale").replace(/[ ]/g, " ")).toBe(localTime);+ expect(format(date, "12-hour").replace(/[ ]/g, " ")).toMatch(/^3:44 [ap]m$/i);+ expect(format(date, "24-hour")).toBe("15:44");});});diff --git a/apps/web/src/timestampFormat.ts b/apps/web/src/timestampFormat.tsindex c6a9bdd29e1..0f87204efde 100644--- a/apps/web/src/timestampFormat.ts+++ b/apps/web/src/timestampFormat.ts@@ -1,6 +1,6 @@import { type TimestampFormat } from "@t3tools/contracts/settings";-export function getTimestampFormatOptions(+function getTimestampFormatOptions(timestampFormat: TimestampFormat,includeSeconds: boolean,): Intl.DateTimeFormatOptions {