diff --git a/frontend/src/lib/utils.test.ts b/frontend/src/lib/utils.test.ts index 3fe64fb..7906be6 100644 --- a/frontend/src/lib/utils.test.ts +++ b/frontend/src/lib/utils.test.ts @@ -1,11 +1,22 @@ import { describe, expect, it } from "vitest"; -import { cn, formatScore, statusBadge } from "@/lib/utils"; +import { cn, formatNumber, formatScore, statusBadge } from "@/lib/utils"; describe("utils", () => { it("merges class names", () => { expect(cn("px-2", "px-4")).toContain("px-4"); }); + it("formats finite numbers compactly and handles invalid values", () => { + expect(formatNumber(1200)).toBe("1.2K"); + expect(formatNumber(0)).toBe("0"); + expect(formatNumber(-1200)).toBe("-1.2K"); + expect(formatNumber(null)).toBe("—"); + expect(formatNumber(undefined)).toBe("—"); + expect(formatNumber(Number.NaN)).toBe("—"); + expect(formatNumber(Number.POSITIVE_INFINITY)).toBe("—"); + expect(formatNumber(Number.NEGATIVE_INFINITY)).toBe("—"); + }); + it("formats scores", () => { expect(formatScore(87.4)).toBe("87"); expect(formatScore(null)).toBe("—"); diff --git a/frontend/src/lib/utils.ts b/frontend/src/lib/utils.ts index 15f08ef..f077955 100644 --- a/frontend/src/lib/utils.ts +++ b/frontend/src/lib/utils.ts @@ -6,7 +6,7 @@ export function cn(...inputs: ClassValue[]) { } export function formatNumber(n: number | null | undefined): string { - if (n == null) return "—"; + if (n == null || !Number.isFinite(n)) return "—"; return new Intl.NumberFormat("en-US", { notation: "compact" }).format(n); }