Files
heroui-vue/tests/date-foundation.test.ts
T
2026-10-23 23:56:18 +08:00

228 lines
9.1 KiB
TypeScript

import { afterEach, describe, it, expect } from "vitest";
import { mount, flushPromises } from "@vue/test-utils";
import { defineComponent, ref } from "vue";
import {
CalendarDate,
CalendarDateTime,
parseZonedDateTime,
toCalendar,
PersianCalendar,
} from "@internationalized/date";
import DateFieldFoundation from "../packages/vue/tests/fixtures/DateFieldFoundation.vue";
import CalendarFoundation from "../packages/vue/tests/fixtures/CalendarFoundation.vue";
const wrappers: ReturnType<typeof mount>[] = [];
afterEach(async () => {
wrappers.splice(0).forEach((w) => w.unmount());
await flushPromises();
document.body.innerHTML = "";
});
function field(template: string, setup = () => ({})) {
const w = mount(
defineComponent({ components: { DateFieldFoundation }, template, setup }),
{ attachTo: document.body },
);
wrappers.push(w);
return w;
}
const initial = new CalendarDate(2024, 2, 10);
const segment = (w: ReturnType<typeof mount>, part: string) =>
w.get<HTMLElement>(`[data-reka-date-field-segment="${part}"]`);
const day = (w: ReturnType<typeof mount>, value: string) =>
w.get<HTMLElement>(
`[data-reka-calendar-cell-trigger][data-value="${value}"]:not([data-outside-view])`,
);
function calendar(props: Record<string, unknown> = {}) {
const w = mount(CalendarFoundation, {
props: { defaultPlaceholder: initial, ...props },
attachTo: document.body,
});
wrappers.push(w);
return w;
}
describe("installed Reka DateField capability probes (not public acceptance)", () => {
it("edits a day while retaining CalendarDate type and native date serialization", async () => {
const w = field(
'<form><DateFieldFoundation :default-value="initial" name="date"/></form>',
() => ({ initial }),
);
await segment(w, "day").trigger("keydown", { key: "ArrowUp" });
await flushPromises();
const emitted = w
.getComponent(DateFieldFoundation)
.emitted("update:modelValue")!
.at(-1)![0] as CalendarDate;
expect(emitted).toBeInstanceOf(CalendarDate);
expect(emitted.toString()).toBe("2024-02-11");
expect(
new FormData(w.get("form").element as HTMLFormElement).get("date"),
).toBe("2024-02-11");
});
it("documents that raw DateField reset does not restore displayed segments", async () => {
const w = field(
'<form><DateFieldFoundation :default-value="initial" name="date"/></form>',
() => ({ initial }),
);
await segment(w, "day").trigger("keydown", { key: "ArrowUp" });
(w.get("form").element as HTMLFormElement).reset();
await flushPromises();
expect(segment(w, "day").text()).toBe("11");
});
it("documents controlled rejection leaving optimistic segment text", async () => {
const w = field('<DateFieldFoundation :model-value="initial"/>', () => ({
initial,
}));
await segment(w, "day").trigger("keydown", { key: "ArrowUp" });
await flushPromises();
expect(segment(w, "day").text()).toBe("11");
expect(w.get("input").element.value).toBe("2024-02-10");
});
it("retains seconds in the emitted datetime but truncates native minute submission", async () => {
const value = new CalendarDateTime(2024, 2, 10, 13, 14, 45, 123);
const w = field(
'<form><DateFieldFoundation :default-value="value" granularity="minute" name="date"/></form>',
() => ({ value }),
);
await segment(w, "day").trigger("keydown", { key: "ArrowUp" });
const next = w
.getComponent(DateFieldFoundation)
.emitted("update:modelValue")!
.at(-1)![0] as CalendarDateTime;
expect(next.second).toBe(45);
expect(next.millisecond).toBe(123);
expect(
new FormData(w.get("form").element as HTMLFormElement).get("date"),
).toBe("2024-02-11T13:14");
});
it("documents native datetime-local losing zone metadata", () => {
const value = parseZonedDateTime(
"2024-03-10T01:30-08:00[America/Los_Angeles]",
);
const w = field(
'<DateFieldFoundation :default-value="value" name="date"/>',
() => ({ value }),
);
expect(w.get("input").element.value).toBe("2024-03-10T01:30");
expect(w.text()).toContain("PST");
});
it("documents alternate-calendar native fields not using ISO Gregorian serialization", () => {
const value = toCalendar(initial, new PersianCalendar());
const w = field(
'<DateFieldFoundation :default-value="value" locale="fa-IR-u-ca-persian" name="date"/>',
() => ({ value }),
);
expect(w.get("input").element.value).not.toBe(value.toString());
});
it("supports native required and disabled omission but readonly remains submitted", () => {
const required = field(
'<form><DateFieldFoundation required name="date" :default-placeholder="initial"/></form>',
() => ({ initial }),
);
expect(
required.get<HTMLInputElement>("input").element.checkValidity(),
).toBe(false);
const readonly = field(
'<form><DateFieldFoundation readonly name="date" :default-value="initial"/></form>',
() => ({ initial }),
);
expect(segment(readonly, "day").attributes("contenteditable")).toBe(
"false",
);
expect(
new FormData(readonly.get("form").element as HTMLFormElement).get("date"),
).toBe("2024-02-10");
const disabled = field(
'<form><DateFieldFoundation disabled name="date" :default-value="initial"/></form>',
() => ({ initial }),
);
expect(
new FormData(disabled.get("form").element as HTMLFormElement).has("date"),
).toBe(false);
});
it("updates segment ordering and content when locale changes", async () => {
const locale = ref("en-US");
const w = field(
'<DateFieldFoundation :locale="locale" :default-value="initial"/>',
() => ({ locale, initial }),
);
const parts = () =>
w
.findAll('[role="spinbutton"]')
.map((e) => e.attributes("data-reka-date-field-segment"));
expect(parts()).toEqual(["month", "day", "year"]);
locale.value = "zh-CN";
await flushPromises();
expect(parts()).toEqual(["year", "month", "day"]);
});
});
describe("installed Reka Calendar capability probes (not public acceptance)", () => {
it("renders leap-month dates, skips disabled days and focuses unavailable days", async () => {
const w = calendar({
defaultValue: initial,
isDateDisabled: (value: CalendarDate) => value.day === 11,
isDateUnavailable: (value: CalendarDate) => value.day === 12,
});
expect(day(w, "2024-02-29").element.textContent).toContain("29");
day(w, "2024-02-10").element.focus();
await day(w, "2024-02-10").trigger("keydown", {
key: "ArrowRight",
code: "ArrowRight",
});
await flushPromises();
expect(document.activeElement).toBe(day(w, "2024-02-12").element);
await day(w, "2024-02-12").trigger("click");
expect(w.emitted("update:modelValue")).toBeUndefined();
});
it("keeps a controlled rejected selection visually accepted only by the parent", async () => {
const w = calendar({ modelValue: initial });
await day(w, "2024-02-13").trigger("click");
await day(w, "2024-02-13").trigger("click");
expect(w.emitted("update:modelValue")).toHaveLength(2);
expect(day(w, "2024-02-10").attributes("data-selected")).toBe("true");
expect(day(w, "2024-02-13").attributes("data-selected")).toBeUndefined();
});
it("supports multiple dates at runtime despite the scalar emit declaration", async () => {
const w = calendar({ multiple: true, modelValue: [initial] });
await day(w, "2024-02-13").trigger("click");
const next = w.emitted("update:modelValue")!.at(-1)![0] as CalendarDate[];
expect(Array.isArray(next)).toBe(true);
expect(next.map((v) => v.toString())).toEqual(["2024-02-10", "2024-02-13"]);
});
it("respects min/max and allows readonly navigation without selection", async () => {
const w = calendar({
readonly: true,
minValue: initial,
maxValue: initial.add({ days: 5 }),
});
expect(day(w, "2024-02-09").attributes("data-disabled")).toBeDefined();
await day(w, "2024-02-13").trigger("click");
expect(w.emitted("update:modelValue")).toBeUndefined();
});
it("documents cancellation happening after the internal keyboard focus movement", async () => {
const w = calendar({
defaultValue: initial,
onKeydown: (event: KeyboardEvent) => event.preventDefault(),
});
day(w, "2024-02-10").element.focus();
await day(w, "2024-02-10").trigger("keydown", {
key: "ArrowRight",
code: "ArrowRight",
});
await flushPromises();
expect(document.activeElement).toBe(day(w, "2024-02-11").element);
});
it("supports two month grids and fixed six-week views", () => {
const w = calendar({ numberOfMonths: 2, fixedWeeks: true });
expect(w.findAll("table")).toHaveLength(2);
expect(w.findAll("tbody tr")).toHaveLength(12);
});
});
it("records that an explicit Gregorian placeholder keeps a Gregorian month grid under an Indian locale", () => {
const w = calendar({ defaultValue: initial, locale: "hi-IN-u-ca-indian" });
const current = w.findAll(
"[data-reka-calendar-cell-trigger]:not([data-outside-view])",
);
expect(current[0].attributes("data-value")).toBe("2024-02-01");
expect(current).toHaveLength(29);
});