55 lines
2.2 KiB
TypeScript
55 lines
2.2 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import { formatQuantity, parseNumber, scale, shoppingQuantity, toBase } from "@/lib/units";
|
|
|
|
describe("units", () => {
|
|
it("scales per-portion amounts", () => {
|
|
expect(scale(125, 4)).toBe(500);
|
|
expect(scale(0.25, 4)).toBe(1);
|
|
});
|
|
|
|
it("formats weights and volumes the way a kitchen reads them", () => {
|
|
expect(formatQuantity(125, "g")).toEqual({ amount: "125", unit: "g" });
|
|
expect(formatQuantity(1500, "g")).toEqual({ amount: "1,5", unit: "kg" });
|
|
expect(formatQuantity(1000, "ml")).toEqual({ amount: "1", unit: "l" });
|
|
expect(formatQuantity(482, "g")).toEqual({ amount: "480", unit: "g" });
|
|
expect(formatQuantity(2.3, "g")).toEqual({ amount: "2,5", unit: "g" });
|
|
expect(formatQuantity(1500, "g", "en")).toEqual({ amount: "1.5", unit: "kg" });
|
|
});
|
|
|
|
it("shows pieces as kitchen fractions", () => {
|
|
expect(formatQuantity(0.5, "piece").amount).toBe("½");
|
|
expect(formatQuantity(1.25, "piece").amount).toBe("1¼");
|
|
expect(formatQuantity(0.75, "piece").amount).toBe("¾");
|
|
expect(formatQuantity(2, "piece").amount).toBe("2");
|
|
expect(formatQuantity(1.4, "piece").amount).toBe("1,4");
|
|
});
|
|
|
|
it("rounds pieces up for shopping", () => {
|
|
expect(shoppingQuantity(0.25, "piece")).toBe(1);
|
|
expect(shoppingQuantity(2, "piece")).toBe(2);
|
|
expect(shoppingQuantity(480, "g")).toBe(480);
|
|
});
|
|
|
|
it("converts kitchen measures into the base unit, and refuses what does not fit", () => {
|
|
expect(toBase(1.5, "kg", "g")).toBe(1500);
|
|
expect(toBase(2, "tbsp", "ml")).toBe(30);
|
|
expect(toBase(1, "l", "ml")).toBe(1000);
|
|
expect(toBase(2, "tsp", "g")).toBe(8);
|
|
expect(toBase(3, "piece", "piece")).toBe(3);
|
|
expect(toBase(200, "g", "piece")).toBeNull();
|
|
expect(toBase(2, "piece", "g")).toBeNull();
|
|
expect(toBase(-1, "g", "g")).toBeNull();
|
|
});
|
|
|
|
it("reads numbers in German and English notation", () => {
|
|
expect(parseNumber("1,5")).toBe(1.5);
|
|
expect(parseNumber("1.5")).toBe(1.5);
|
|
expect(parseNumber("1 1/2")).toBe(1.5);
|
|
expect(parseNumber("½")).toBe(0.5);
|
|
expect(parseNumber("2½")).toBe(2.5);
|
|
expect(parseNumber("3/4")).toBe(0.75);
|
|
expect(parseNumber("")).toBeNull();
|
|
expect(parseNumber("etwas")).toBeNull();
|
|
});
|
|
});
|