From 460a12e6950910cc30bef6aef3634fd523a51d63 Mon Sep 17 00:00:00 2001 From: Horizontal42 <75383519+Horizontal42@users.noreply.github.com> Date: Sun, 9 Aug 2026 15:57:15 +0300 Subject: [PATCH] improve date order detection tests with real Intl calls --- src/engine/datetime.test.ts | 34 ++++------------------------------ src/engine/datetime.ts | 4 ++-- 2 files changed, 6 insertions(+), 32 deletions(-) diff --git a/src/engine/datetime.test.ts b/src/engine/datetime.test.ts index c327c52..ee10c62 100644 --- a/src/engine/datetime.test.ts +++ b/src/engine/datetime.test.ts @@ -13,38 +13,12 @@ describe("datetime", () => { vi.restoreAllMocks(); }); - it("should detect dmy", () => { - const mockFormatToParts = vi.fn().mockReturnValue([ - { type: "day", value: "31" }, - { type: "literal", value: "/" }, - { type: "month", value: "12" }, - { type: "literal", value: "/" }, - { type: "year", value: "2000" }, - ]); - - const mockDateTimeFormat = vi.spyOn(Intl, "DateTimeFormat").mockImplementation(() => ({ - formatToParts: mockFormatToParts, - }) as any); - - expect(detectDateOrder()).toBe("dmy"); - mockDateTimeFormat.mockRestore(); + it("should detect dmy (e.g. en-GB)", () => { + expect(detectDateOrder("en-GB")).toBe("dmy"); }); - it("should detect mdy", () => { - const mockFormatToParts = vi.fn().mockReturnValue([ - { type: "month", value: "12" }, - { type: "literal", value: "/" }, - { type: "day", value: "31" }, - { type: "literal", value: "/" }, - { type: "year", value: "2000" }, - ]); - - const mockDateTimeFormat = vi.spyOn(Intl, "DateTimeFormat").mockImplementation(() => ({ - formatToParts: mockFormatToParts, - }) as any); - - expect(detectDateOrder()).toBe("mdy"); - mockDateTimeFormat.mockRestore(); + it("should detect mdy (e.g. en-US)", () => { + expect(detectDateOrder("en-US")).toBe("mdy"); }); it("should default to dmy if neither is found", () => { diff --git a/src/engine/datetime.ts b/src/engine/datetime.ts index 226a331..078d4f4 100644 --- a/src/engine/datetime.ts +++ b/src/engine/datetime.ts @@ -2,8 +2,8 @@ import { DateOrder } from "./types"; /** Does the OS locale write the day or the month first? (31.12 vs 12/31) */ -export function detectDateOrder(): DateOrder { - const parts = new Intl.DateTimeFormat(undefined, { day: "numeric", month: "numeric", year: "numeric" }) +export function detectDateOrder(locale?: string): DateOrder { + const parts = new Intl.DateTimeFormat(locale, { day: "numeric", month: "numeric", year: "numeric" }) .formatToParts(new Date(2000, 11, 31)); for (const p of parts) { if (p.type === "day") return "dmy";