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 <noreply@anthropic.com>
This commit is contained in:
2026-03-16 12:15:09 +01:00
parent f1f63eced9
commit 431c179814

View File

@@ -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("--");
});
});
});