import { describe, it, expect } from "vitest"; import { formatCurrency } from "./format-currency"; import { EMPTY } from "./placeholder"; describe("formatCurrency", () => { it("formats numbers as USD by default", () => { expect(formatCurrency(1234.5)).toBe("$1,234.50"); expect(formatCurrency(0)).toBe("$0.00"); }); it("coerces numeric strings (schema decimals arrive as strings)", () => { expect(formatCurrency("1234.5")).toBe("$1,234.50"); }); it("returns the canonical EMPTY placeholder for null/undefined/NaN so callers can pass raw values", () => { expect(formatCurrency(null)).toBe(EMPTY); expect(formatCurrency(undefined)).toBe(EMPTY); expect(formatCurrency("not a number")).toBe(EMPTY); }); it("honors an explicit currency code", () => { expect(formatCurrency(1234.5, "EUR")).toBe("€1,234.50"); }); it("respects an optional maxDecimals cap", () => { expect(formatCurrency(1234.5678, "USD", 4)).toBe("$1,234.5678"); }); it("falls back to a bare localized number on an invalid currency code", () => { // Intl.NumberFormat throws on garbage ISO codes; we must not crash. expect(formatCurrency(1234.5, "NOTACODE")).toBe("1,234.50"); }); });