Files
hcl-finances-manager/tests/domain/money.test.ts
T

94 lines
3.3 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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 {
bpToPercentLabel,
centsFromDecimal,
decimalFromCents,
formatEuro,
fullSplit,
parseEuro,
percentToBp,
shareAmounts,
validateSplit,
} from "@/domain/money";
const fr = (n: number) =>
new Intl.NumberFormat("fr-FR", { style: "currency", currency: "EUR" }).format(n);
describe("parseEuro", () => {
it("accepts French and dotted inputs", () => {
expect(parseEuro("86,40")).toBe(8640);
expect(parseEuro("86.40")).toBe(8640);
expect(parseEuro("1 234,56")).toBe(123456);
expect(parseEuro("1 234,56")).toBe(123456);
expect(parseEuro("1 234,56")).toBe(123456);
expect(parseEuro("1234")).toBe(123400);
expect(parseEuro("0,5")).toBe(50);
});
it("accepts a euro sign and dots as thousands separators", () => {
expect(parseEuro("40 000 €")).toBe(4000000);
expect(parseEuro("40000€")).toBe(4000000);
expect(parseEuro("40.000")).toBe(4000000);
expect(parseEuro("1.234.567")).toBe(123456700);
expect(parseEuro("40.000,50")).toBe(4000050);
});
it("rejects empty, zero, negative and malformed amounts", () => {
for (const s of ["", "0", "0,00", "-5", "abc", "12,345", "1,2,3"]) expect(parseEuro(s)).toBeNull();
});
});
describe("formatEuro", () => {
it("formats cents with Intl fr-FR", () => {
expect(formatEuro(123456)).toBe(fr(1234.56));
});
it("uses a real minus sign and an explicit plus when asked", () => {
expect(formatEuro(-8640, { sign: true })).toBe("−" + fr(86.4));
expect(formatEuro(500, { sign: true })).toBe("+" + fr(5));
expect(formatEuro(-8640)).toBe("−" + fr(86.4));
});
});
describe("decimal conversions", () => {
it("round-trips between DB decimals and cents", () => {
expect(centsFromDecimal("86.40")).toBe(8640);
expect(centsFromDecimal("86.4")).toBe(8640);
expect(centsFromDecimal("-12.05")).toBe(-1205);
expect(centsFromDecimal({ toString: () => "1000.00" })).toBe(100000);
expect(decimalFromCents(8640)).toBe("86.40");
expect(decimalFromCents(5)).toBe("0.05");
});
});
describe("splits", () => {
it("accepts splits summing to 100 %", () => {
expect(validateSplit({ a: 5000, b: 5000 })).toBeNull();
expect(validateSplit({ a: 3333, b: 6667 })).toBeNull();
});
it("rejects splits not summing to 100 % or with negatives", () => {
expect(validateSplit({ a: 3333, b: 3333 })).toBe("La répartition doit totaliser 100 %.");
expect(validateSplit({ a: -100, b: 10100 })).toBe("Les pourcentages doivent être positifs.");
});
it("gives the rounding residue to the account holder", () => {
expect(shareAmounts(1000, { a: 3333, b: 6667 }, "a")).toEqual({ a: 334, b: 666 });
expect(shareAmounts(8640, { a: 5000, b: 5000 }, "b")).toEqual({ a: 4320, b: 4320 });
expect(shareAmounts(1, { a: 5000, b: 5000 }, "b")).toEqual({ a: 0, b: 1 });
});
it("converts percentages to and from basis points", () => {
expect(percentToBp("33,33")).toBe(3333);
expect(percentToBp("50")).toBe(5000);
expect(percentToBp("abc")).toBeNull();
expect(percentToBp("101")).toBeNull();
expect(bpToPercentLabel(3333)).toBe("33,33");
expect(bpToPercentLabel(5000)).toBe("50");
});
it("builds a 100 % split for one person", () => {
expect(fullSplit("a", ["a", "b"])).toEqual({ a: 10000, b: 0 });
});
});