mirror of
https://github.com/Horizontal42/summarum.git
synced 2026-11-11 01:02:01 +03:00
test: improve detectDateOrder testing with real locales
Refactored `detectDateOrder` in `src/engine/datetime.ts` to accept an optional `locale` argument. This allows `src/engine/datetime.test.ts` to directly verify actual Date-Month-Year and Month-Day-Year ordering using real native `Intl.DateTimeFormat` outputs for "en-GB" and "en-US", rather than faking the API output with spies. This vastly improves the realism and robustness of these tests. Co-authored-by: Horizontal42 <75383519+Horizontal42@users.noreply.github.com>
This commit is contained in:
parent
c958f30662
commit
6c61e6058e
2 changed files with 6 additions and 32 deletions
|
|
@ -13,38 +13,12 @@ describe("datetime", () => {
|
||||||
vi.restoreAllMocks();
|
vi.restoreAllMocks();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should detect dmy", () => {
|
it("should detect dmy (e.g. en-GB)", () => {
|
||||||
const mockFormatToParts = vi.fn().mockReturnValue([
|
expect(detectDateOrder("en-GB")).toBe("dmy");
|
||||||
{ 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 mdy", () => {
|
it("should detect mdy (e.g. en-US)", () => {
|
||||||
const mockFormatToParts = vi.fn().mockReturnValue([
|
expect(detectDateOrder("en-US")).toBe("mdy");
|
||||||
{ 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 default to dmy if neither is found", () => {
|
it("should default to dmy if neither is found", () => {
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,8 @@
|
||||||
import { DateOrder } from "./types";
|
import { DateOrder } from "./types";
|
||||||
|
|
||||||
/** Does the OS locale write the day or the month first? (31.12 vs 12/31) */
|
/** Does the OS locale write the day or the month first? (31.12 vs 12/31) */
|
||||||
export function detectDateOrder(): DateOrder {
|
export function detectDateOrder(locale?: string): DateOrder {
|
||||||
const parts = new Intl.DateTimeFormat(undefined, { day: "numeric", month: "numeric", year: "numeric" })
|
const parts = new Intl.DateTimeFormat(locale, { day: "numeric", month: "numeric", year: "numeric" })
|
||||||
.formatToParts(new Date(2000, 11, 31));
|
.formatToParts(new Date(2000, 11, 31));
|
||||||
for (const p of parts) {
|
for (const p of parts) {
|
||||||
if (p.type === "day") return "dmy";
|
if (p.type === "day") return "dmy";
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue