import { afterEach, describe, expect, it } from "vitest"; import { mount, flushPromises } from "@vue/test-utils"; import { createSSRApp, defineComponent, ref } from "vue"; import { renderToString } from "@vue/server-renderer"; import { CalendarDate, CalendarDateTime, parseZonedDateTime, PersianCalendar, toCalendar, } from "@internationalized/date"; import { DateField, DateFieldGroup, DateFieldInput, DateFieldSegment, DateFieldPrefix, Label, Description, FieldError, Fieldset, Form, ConfigProvider, } from "../packages/vue/src"; const components = { DateField, DateFieldGroup, DateFieldInput, DateFieldSegment, DateFieldPrefix, Label, Description, FieldError, Fieldset, Form, ConfigProvider, }; const content = 'Help'; const initial = new CalendarDate(2024, 2, 10); const wrappers: ReturnType[] = []; afterEach(() => { wrappers.splice(0).forEach((w) => w.unmount()); document.body.innerHTML = ""; }); function fixture(template: string, setup = () => ({})) { const w = mount(defineComponent({ components, template, setup }), { attachTo: document.body, }); wrappers.push(w); return w; } const part = (w: ReturnType, type = "day") => w.get(`[data-type="${type}"]`); const values = (w: ReturnType) => new FormData(w.get("form").element as HTMLFormElement).getAll("date"); describe("DateField", () => { it("renders separators and shares field state/name/description on the actual group", async () => { const w = fixture( `
${content}
`, () => ({ initial }), ); expect(w.findAll('[data-type="literal"]')).toHaveLength(2); const root = w.get('[data-slot="date-field"]'); expect(root.attributes("aria-labelledby")).toContain( w.get('[data-slot="label"]').attributes("id"), ); expect(root.attributes("aria-describedby")).toContain( w.get('[data-slot="description"]').attributes("id"), ); expect(values(w)).toEqual(["2024-02-10"]); await part(w).trigger("keydown", { key: "ArrowUp" }); await flushPromises(); expect(values(w)).toEqual(["2024-02-11"]); expect(part(w).text()).toBe("11"); }); it("restores rejected nonempty and empty completed proposals with stable DOM/focus", async () => { const value = ref(null); const w = fixture( `
${content}
`, () => ({ initial, value }), ); const el = part(w, "year").element as HTMLElement; el.focus(); for (const type of ["month", "day", "year"]) await part(w, type).trigger("keydown", { key: "ArrowUp" }); await flushPromises(); expect(part(w, "year").element).toBe(el); expect(document.activeElement).toBe(el); expect(part(w).attributes("data-placeholder")).toBe("true"); expect(values(w)).toEqual([""]); const field = w.getComponent(DateField); value.value = initial; await flushPromises(); await part(w).trigger("keydown", { key: "ArrowUp" }); await flushPromises(); expect(part(w).text()).toBe("10"); expect(values(w)).toEqual(["2024-02-10"]); }); it("keeps time precision and the named zone at day granularity", async () => { const value = ref( parseZonedDateTime("2024-03-10T01:30:45.123-08:00[America/Los_Angeles]"), ); const w = fixture( `
${content}
`, () => ({ value }), ); expect(w.find('[data-type="hour"]').exists()).toBe(false); await part(w).trigger("keydown", { key: "ArrowUp" }); await flushPromises(); expect(value.value.timeZone).toBe("America/Los_Angeles"); expect(value.value.second).toBe(45); expect(value.value.millisecond).toBe(123); expect(values(w)).toEqual([value.value.toString()]); }); it("preserves the model calendar while editing in the locale calendar", async () => { const value = ref(toCalendar(initial, new PersianCalendar())); const w = fixture( `
${content}
`, () => ({ value }), ); expect(part(w, "year").text()).toBe("2024"); await part(w).trigger("keydown", { key: "ArrowUp" }); await flushPromises(); expect(value.value.calendar.identifier).toBe("persian"); expect(values(w)).toEqual(["2024-02-11"]); }); it("uses provider locale and updates segment order", async () => { const locale = ref("en-US"); const w = fixture( `${content}`, () => ({ locale, initial }), ); expect( w .findAll('[data-slot="date-input-group-segment"]')[0] .attributes("data-type"), ).toBe("month"); locale.value = "zh-CN"; await flushPromises(); expect( w .findAll('[data-slot="date-input-group-segment"]')[0] .attributes("data-type"), ).toBe("year"); }); it("resets values and partial drafts; respects cancelled reset", async () => { const w = fixture( `
${content}
`, () => ({ initial }), ); await part(w).trigger("keydown", { key: "ArrowUp" }); await flushPromises(); (w.get("form").element as HTMLFormElement).reset(); await flushPromises(); expect(values(w)).toEqual(["2024-02-10"]); expect(part(w).text()).toBe("10"); }); it("validates required, range and custom constraints with FieldError and native invalid focus", async () => { const w = fixture( `
${content}
`, () => ({ initial }), ); const input = w.get(".hv-choice-validation").element as HTMLInputElement; expect(input.checkValidity()).toBe(false); await flushPromises(); expect(w.get('[data-slot="date-field"]').attributes("aria-invalid")).toBe( "true", ); expect(w.get('[data-slot="field-error"]').text()).toContain("请填写"); expect(document.activeElement).toBe(part(w, "month").element); }); it("supports aria validation and server errors with accepted edit dismissal", async () => { const errors = ref({ date: "Server error" }); const w = fixture( `
${content}
`, () => ({ initial, errors }), ); expect(w.get('[data-slot="field-error"]').text()).toContain("Server error"); expect( ( w.get(".hv-choice-validation").element as HTMLInputElement ).checkValidity(), ).toBe(true); await part(w).trigger("keydown", { key: "ArrowUp" }); await flushPromises(); expect(w.find('[data-slot="field-error"]').exists()).toBe(false); }); it("readonly submits and fieldset disabled omits; neither edits", async () => { const w = fixture( `
${content}
`, () => ({ initial }), ); expect(values(w)).toEqual([]); expect(part(w).attributes("contenteditable")).toBe("false"); await part(w).trigger("keydown", { key: "ArrowUp" }); expect(w.getComponent(DateField).emitted("change")).toBeUndefined(); }); it("honors segment cancellation and explicit stop before the base navigation", async () => { const w = fixture( `
`, () => ({ initial }), ); const el = part(w).element as HTMLElement; el.focus(); await part(w).trigger("keydown", { key: "ArrowUp" }); await flushPromises(); expect(values(w)).toEqual(["2024-02-10"]); part(w).element.dispatchEvent( new KeyboardEvent("keydown", { key: "ArrowRight", bubbles: true, cancelable: true, }), ); expect(document.activeElement).toBe(el); }); it("updates granularity/hourCycle without dropping precision, supports leading zeros", async () => { const value = ref(new CalendarDateTime(2024, 2, 3, 13, 4, 5)); const granularity = ref<"day" | "second">("day"); const cycle = ref<12 | 24>(12); const w = fixture( `
${content}
`, () => ({ value, granularity, cycle }), ); expect(part(w).text()).toBe("03"); granularity.value = "second"; cycle.value = 24; await flushPromises(); expect(part(w, "hour").text()).toBe("13"); expect(part(w, "second").text()).toBe("05"); expect(values(w)).toEqual(["2024-02-03T13:04:05"]); }); it("hydrates explicit date content with unique IDs and stable nodes", async () => { const component = defineComponent({ components, setup: () => ({ initial }), template: `${content}`, }); const host = document.createElement("div"); host.innerHTML = await renderToString(createSSRApp(component)); document.body.append(host); const before = host.querySelector('[data-type="day"]'); const warnings: string[] = []; const app = createSSRApp(component); app.config.warnHandler = (message) => warnings.push(message); app.mount(host); await flushPromises(); try { expect(warnings).toEqual([]); expect(host.querySelector('[data-type="day"]')).toBe(before); } finally { app.unmount(); } }); }); it("applies min/max and unavailable constraints to the shared native validator", async () => { const w = fixture( `
${content}
`, () => ({ initial }), ); expect(w.get('[data-slot="field-error"]').text()).toContain("不能早于"); await part(w).trigger("keydown", { key: "ArrowUp" }); await flushPromises(); expect(w.find('[data-slot="field-error"]').exists()).toBe(false); }); it("keeps read-only submission and blocks edits without invalid constraints", async () => { const w = fixture( `
${content}
`, () => ({ initial }), ); expect(values(w)).toEqual(["2024-02-10"]); await part(w).trigger("keydown", { key: "ArrowUp" }); expect(w.getComponent(DateField).emitted("change")).toBeUndefined(); expect(part(w).attributes("contenteditable")).toBe("false"); expect( (w.get(".hv-choice-validation").element as HTMLInputElement).disabled, ).toBe(true); }); it("uses value alias, gives explicit modelValue priority and accepts explicit undefined empty", () => { const value = ref(undefined); const w = fixture( `
${content}
`, () => ({ value, initial }), ); expect(values(w)).toEqual([""]); expect(part(w).attributes("data-placeholder")).toBe("true"); }); it("preserves accepted controlled values on reset and honors prevented reset", async () => { const value = ref(initial); const w = fixture( `
${content}
`, () => ({ value, initial }), ); await part(w).trigger("keydown", { key: "ArrowUp" }); await flushPromises(); (w.get("form").element as HTMLFormElement).reset(); await flushPromises(); expect(part(w).text()).toBe("11"); expect(values(w)).toEqual(["2024-02-11"]); }); it("uses one externally associated ISO value and restores after native autofill rejection", async () => { const w = fixture( `
${content}
`, () => ({ initial }), ); const input = w.get('[data-slot="date-field-value"]'); await input.setValue("2024-02-12"); await flushPromises(); expect(values(w)).toEqual(["2024-02-10"]); expect((input.element as HTMLInputElement).value).toBe("2024-02-10"); expect(w.getComponent(DateField).emitted("change")).toHaveLength(1); }); it("keeps IME composition keys out of the accepted model", async () => { const w = fixture( `${content}`, () => ({ initial }), ); part(w).element.dispatchEvent( new KeyboardEvent("keydown", { key: "2", isComposing: true, bubbles: true, cancelable: true, }), ); await flushPromises(); expect(values(w)).toEqual(["2024-02-10"]); expect(part(w).text()).toBe("10"); });