Files
butcem/src/shared/quantity.test.ts
T
2026-11-05 02:57:58 +03:00

41 lines
1.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, expect, it } from "vitest";
import { convertQuantity, formatQuantity, parseQuantity } from "./quantity";
describe("parseQuantity", () => {
it.each([
["1,5", 1500],
["1.5", 1500],
["2", 2000],
["0,125", 125],
["5", 5000],
[" 10,25 ", 10250],
])("%s → %i", (input, expected) => {
expect(parseQuantity(input)).toBe(expected);
});
it.each(["", "-1", "1,2345", "1,2,3", "abc", "1e3"])("geçersiz: %s", (input) => {
expect(parseQuantity(input)).toBeNull();
});
});
describe("formatQuantity", () => {
it("gereksiz sıfırları atar, birim ekler", () => {
expect(formatQuantity(1500, "kg")).toBe("1,5 kg");
expect(formatQuantity(5000, "gr")).toBe("5 gr");
expect(formatQuantity(125)).toBe("0,125");
expect(formatQuantity(1234500)).toBe("1.234,5");
expect(formatQuantity(-250)).toBe("-0,25");
});
});
describe("convertQuantity", () => {
it("aynı boyutta dönüştürür", () => {
expect(convertQuantity(1500, "kg", "g")).toBe(1500000); // 1,5 kg = 1500 g
expect(convertQuantity(500000, "g", "kg")).toBe(500); // 500 g = 0,5 kg
expect(convertQuantity(2000, "lt", "ml")).toBe(2000000);
});
it("uyumsuz birimde hata verir", () => {
expect(() => convertQuantity(1000, "kg", "lt")).toThrow(RangeError);
});
});