Spaces:
Running

AHMDRTAP / src /lib /format.test.ts
ahmdelbaz28-ux
fix(pwa+numerals): stuck-device SW cure — build-signed SW URL; device-locale-proof digits
c7ed040
Raw History Blame Contribute Delete
2.21 kB
import { describe, expect, it } from "vitest";
import { formatDateTime, formatDate, formatNumber } from "./format";
/**
* Root-cause regression tests (owner report 2026-12-03): every displayed
* number must use an EXPLICIT locale so digits can never follow the
* device/browser locale. These tests pin the contract.
*/
describe("formatNumber", () => {
it("formats with Western digits regardless of runtime locale", () => {
expect(formatNumber(1234)).toBe("1,234");
expect(formatNumber(500)).toBe("500");
expect(formatNumber(0)).toBe("0");
expect(formatNumber(1234567)).toBe("1,234,567");
});
it("never emits Arabic-Indic digits (device-locale-proof)", () => {
for (const v of [0, 7, 99, 1234, 987654, 2500000]) {
const out = formatNumber(v);
expect(out).not.toMatch(/[\u0660-\u0669]/);
expect(out).not.toMatch(/[\u06F0-\u06F9]/);
}
});
it("falls back to 0 for non-finite input", () => {
expect(formatNumber(Number.NaN)).toBe("0");
expect(formatNumber(Number.POSITIVE_INFINITY)).toBe("0");
});
});
describe("formatDate", () => {
it("English site language uses en-US digits and no Arabic-Indic numerals", () => {
const out = formatDate("2026-12-03T10:00:00Z", "en");
expect(out).toMatch(/^Dec 3, 2026$/);
expect(out).not.toMatch(/[\u0660-\u0669]/);
});
it("Arabic site language uses ar-EG (site language, not device)", () => {
const out = formatDate("2026-12-03T10:00:00Z", "ar");
expect(out.length).toBeGreaterThan(0);
expect(out).toContain("ديسمبر");
expect(out).toContain("٢٠٢٦");
});
it("invalid input renders the em-dash placeholder", () => {
expect(formatDate("not-a-date", "en")).toBe("—");
expect(formatDate("", "ar")).toBe("—");
});
});
describe("formatDateTime", () => {
it("English site language renders a deterministic en-US stamp", () => {
const out = formatDateTime("2026-12-03T16:25:00Z", "en");
expect(out).toMatch(/Dec 3, 2026/);
expect(out).toMatch(/\d{1,2}:\d{2}/);
expect(out).not.toMatch(/[\u0660-\u0669]/);
});
it("invalid input renders the em-dash placeholder", () => {
expect(formatDateTime("nope", "ar")).toBe("—");
});
});