refactor(web): test formatted timestamps instead of formatter options (#10221)
This commit is contained in:
1 parent
183c343309
commit
585ce2c2ab
2 files changed
+19
-40
No files matched your search
@@ -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");
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { type TimestampFormat } from "@t3tools/contracts/settings";
|
||||
|
||||
export function getTimestampFormatOptions(
|
||||
function getTimestampFormatOptions(
|
||||
timestampFormat: TimestampFormat,
|
||||
includeSeconds: boolean,
|
||||
): Intl.DateTimeFormatOptions {
|
||||
|
||||
Reference in new issue
Block a user