Файли
openidea/src/platform/dateFormat.test.ts
T
Muuтаmuutot 138209c627 feat(vcs): show Today / Yesterday in the annotate gutter with pretty dates (#6110)
The annotate gutter now shows Today / Yesterday when the global date format's "Use pretty formatting" is on, as IDEA's annotate does (FileAnnotation.formatDate -> DateFormatUtil.formatPrettyDate); older dates, and all dates with pretty formatting off, keep the short date. New formatPrettyDay next to formatTimestamp.

Co-authored-by: muutot <[email protected]>
2026-11-27 05:09:25 +08:00

56 рядки
2.7 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
DEFAULT_DATE_FORMAT,
formatClock,
formatDateTime,
formatDay,
formatPattern,
formatPrettyDay,
formatTimestamp,
} from "./dateFormat";
describe("dateFormat", () => {
const d = new Date(2026, 10, 5, 9, 7);
const system = { ...DEFAULT_DATE_FORMAT, overrideSystem: false };
const long = { year: "numeric", month: "short", day: "numeric" } as const;
it("formats like the locale methods it replaces", () => {
expect(formatDay(d, system)).toBe(d.toLocaleDateString(undefined, long));
expect(formatDay(d, system, true)).toBe(
d.toLocaleDateString(undefined, { year: "2-digit", month: "2-digit", day: "2-digit" }),
);
expect(formatClock(d, system)).toBe(d.toLocaleTimeString(undefined, { hour: "2-digit", minute: "2-digit" }));
expect(formatPattern(d, "MMMM MMM dd.MM.yyyy")).toBe("November Nov 05.11.2026");
});
it("keeps the time of dates before yesterday, as IDEA's pretty date-time does", () => {
const now = new Date(2026, 10, 13, 12, 0).getTime();
const pretty = { ...system, pretty: true };
const numeric = d.toLocaleDateString(undefined, { year: "numeric", month: "2-digit", day: "2-digit" });
expect(formatTimestamp(d.getTime(), now, pretty)).toBe(`${numeric} ${formatClock(d, system)}`);
expect(formatTimestamp(d.getTime(), now, { ...system, pretty: false })).toBe(formatDateTime(d, system));
expect(formatTimestamp(new Date(2026, 10, 13, 9, 7).getTime(), now, pretty)).toMatch(/09:07|9:07/);
const custom = { ...pretty, overrideSystem: true, pattern: "dd.MM.yyyy", use24Hour: true };
expect(formatTimestamp(d.getTime(), now, custom)).toBe("05.11.2026 09:07");
});
it("shows Today/Yesterday without a clock time for blame, older dates keep the short date", () => {
const now = new Date(2026, 10, 13, 12, 0).getTime();
const pretty = { ...system, pretty: true };
const older = new Date(2026, 10, 5, 9, 7);
const short = { year: "2-digit", month: "2-digit", day: "2-digit" } as const;
expect(formatPrettyDay(new Date(2026, 10, 13, 9, 7).getTime(), now, pretty)).toBe("Today");
expect(formatPrettyDay(new Date(2026, 10, 12, 23, 30).getTime(), now, pretty)).toBe("Yesterday");
expect(formatPrettyDay(older.getTime(), now, pretty)).toBe(older.toLocaleDateString(undefined, short));
expect(formatPrettyDay(new Date(2026, 10, 13, 9, 7).getTime(), now, { ...system, pretty: false })).toBe(
new Date(2026, 10, 13, 9, 7).toLocaleDateString(undefined, short),
);
});
it("formats another date with the same cached formatter", () => {
const e = new Date(2025, 0, 31);
formatDay(d, system);
expect(formatDay(e, system)).toBe(e.toLocaleDateString(undefined, long));
});
});