Files
Reactive-Resume/packages/utils/src/date.test.ts
T
Amruth Pillai 6a01207b6b test: add unit and component tests across the monorepo
Adds ~1000 tests to bring the previously-untested packages and apps
under coverage:

- packages/utils — string, color, date, file, level, locale, sanitize,
  field, html, network-icons, rate-limit, url, url-security, monorepo,
  resume/patch, resume/docx/link-utils, style helpers (~97% on the
  testable utility files)
- packages/ui — 28 component test files plus the use-controlled-state,
  use-mobile, use-confirm, use-prompt hooks (95% statements)
- packages/pdf — shared template helpers (filtering, columns, picture,
  metrics, section-links, page-size, rich-text-html, section-title)
- packages/schema — resumeDataSchema, page, templates, default
- packages/fonts — expanded coverage on font helpers
- packages/ai — resume sanitize and extraction template
- packages/api — resume-access-policy
- apps/web — error-message, locale, theme, pwa, dialogs/store, and
  the resume/move-item / section-actions / make-section-item helpers

Adds jsdom polyfills (ResizeObserver, IntersectionObserver,
scrollIntoView, matchMedia) and an explicit React Testing Library
cleanup hook to vitest.setup.ts so portal- and overlay-based components
work without per-test setup.
2026-05-10 20:00:07 +02:00

90 lines
2.6 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { formatDate, formatPeriod, formatSingleDate } from "./date";
describe("formatDate", () => {
it("formats YYYY-MM as 'Month Year'", () => {
expect(formatDate("2024-03")).toBe("March 2024");
});
it("formats YYYY-MM-DD as 'Month Year' by default (day excluded)", () => {
expect(formatDate("2024-03-15")).toBe("March 2024");
});
it("formats YYYY-MM-DD with includeDay=true as 'Month DD, Year'", () => {
expect(formatDate("2024-03-15", true)).toBe("March 15, 2024");
});
it("returns YYYY unchanged when only year provided", () => {
expect(formatDate("2024")).toBe("2024");
});
it("handles January (month 1)", () => {
expect(formatDate("2024-01")).toBe("January 2024");
});
it("handles December (month 12)", () => {
expect(formatDate("2024-12")).toBe("December 2024");
});
it("includes day even with single-digit days", () => {
expect(formatDate("2024-03-05", true)).toBe("March 05, 2024");
});
it("returns 'undefined' month name when out-of-range month is supplied", () => {
// Defensive: behavior documents what happens with bad input
expect(formatDate("2024-13")).toBe("undefined 2024");
});
});
describe("formatPeriod", () => {
it("returns empty string when both dates missing", () => {
expect(formatPeriod()).toBe("");
});
it("returns end date alone when start is missing", () => {
expect(formatPeriod(undefined, "2024-05")).toBe("2024-05");
});
it("returns start - Present when end is missing", () => {
expect(formatPeriod("2024-01")).toBe("January 2024 - Present");
});
it("formats both ends when both provided", () => {
expect(formatPeriod("2020-06", "2024-03")).toBe("June 2020 - March 2024");
});
it("treats empty string start same as undefined", () => {
expect(formatPeriod("", "2024-05")).toBe("2024-05");
});
it("treats empty string end same as undefined", () => {
expect(formatPeriod("2024-01", "")).toBe("January 2024 - Present");
});
it("returns empty when start empty and end empty", () => {
expect(formatPeriod("", "")).toBe("");
});
});
describe("formatSingleDate", () => {
it("returns empty string when date is undefined", () => {
expect(formatSingleDate()).toBe("");
});
it("returns empty string when date is empty string", () => {
expect(formatSingleDate("")).toBe("");
});
it("formats full date with day included", () => {
expect(formatSingleDate("2024-03-15")).toBe("March 15, 2024");
});
it("formats year-month even with includeDay flag implied", () => {
expect(formatSingleDate("2024-03")).toBe("March 2024");
});
it("returns plain year when only year present", () => {
expect(formatSingleDate("2024")).toBe("2024");
});
});