291 lines
13 KiB
TypeScript
291 lines
13 KiB
TypeScript
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
import { mount, flushPromises } from "@vue/test-utils";
|
|
import { defineComponent, shallowRef, type Component } from "vue";
|
|
import {
|
|
CalendarDate,
|
|
CalendarDateTime,
|
|
PersianCalendar,
|
|
toCalendar,
|
|
parseZonedDateTime,
|
|
type DateValue,
|
|
} from "@internationalized/date";
|
|
import * as library from "../packages/vue/src/index";
|
|
const wrappers: ReturnType<typeof mount>[] = [];
|
|
afterEach(() => {
|
|
wrappers.splice(0).forEach((w) => w.unmount());
|
|
document.body.innerHTML = "";
|
|
vi.restoreAllMocks();
|
|
});
|
|
const initial = new CalendarDate(2024, 2, 10);
|
|
const parts = `<CalendarHeader><CalendarHeading/><CalendarNavButton slot="previous"/><CalendarNavButton slot="next"/></CalendarHeader><CalendarGrid><CalendarGridHeader/><CalendarGridBody/></CalendarGrid>`;
|
|
function fixture(attributes = "", setup = () => ({}), children = parts) {
|
|
const w = mount(
|
|
defineComponent({
|
|
components: Object.fromEntries(
|
|
Object.entries(library).filter(([name]) => name.startsWith("Calendar")),
|
|
) as Record<string, Component>,
|
|
setup: () => ({ initial, ...setup() }),
|
|
template: `<Calendar aria-label="Date" :default-focused-value="initial" ${attributes}>${children}</Calendar>`,
|
|
}),
|
|
{ attachTo: document.body },
|
|
);
|
|
wrappers.push(w);
|
|
return w;
|
|
}
|
|
const cell = (w: ReturnType<typeof mount>, iso: string) =>
|
|
w.get<HTMLElement>(
|
|
`[data-slot="calendar-cell"][data-date="${iso}"]:not([data-outside-month])`,
|
|
);
|
|
const calendar = (w: ReturnType<typeof mount>) =>
|
|
w.findComponent(library.Calendar);
|
|
async function key(
|
|
w: ReturnType<typeof mount>,
|
|
iso: string,
|
|
key: string,
|
|
extra = {},
|
|
) {
|
|
cell(w, iso).element.focus();
|
|
await cell(w, iso).trigger("keydown", { key, code: key, ...extra });
|
|
await flushPromises();
|
|
}
|
|
describe("Calendar public contract", () => {
|
|
it("renders semantic grids and selects a date without named native submission", async () => {
|
|
const w = fixture();
|
|
expect(w.find('table[role="grid"]').exists()).toBe(true);
|
|
expect(w.findAll("th")).toHaveLength(7);
|
|
await cell(w, "2024-02-29").trigger("click");
|
|
expect(cell(w, "2024-02-29").attributes("data-selected")).toBe("true");
|
|
expect(calendar(w).emitted("change")![0]![0]).toBeInstanceOf(CalendarDate);
|
|
expect(w.find("input[name]").exists()).toBe(false);
|
|
});
|
|
it("keeps controlled rejected values and allows the same proposal again", async () => {
|
|
const w = fixture(':model-value="initial"');
|
|
await cell(w, "2024-02-11").trigger("click");
|
|
await cell(w, "2024-02-11").trigger("click");
|
|
expect(calendar(w).emitted("change")).toHaveLength(2);
|
|
expect(cell(w, "2024-02-10").attributes("data-selected")).toBe("true");
|
|
expect(cell(w, "2024-02-11").attributes("data-selected")).toBeUndefined();
|
|
});
|
|
it("supports multiple toggling and copies proposals without mutating parent dates", async () => {
|
|
const w = fixture('selection-mode="multiple" :default-value="[initial]"');
|
|
await cell(w, "2024-02-11").trigger("click");
|
|
expect(
|
|
(calendar(w).emitted("change")![0]![0] as DateValue[]).map((d) =>
|
|
d.toString(),
|
|
),
|
|
).toEqual(["2024-02-10", "2024-02-11"]);
|
|
await cell(w, "2024-02-10").trigger("click");
|
|
expect(cell(w, "2024-02-10").attributes("data-selected")).toBeUndefined();
|
|
expect(initial.toString()).toBe("2024-02-10");
|
|
});
|
|
it("exposes actual hover, focus and pressed slot state without repeating a held selection", async () => {
|
|
const w = fixture(
|
|
'selection-mode="multiple"',
|
|
() => ({}),
|
|
`<CalendarGrid><CalendarGridHeader/><CalendarGridBody v-slot="{date}"><CalendarCell :date="date" v-slot="{formattedDate,isHovered,isPressed,isFocused}"><span>{{ formattedDate }}:{{ isHovered }}:{{ isPressed }}:{{ isFocused }}</span></CalendarCell></CalendarGridBody></CalendarGrid>`,
|
|
);
|
|
const day = cell(w, "2024-02-10");
|
|
await day.trigger("pointerenter");
|
|
expect(day.text()).toContain(":true:false:false");
|
|
day.element.focus();
|
|
await flushPromises();
|
|
expect(day.text()).toContain(":true:false:true");
|
|
await day.trigger("keydown", { key: " ", code: "Space" });
|
|
expect(day.attributes("data-pressed")).toBe("true");
|
|
await day.trigger("keydown", { key: " ", code: "Space", repeat: true });
|
|
expect(calendar(w).emitted("change")).toHaveLength(1);
|
|
await day.trigger("keyup", { key: " ", code: "Space" });
|
|
expect(day.attributes("data-pressed")).toBeUndefined();
|
|
});
|
|
it("preserves time precision and zone in date-only selection", async () => {
|
|
for (const value of [
|
|
new CalendarDateTime(2024, 2, 10, 13, 14, 45, 123),
|
|
parseZonedDateTime("2024-02-10T13:14:45.123-08:00[America/Los_Angeles]"),
|
|
]) {
|
|
const w = fixture(':default-value="value"', () => ({ value }));
|
|
await cell(w, "2024-02-11").trigger("click");
|
|
const next = calendar(w).emitted("change")![0]![0] as DateValue;
|
|
expect(next.toString()).toBe(value.set({ day: 11 }).toString());
|
|
}
|
|
});
|
|
it("distinguishes disabled from focusable unavailable dates and honors actual keyboard cancellation", async () => {
|
|
const w = fixture(
|
|
':is-date-disabled="d=>d.day===11" :is-date-unavailable="d=>d.day===12"',
|
|
);
|
|
await key(w, "2024-02-10", "ArrowRight");
|
|
expect(document.activeElement).toBe(cell(w, "2024-02-12").element);
|
|
await cell(w, "2024-02-12").trigger("click");
|
|
expect(calendar(w).emitted("change")).toBeUndefined();
|
|
const cancelled = fixture(
|
|
"",
|
|
() => ({}),
|
|
`<CalendarGrid><CalendarGridHeader/><CalendarGridBody v-slot="{date}"><CalendarCell :date="date" @keydown.prevent /></CalendarGridBody></CalendarGrid>`,
|
|
);
|
|
await key(cancelled, "2024-02-10", "ArrowRight");
|
|
expect(document.activeElement).toBe(cell(cancelled, "2024-02-10").element);
|
|
});
|
|
it("supports readonly navigation and fully disabled controls", async () => {
|
|
const ro = fixture("is-read-only");
|
|
await key(ro, "2024-02-10", "ArrowRight");
|
|
expect(document.activeElement).toBe(cell(ro, "2024-02-11").element);
|
|
await cell(ro, "2024-02-11").trigger("click");
|
|
expect(calendar(ro).emitted("change")).toBeUndefined();
|
|
const disabled = fixture("is-disabled");
|
|
expect(disabled.findAll("button:not(:disabled)")).toHaveLength(0);
|
|
});
|
|
it("pages with bounds, RTL keyboard movement and programmatic focused values", async () => {
|
|
const focus = shallowRef<DateValue>(initial);
|
|
const w = fixture(
|
|
'v-model:focused-value="focus" dir="rtl" :min-value="initial" :max-value="initial.add({months:1})"',
|
|
() => ({ focus }),
|
|
);
|
|
await key(w, "2024-02-10", "ArrowLeft");
|
|
expect(focus.value.day).toBe(11);
|
|
await key(w, "2024-02-11", "PageDown");
|
|
expect(w.get('[data-slot="calendar-heading"]').text()).toContain("March");
|
|
focus.value = new CalendarDate(2024, 2, 15);
|
|
await flushPromises();
|
|
expect(w.get('[data-slot="calendar-heading"]').text()).toBe(
|
|
"February 2024",
|
|
);
|
|
});
|
|
it("pages a single unit even when the proposed focus remains inside a multi-unit view", async () => {
|
|
const w = fixture(':visible-duration="{months:2}" page-behavior="single"');
|
|
await w.get('[aria-label="Next"]').trigger("click");
|
|
await flushPromises();
|
|
expect(w.get('[data-slot="calendar-heading"]').text()).toContain("March");
|
|
expect(w.get('[data-slot="calendar-heading"]').text()).toContain("April");
|
|
const rejected = fixture(
|
|
':focused-value="initial" :visible-duration="{weeks:2}" page-behavior="single"',
|
|
);
|
|
const heading = rejected.get('[data-slot="calendar-heading"]').text();
|
|
await rejected.get('[aria-label="Next"]').trigger("click");
|
|
await flushPromises();
|
|
expect(rejected.get('[data-slot="calendar-heading"]').text()).toBe(heading);
|
|
});
|
|
it("keeps one Tab stop when the initial focused day is disabled", () => {
|
|
const w = fixture(':is-date-disabled="d=>d.day===10"');
|
|
expect(w.findAll('[data-slot="calendar-cell"][tabindex="0"]')).toHaveLength(
|
|
1,
|
|
);
|
|
expect(cell(w, "2024-02-01").attributes("tabindex")).toBe("0");
|
|
});
|
|
it("retains keyboard focus across programmatic month changes without stealing outside focus", async () => {
|
|
const focused = shallowRef<DateValue>(initial);
|
|
const w = fixture('v-model:focused-value="focused"', () => ({ focused }));
|
|
cell(w, "2024-02-10").element.focus();
|
|
focused.value = new CalendarDate(2025, 6, 15);
|
|
await flushPromises();
|
|
expect(document.activeElement).toBe(cell(w, "2025-06-15").element);
|
|
const outside = document.createElement("button");
|
|
document.body.append(outside);
|
|
outside.focus();
|
|
focused.value = new CalendarDate(2024, 2, 20);
|
|
await flushPromises();
|
|
expect(document.activeElement).toBe(outside);
|
|
});
|
|
it("honors per-control disabled state and native event cancellation on navigation", async () => {
|
|
const w = fixture(
|
|
"",
|
|
() => ({}),
|
|
`<CalendarHeader><CalendarHeading/><CalendarNavButton disabled slot="next"/><CalendarNavButton slot="previous" @click.prevent/></CalendarHeader><CalendarGrid><CalendarGridHeader/><CalendarGridBody/></CalendarGrid>`,
|
|
);
|
|
expect(w.get('[aria-label="Next"]').attributes("disabled")).toBe("");
|
|
await w.get('[aria-label="Previous"]').trigger("click");
|
|
expect(w.get('[data-slot="calendar-heading"]').text()).toBe(
|
|
"February 2024",
|
|
);
|
|
});
|
|
it("renders true weekly, daily and offset multi-month layouts", () => {
|
|
expect(
|
|
fixture(':visible-duration="{weeks:2}"').findAll("tbody tr"),
|
|
).toHaveLength(2);
|
|
expect(
|
|
fixture(
|
|
':visible-duration="{days:3}" selection-alignment="start"',
|
|
).findAll('[data-slot="calendar-cell"]'),
|
|
).toHaveLength(3);
|
|
const two = fixture(
|
|
':visible-duration="{months:2}"',
|
|
() => ({}),
|
|
`${parts}<CalendarGrid :offset="{months:1}"><CalendarGridHeader/><CalendarGridBody/></CalendarGrid>`,
|
|
);
|
|
expect(two.findAll("table")).toHaveLength(2);
|
|
expect(cell(two, "2024-03-15").element.tagName === "BUTTON").toBe(true);
|
|
expect(two.findAll('[data-date="2024-03-01"]:not(:disabled)')).toHaveLength(
|
|
1,
|
|
);
|
|
});
|
|
it("keeps empty selection Gregorian independently of the focused view calendar", async () => {
|
|
const persian = toCalendar(initial, new PersianCalendar());
|
|
const w = fixture(
|
|
':focused-value="persian" locale="fa-IR-u-ca-persian"',
|
|
() => ({ persian }),
|
|
);
|
|
await cell(w, "2024-02-11").trigger("click");
|
|
const value = calendar(w).emitted("change")![0]![0] as DateValue;
|
|
expect(value.calendar.identifier).toBe("gregory");
|
|
expect(value.toString()).toBe("2024-02-11");
|
|
});
|
|
it("restores accepted DOM focus after a controlled pointer focus is rejected", async () => {
|
|
const w = fixture(':focused-value="initial"');
|
|
cell(w, "2024-02-11").element.focus();
|
|
await flushPromises();
|
|
expect(document.activeElement).toBe(cell(w, "2024-02-10").element);
|
|
});
|
|
it("translates the actual calendar view and selection back to the public Gregorian model", async () => {
|
|
const w = fixture(':default-value="initial" locale="hi-IN-u-ca-indian"');
|
|
expect(w.get('[data-slot="calendar-heading"]').text()).not.toContain(
|
|
"February",
|
|
);
|
|
await cell(w, "2024-02-11").trigger("click");
|
|
const next = calendar(w).emitted("change")![0]![0] as DateValue;
|
|
expect(next.calendar.identifier).toBe("gregory");
|
|
expect(next.toString()).toBe("2024-02-11");
|
|
});
|
|
});
|
|
describe("CalendarYearPicker", () => {
|
|
const children = `<CalendarHeader><CalendarYearPickerTrigger><CalendarYearPickerTriggerHeading/><CalendarYearPickerTriggerIndicator/></CalendarYearPickerTrigger><CalendarNavButton slot="previous"/><CalendarNavButton slot="next"/></CalendarHeader><CalendarGrid><CalendarGridHeader/><CalendarGridBody/></CalendarGrid><CalendarYearPickerGrid><CalendarYearPickerGridBody/></CalendarYearPickerGrid>`;
|
|
it("keeps hidden years out of Tab, focuses the selected year and escapes back to the trigger", async () => {
|
|
const w = fixture("", () => ({}), children);
|
|
expect(
|
|
w.get('[data-slot="calendar-year-picker-grid"]').attributes("inert"),
|
|
).toBe("");
|
|
await w.get('[data-slot="calendar-year-picker-trigger"]').trigger("click");
|
|
await flushPromises();
|
|
expect(document.activeElement?.getAttribute("data-year")).toBe("2024");
|
|
await w.get('[data-year="2024"]').trigger("keydown", { key: "ArrowDown" });
|
|
expect(document.activeElement?.getAttribute("data-year")).toBe("2027");
|
|
await w.get('[data-year="2027"]').trigger("keydown", { key: "Escape" });
|
|
await flushPromises();
|
|
expect(document.activeElement).toBe(
|
|
w.get('[data-slot="calendar-year-picker-trigger"]').element,
|
|
);
|
|
});
|
|
it("selects a year without changing the selected day value, and respects close rejection", async () => {
|
|
const w = fixture(':default-value="initial"', () => ({}), children);
|
|
await w.get('[data-slot="calendar-year-picker-trigger"]').trigger("click");
|
|
await flushPromises();
|
|
await w.get('[data-year="2026"]').trigger("click");
|
|
await flushPromises();
|
|
expect(w.find('[data-slot="calendar-heading"]').exists()).toBe(false);
|
|
expect(
|
|
w.get('[data-slot="calendar-year-picker-trigger-heading"]').text(),
|
|
).toContain("2026");
|
|
expect(calendar(w).emitted("change")).toBeUndefined();
|
|
const rejected = fixture(
|
|
':is-year-picker-open="true"',
|
|
() => ({}),
|
|
children,
|
|
);
|
|
await flushPromises();
|
|
await rejected.get('[data-year="2026"]').trigger("click");
|
|
await flushPromises();
|
|
expect(
|
|
rejected
|
|
.get('[data-slot="calendar-year-picker-grid"]')
|
|
.attributes("data-open"),
|
|
).toBe("true");
|
|
});
|
|
});
|