import { afterEach, describe, expect, test, vi } from "vitest"; import { MISSING_NUMBER_PLACEHOLDER, formatCurrency, formatDate, formatDateTime, formatNumber, formatRelativeDate, formatRelativeTime, toFiniteNumber, } from "./format"; describe("locale formatters", () => { afterEach(() => { vi.restoreAllMocks(); }); test("renders a dash instead of NaN for values that are not finite numbers", () => { // A Prisma Decimal that survived loader serialization as an object used to // reach this formatter as NaN and rendered as "NaN €". expect(formatCurrency(Number.NaN, "EUR", "de")).toBe(MISSING_NUMBER_PLACEHOLDER); expect(formatNumber(Number.POSITIVE_INFINITY, "de")).toBe(MISSING_NUMBER_PLACEHOLDER); expect(formatCurrency(19.99, "EUR", "de")).not.toBe(MISSING_NUMBER_PLACEHOLDER); }); test("coerces loader payload shapes into finite numbers", () => { expect(toFiniteNumber(19.99)).toBe(19.99); expect(toFiniteNumber("19.99")).toBe(19.99); expect(toFiniteNumber({ toString: () => "299.99" })).toBe(299.99); expect(toFiniteNumber(Number.NaN)).toBeNull(); expect(toFiniteNumber(undefined)).toBeNull(); expect(toFiniteNumber("")).toBeNull(); expect(toFiniteNumber({})).toBeNull(); }); test("reuses number formatters for repeated locale and option combinations", () => { const constructorSpy = vi.spyOn(Intl, "NumberFormat"); expect(formatNumber(1_234.5, "en-US", { maximumFractionDigits: 1 })).toBe("1,234.5"); expect(formatNumber(9_876.5, "en-US", { maximumFractionDigits: 1 })).toBe("9,876.5"); expect(constructorSpy).toHaveBeenCalledTimes(1); }); test("keeps separate number formatters for different options", () => { const constructorSpy = vi.spyOn(Intl, "NumberFormat"); expect(formatNumber(1_234.5, "en-US", { minimumFractionDigits: 3 })).toBe("1,234.500"); expect(formatNumber(1_234.5, "en-US", { minimumFractionDigits: 4 })).toBe("1,234.5000"); expect(constructorSpy).toHaveBeenCalledTimes(2); }); test("reuses currency formatter instances", () => { const constructorSpy = vi.spyOn(Intl, "NumberFormat"); expect(formatCurrency(42, "USD", "en-US")).toBe("$42.00"); expect(formatCurrency(99, "USD", "en-US")).toBe("$99.00"); expect(constructorSpy).toHaveBeenCalledTimes(1); }); test("formats euro prices with German separators and two decimal places", () => { expect(formatCurrency(10_000, "EUR", "de").replace(/[\u00a0\u202f]/g, " ")).toBe("10.000,00 €"); }); test("reuses date-time formatters for repeated locale and options", () => { const constructorSpy = vi.spyOn(Intl, "DateTimeFormat"); const options: Intl.DateTimeFormatOptions = { dateStyle: "medium", timeZone: "UTC" }; expect(formatDate("2026-05-27T10:30:00.000Z", "en-US", options)).toBe("May 27, 2026"); expect(formatDate("2026-05-28T10:30:00.000Z", "en-US", options)).toBe("May 28, 2026"); expect(constructorSpy).toHaveBeenCalledTimes(1); }); test("formats date-times in the fixed app time zone by default", () => { // 10:00 UTC is 11:00 in Europe/Berlin (CET) — deterministic regardless of // the host time zone, so SSR and client output match (React #418). expect(formatDateTime("2026-01-15T10:00:00.000Z", "de")).toBe("15.01.2026, 11:00"); // During DST (CEST) the offset is +2 hours. expect(formatDateTime("2026-07-15T10:00:00.000Z", "de")).toBe("15.07.2026, 12:00"); }); test("keeps an explicitly passed time zone over the app default", () => { expect(formatDateTime("2026-01-15T10:00:00.000Z", "de", { timeZone: "UTC" })).toBe("15.01.2026, 10:00"); }); test("preserves relative date output", () => { const relativeTo = new Date("2026-05-27T10:00:00.000Z"); expect(formatRelativeDate("2026-05-26T10:00:00.000Z", "en-US", { relativeTo })).toBe("yesterday"); expect(formatRelativeDate("2026-05-28T10:00:00.000Z", "en-US", { relativeTo })).toBe("tomorrow"); }); test("preserves relative time output", () => { expect(formatRelativeTime(-2, "day", "en-US")).toBe("2 days ago"); expect(formatRelativeTime(3, "hour", "en-US")).toBe("in 3 hours"); }); });