From 431c179814fad88a6140dd92979b7926e25f8364 Mon Sep 17 00:00:00 2001 From: Jean-Luc Makiola Date: Mon, 16 Mar 2026 12:15:09 +0100 Subject: [PATCH] test(07-01): add failing tests for formatWeight unit conversion - Tests for all 4 units (g, oz, lb, kg) with known gram values - Null and undefined handling for each unit - Default parameter backward compatibility - Zero and precision edge cases - 11 pass (existing behavior), 10 fail (new unit conversion) Co-Authored-By: Claude Opus 4.6 --- tests/lib/formatters.test.ts | 98 ++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 tests/lib/formatters.test.ts diff --git a/tests/lib/formatters.test.ts b/tests/lib/formatters.test.ts new file mode 100644 index 0000000..c31a4c6 --- /dev/null +++ b/tests/lib/formatters.test.ts @@ -0,0 +1,98 @@ +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("--"); + }); + }); +});