gram/packages/renderer/tests/amount-rounding.test.ts
abiwab 9af07c9a3c
All checks were successful
CI / Build & Test (pull_request) Successful in 5m50s
fix(renderer): show a quantity rounded to its size, not to a decimal of its unit
2026-11-22 11:32:13 +01:00

34 lines
1.1 KiB
TypeScript

import { describe, expect, it } from "bun:test";
import { formatDecimalToFraction, formatGrams } from "../src/shared/utils";
// #230: a quantity is shown rounded to its size, not to a decimal of its
// unit: `0.15 kg` read `0.2 kg`, `0.02 kg` read `0 kg`, `0.05 l` read
// `0.1 l`, and a pinch of saffron `0 g`.
describe("a quantity as a cook reads it (#230)", () => {
for (const [value, unit, shown] of [
[0.03, "g", "0.03"],
[0.004, "g", "0.004"],
[0.02, "kg", "0.02"],
[0.15, "kg", "0.15"],
[1.15, "kg", "1.15"],
[0.05, "l", "0.05"],
[152.37, "g", "152"],
[15.24, "g", "15.2"],
[0.5, "cup", "1/2"],
[0.25, "kg", "1/4"],
] as const) {
it(`shows ${value} ${unit} as ${shown} ${unit}`, () => {
expect(formatDecimalToFraction(value, unit)).toBe(shown);
});
}
it("writes a decimal comma in French", () => {
expect(formatDecimalToFraction(0.15, "kg", { lang: "fr" })).toBe("0,15");
});
it("keeps one decimal for a weight in grams, more below 1 g", () => {
expect(formatGrams(147.5)).toBe("147.5 g");
expect(formatGrams(0.03)).toBe("0.03 g");
expect(formatGrams(0.0047)).toBe("0.0047 g");
});
});