import { describe, expect, test } from "bun:test"; import { formatWeight } from "@/client/lib/formatters"; describe("formatWeight", () => { describe("grams (default)", () => { test("formats grams with no decimal", () => { expect(formatWeight(100, "g")).toBe("100g"); }); test("rounds fractional grams", () => { expect(formatWeight(99.6, "g")).toBe("100g"); }); test("formats zero grams", () => { expect(formatWeight(0, "g")).toBe("0g"); }); test("defaults to grams when no unit specified (backward compatible)", () => { expect(formatWeight(100)).toBe("100g"); }); }); describe("ounces", () => { test("converts grams to ounces with 1 decimal", () => { expect(formatWeight(100, "oz")).toBe("3.5 oz"); }); test("converts 1000g to ounces", () => { expect(formatWeight(1000, "oz")).toBe("35.3 oz"); }); test("formats zero as ounces", () => { expect(formatWeight(0, "oz")).toBe("0.0 oz"); }); }); describe("pounds", () => { test("converts grams to pounds with 2 decimals", () => { expect(formatWeight(1000, "lb")).toBe("2.20 lb"); }); test("handles small weight in pounds", () => { expect(formatWeight(5, "lb")).toBe("0.01 lb"); }); test("formats zero as pounds", () => { expect(formatWeight(0, "lb")).toBe("0.00 lb"); }); }); describe("kilograms", () => { test("converts grams to kilograms with 2 decimals", () => { expect(formatWeight(1500, "kg")).toBe("1.50 kg"); }); test("handles large weight in kilograms", () => { expect(formatWeight(50000, "kg")).toBe("50.00 kg"); }); test("converts 1000g to 1 kg", () => { expect(formatWeight(1000, "kg")).toBe("1.00 kg"); }); test("formats zero as kilograms", () => { expect(formatWeight(0, "kg")).toBe("0.00 kg"); }); }); describe("null and undefined handling", () => { test("returns '--' for null with no unit", () => { expect(formatWeight(null)).toBe("--"); }); test("returns '--' for null with grams", () => { expect(formatWeight(null, "g")).toBe("--"); }); test("returns '--' for null with ounces", () => { expect(formatWeight(null, "oz")).toBe("--"); }); test("returns '--' for null with pounds", () => { expect(formatWeight(null, "lb")).toBe("--"); }); test("returns '--' for null with kilograms", () => { expect(formatWeight(null, "kg")).toBe("--"); }); test("returns '--' for undefined with kilograms", () => { expect(formatWeight(undefined, "kg")).toBe("--"); }); test("returns '--' for undefined with no unit", () => { expect(formatWeight(undefined)).toBe("--"); }); }); });