/** * Author: Simon-Pierre Boucher * Contact: contact@spboucher.ai * Project: Groupe Ka / Ka Maps */ import { describe, expect, it } from "vitest"; import { formatCompactPrice, formatFullPrice, formatPercent, median, } from "../src/utils/format.js"; describe("formatCompactPrice", () => { it("formats the spec examples", () => { expect(formatCompactPrice(289_000)).toBe("289 k$"); expect(formatCompactPrice(499_900)).toBe("500 k$"); expect(formatCompactPrice(1_250_000)).toBe("1,25 M$"); expect(formatCompactPrice(2_800_000)).toBe("2,8 M$"); }); it("keeps rents in whole dollars", () => { expect(formatCompactPrice(950)).toBe("950 $"); // Group separator is U+202F (narrow no-break space), fr-CA convention. expect(formatCompactPrice(1450)).toBe("1 450 $"); }); it("rounds 999 500+ into millions", () => { expect(formatCompactPrice(999_600)).toBe("1 M$"); }); it("handles large and degenerate values", () => { expect(formatCompactPrice(12_300_000)).toBe("12,3 M$"); expect(formatCompactPrice(Number.NaN)).toBe(""); expect(formatCompactPrice(-5)).toBe(""); }); }); describe("formatFullPrice", () => { it("groups with narrow no-break spaces", () => { expect(formatFullPrice(589_000)).toBe("589 000 $"); expect(formatFullPrice(1_250_000)).toBe("1 250 000 $"); }); }); describe("formatPercent", () => { it("signs and localizes", () => { expect(formatPercent(0.058)).toBe("+5,8 %"); expect(formatPercent(-0.012)).toBe("-1,2 %"); expect(formatPercent(0)).toBe("0,0 %"); }); }); describe("median", () => { it("computes odd/even medians and ignores junk", () => { expect(median([3, 1, 2])).toBe(2); expect(median([4, 1, 2, 3])).toBe(2.5); expect(median([1, Number.NaN, 3])).toBe(2); expect(median([])).toBeUndefined(); }); });