54 lines
2.3 KiB
TypeScript
54 lines
2.3 KiB
TypeScript
|
|
import { MoneyValue } from "./money-value";
|
||
|
|
|
||
|
|
describe("MoneyValue", () => {
|
||
|
|
test("should correctly instantiate with amount, scale, and currency", () => {
|
||
|
|
const money = new MoneyValue({ amount: 10000, scale: 2, currency_code: "EUR" });
|
||
|
|
expect(money.amount).toBe(100);
|
||
|
|
expect(money.currency).toBe("EUR");
|
||
|
|
expect(money.scale).toBe(2);
|
||
|
|
});
|
||
|
|
|
||
|
|
test("should add two MoneyValue instances with the same currency", () => {
|
||
|
|
const money1 = new MoneyValue({ amount: 10000, scale: 2, currency_code: "EUR" });
|
||
|
|
const money2 = new MoneyValue({ amount: 5000, scale: 2, currency_code: "EUR" });
|
||
|
|
const result = money1.add(money2);
|
||
|
|
expect(result.amount).toBe(150);
|
||
|
|
});
|
||
|
|
|
||
|
|
test("should subtract two MoneyValue instances with the same currency", () => {
|
||
|
|
const money1 = new MoneyValue({ amount: 20000, scale: 2, currency_code: "EUR" });
|
||
|
|
const money2 = new MoneyValue({ amount: 5000, scale: 2, currency_code: "EUR" });
|
||
|
|
const result = money1.subtract(money2);
|
||
|
|
expect(result.amount).toBe(150);
|
||
|
|
});
|
||
|
|
|
||
|
|
test("should throw an error when adding different currencies", () => {
|
||
|
|
const money1 = new MoneyValue({ amount: 10000, scale: 2, currency_code: "EUR" });
|
||
|
|
const money2 = new MoneyValue({ amount: 5000, scale: 2, currency_code: "USD" });
|
||
|
|
expect(() => money1.add(money2)).toThrow("Currency mismatch");
|
||
|
|
});
|
||
|
|
|
||
|
|
test("should correctly convert scale", () => {
|
||
|
|
const money = new MoneyValue({ amount: 10000, scale: 2, currency_code: "EUR" });
|
||
|
|
const converted = money.convertScale(4);
|
||
|
|
expect(converted.amount).toBe(100);
|
||
|
|
expect(converted.scale).toBe(4);
|
||
|
|
});
|
||
|
|
|
||
|
|
test("should format correctly according to locale", () => {
|
||
|
|
const money = new MoneyValue({ amount: 123456, scale: 2, currency_code: "EUR" });
|
||
|
|
expect(money.format("es-ES")).toBe("1.234,56 €");
|
||
|
|
expect(money.format("en-US")).toBe("€1,234.56");
|
||
|
|
});
|
||
|
|
|
||
|
|
test("should compare MoneyValue instances correctly", () => {
|
||
|
|
const money1 = new MoneyValue({ amount: 10000, scale: 2, currency_code: "EUR" });
|
||
|
|
const money2 = new MoneyValue({ amount: 10000, scale: 2, currency_code: "EUR" });
|
||
|
|
const money3 = new MoneyValue({ amount: 5000, scale: 2, currency_code: "EUR" });
|
||
|
|
|
||
|
|
expect(money1.equalsTo(money2)).toBe(true);
|
||
|
|
expect(money1.greaterThan(money3)).toBe(true);
|
||
|
|
expect(money3.lessThan(money1)).toBe(true);
|
||
|
|
});
|
||
|
|
});
|