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

88 lines
2.5 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 { allocate, formatMoneyInput, parseMoney, splitEvenly, sumMoney } from "./money";
describe("parseMoney", () => {
it.each([
["1.250,50", 125050],
["1250,50", 125050],
["1250,5", 125050],
["1250", 125000],
["0,01", 1],
[",5", 50],
["1.250", 125000],
["12.345.678,90", 1234567890],
["12.5", 1250],
["12.50", 1250],
["₺1.250,50", 125050],
["1.250,50 TL", 125050],
[" 1 250,50 ", 125050],
["-12,30", -1230],
["−12,30", -1230],
["+7", 700],
["0", 0],
["-0", 0],
])("%s → %i", (input, expected) => {
expect(parseMoney(input)).toBe(expected);
});
it.each([
"",
"abc",
"1,2,3",
"12,345",
"1.25,00",
"1.2345",
"12.345.6",
"1,5e3",
"--5",
"100000000000,01",
])("geçersiz: %s", (input) => {
expect(parseMoney(input)).toBeNull();
});
});
describe("formatMoneyInput", () => {
it("Türkçe biçimde simgesiz yazar ve parseMoney ile geri döner", () => {
for (const v of [0, 1, 99, 100, 125050, 1234567890, -1230]) {
expect(parseMoney(formatMoneyInput(v))).toBe(v);
}
expect(formatMoneyInput(125050)).toBe("1.250,50");
expect(formatMoneyInput(-5)).toBe("-0,05");
});
});
describe("allocate", () => {
it("toplam her zaman korunur (kuruş kaybı yok)", () => {
for (const total of [1, 100, 10000, 99999, 120000, -10001]) {
for (const weights of [[1], [1, 1, 1], [800, 250, 150], [1, 2, 3, 4, 5, 6, 7], [0, 1]]) {
const parts = allocate(total, weights);
expect(parts.reduce((a, b) => a + b, 0)).toBe(total);
expect(parts).toHaveLength(weights.length);
}
}
});
it("ağırlıklara orantılıdır ve deterministiktir", () => {
expect(allocate(120000, [800, 250, 150])).toEqual([80000, 25000, 15000]);
expect(allocate(100, [1, 1, 1])).toEqual([34, 33, 33]);
expect(allocate(-100, [1, 1, 1])).toEqual([-34, -33, -33]);
expect(allocate(5, [0, 1])).toEqual([0, 5]);
});
it("geçersiz girdiyi reddeder", () => {
expect(() => allocate(100, [])).toThrow(RangeError);
expect(() => allocate(100, [0, 0])).toThrow(RangeError);
expect(() => allocate(100, [-1, 2])).toThrow(RangeError);
expect(() => allocate(1.5, [1])).toThrow(TypeError);
});
});
describe("splitEvenly", () => {
it("9 taksit: 1.000,00 TL", () => {
const parts = splitEvenly(100000, 9);
expect(sumMoney(parts)).toBe(100000);
expect(parts[0]).toBe(11112);
expect(parts.slice(1).every((p) => p === 11111)).toBe(true);
});
});