mirror of
https://github.com/jakejarvis/sofa.git
synced 2026-10-16 07:25:44 -04:00
fix(i18n): format date-only values in UTC and share an age helper
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
4 files changed
+68
-25
No files matched your search
@@ -1,10 +1,11 @@
|
||||
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
|
||||
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, test, vi } from "vitest";
|
||||
|
||||
vi.mock("../src/index", () => ({
|
||||
i18n: { locale: "en" },
|
||||
}));
|
||||
|
||||
import {
|
||||
calculateAge,
|
||||
formatBytes,
|
||||
formatDate,
|
||||
formatList,
|
||||
@@ -161,3 +162,37 @@ describe("formatList", () => {
|
||||
expect(formatList(["ZIP"], { type: "disjunction" })).toBe("ZIP");
|
||||
});
|
||||
});
|
||||
|
||||
describe("date-only values west of UTC", () => {
|
||||
let prevTz: string | undefined;
|
||||
|
||||
beforeAll(() => {
|
||||
prevTz = process.env.TZ;
|
||||
process.env.TZ = "America/Los_Angeles";
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
if (prevTz === undefined) delete process.env.TZ;
|
||||
else process.env.TZ = prevTz;
|
||||
});
|
||||
|
||||
test("formatShortDate keeps the calendar day", () => {
|
||||
const result = formatShortDate("2024-01-03");
|
||||
expect(result).toContain("3");
|
||||
expect(result).toContain("Jan");
|
||||
expect(result).not.toContain("Jan 2");
|
||||
});
|
||||
|
||||
test("calculateAge does not increment early", () => {
|
||||
expect(calculateAge("1990-05-15", null, new Date(2024, 4, 14, 20, 0))).toBe(33);
|
||||
expect(calculateAge("1990-05-15", null, new Date(2024, 4, 15, 9, 0))).toBe(34);
|
||||
});
|
||||
|
||||
test("calculateAge uses deathday when present", () => {
|
||||
expect(calculateAge("1950-03-10", "2000-03-09")).toBe(49);
|
||||
});
|
||||
|
||||
test("calculateAge returns null for malformed input", () => {
|
||||
expect(calculateAge("unknown")).toBeNull();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user