Files
heroui-vue/tests/calendar-view.test.ts
2026-10-24 20:59:19 +08:00

103 lines
3.3 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
CalendarDate,
HebrewCalendar,
PersianCalendar,
toCalendar,
} from "@internationalized/date";
import {
calendarDuration,
calendarRange,
calendarRows,
calendarWeekdays,
calendarHeading,
calendarYears,
} from "../packages/vue/src/components/date/calendar";
const date = new CalendarDate(2024, 2, 10);
const options = { locale: "en-US" };
describe("calendar view adapter", () => {
it("renders leap-month rows and offset grids with real calendar arithmetic", () => {
const view = calendarRows(date, options);
expect(view.rows.flat().some((d) => d?.toString() === "2024-02-29")).toBe(
true,
);
expect(view.rows).toHaveLength(5);
expect(
calendarRows(date, { ...options, weeksInMonth: 6 }).rows,
).toHaveLength(6);
expect(calendarRows(date, options, { months: 1 }).month.toString()).toBe(
"2024-03-01",
);
});
it("generates true week views across month boundaries", () => {
const view = calendarRows(new CalendarDate(2024, 2, 29), {
...options,
visibleDuration: { weeks: 2 },
firstDayOfWeek: "mon",
});
expect(view.rows).toHaveLength(2);
expect(view.range.start.toString()).toBe("2024-02-26");
expect(view.range.end.toString()).toBe("2024-03-10");
});
it("keeps short day views compact and long day views aligned with empty trailing cells", () => {
const short = calendarRows(date, {
...options,
visibleDuration: { days: 3 },
});
expect(short.rows[0]!.map((d) => d?.day ?? null)).toEqual([
10,
11,
12,
null,
null,
null,
null,
]);
const long = calendarRows(date, {
...options,
visibleDuration: { days: 10 },
});
expect(long.rows).toHaveLength(3);
expect(long.rows.at(-1)!.filter(Boolean)).toHaveLength(2);
expect(
calendarWeekdays(date, { ...options, visibleDuration: { days: 3 } }),
).toEqual(["Sat", "Sun", "Mon"]);
});
it("converts the view to the locale calendar without mutating a Gregorian value", () => {
const view = calendarRange(date, { locale: "fa-IR-u-ca-persian" });
expect(view.start.calendar.identifier).toBe("persian");
expect(view.start.month).toBe(
toCalendar(date, new PersianCalendar()).month,
);
expect(date.calendar.identifier).toBe("gregory");
});
it("includes 13-month calendar dates and calendar-aware year entries", () => {
const hebrew = toCalendar(
new CalendarDate(2024, 9, 10),
new HebrewCalendar(),
);
expect(hebrew.calendar.getMonthsInYear(hebrew)).toBe(13);
const view = calendarRows(hebrew.set({ month: 13 }), {
locale: "he-IL-u-ca-hebrew",
});
expect(view.month.month).toBe(13);
const years = calendarYears(hebrew, hebrew.add({ years: 2 }));
expect(years).toHaveLength(3);
expect(years.every((y) => y.month === 1 && y.day === 1)).toBe(true);
});
it("formats ranges and rejects ambiguous or invalid durations", () => {
expect(calendarHeading(date, options)).toBe("February 2024");
expect(
calendarHeading(date, { ...options, visibleDuration: { months: 2 } }),
).toContain("March");
for (const value of [
{ days: 0 },
{ days: -1 },
{ months: 1.5 },
{ weeks: 1, days: 1 },
{},
])
expect(() => calendarDuration(value as never)).toThrow(RangeError);
});
});