import { describe, it, expect } from "vitest"; import { formatDecimal, formatNumber, formatPercent, formatQtyDual } from "./utils"; describe("formatQtyDual", () => { it("shows both figures when a conversion happened", () => { expect(formatQtyDual(29.96, "ton", 29960, "kg")).toBe( `${formatNumber(29.96)} ton · ${formatNumber(29960)} kg`, ); }); it("shows one figure when the units match", () => { expect(formatQtyDual(5, "ton", 5, "ton")).toBe("5 ton"); }); it("shows one figure when the slip recorded no unit", () => { expect(formatQtyDual(5, "ton", 5, "")).toBe("5 ton"); }); it("omits the unit entirely when the PO line has none", () => { expect(formatQtyDual(5, "", 5, "")).toBe("5"); }); it("falls back to the raw figure when nothing converted", () => { expect(formatQtyDual(0, "ton", 12, "m3")).toBe("0 ton · 12 m3"); }); }); // en-ZA everywhere a person reads a number: comma decimal, space // thousands. Mixing toFixed ("38.7%") with Intl ("61,3%") on one card // was the bug these pin. describe("formatPercent", () => { it("uses the en-ZA decimal comma", () => { expect(formatPercent(38.66)).toBe("38,7%"); }); it("keeps trailing zeros so a column lines up", () => { expect(formatPercent(12, 1)).toBe("12,0%"); }); it("rounds to whole percent when asked", () => { expect(formatPercent(7.4, 0)).toBe("7%"); }); it("signs a variance when asked", () => { expect(formatPercent(4.25, 1, { signed: true })).toBe("+4,3%"); expect(formatPercent(-99.6, 1, { signed: true })).toBe("-99,6%"); expect(formatPercent(0, 1, { signed: true })).toBe("0,0%"); }); }); describe("formatDecimal", () => { it("fixes the digits and uses en-ZA separators", () => { expect(formatDecimal(0.07, 2)).toBe("0,07"); expect(formatDecimal(1234.5, 1)).toBe(formatNumber(1234.5, { minimumFractionDigits: 1, maximumFractionDigits: 1 })); }); });