import { afterEach, describe, expect, it, vi } from "vitest"; import { flushPromises, mount } from "@vue/test-utils"; import { defineComponent, nextTick, ref } from "vue"; import { ConfigProvider, useOverlayState } from "../packages/vue/src"; import OverlayDialogRoot from "../packages/vue/src/components/overlay/OverlayDialogRoot.vue"; import OverlayDialogContent from "../packages/vue/src/components/overlay/OverlayDialogContent.vue"; import OverlayPortal from "../packages/vue/src/components/overlay/OverlayPortal.vue"; import OverlayBackdrop from "../packages/vue/src/components/overlay/OverlayBackdrop.vue"; import { DialogTrigger, DialogClose, DialogTitle, DialogDescription, } from "../packages/vue/src/components/overlay/primitives"; const wrappers: ReturnType[] = []; afterEach(async () => { wrappers.splice(0).forEach((w) => w.unmount()); await flushPromises(); document.body.innerHTML = ""; }); const components = { ConfigProvider, OverlayDialogRoot, OverlayDialogContent, OverlayPortal, OverlayBackdrop, DialogTrigger, DialogClose, DialogTitle, DialogDescription, }; function fixture(template: string, setup = () => ({})) { const w = mount(defineComponent({ components, setup, template }), { attachTo: document.body, }); wrappers.push(w); return w; } async function settle() { await flushPromises(); await nextTick(); } const content = 'TitleHintclose'; describe("overlay state", () => { it("provides independent default, imperative methods, and notifications", () => { const onOpenChange = vi.fn(); const a = useOverlayState({ defaultOpen: true, onOpenChange }); const b = useOverlayState(); expect(a.isOpen.value).toBe(true); expect(b.isOpen.value).toBe(false); a.close(); a.toggle(); a.setOpen(false); a.open(); expect(onOpenChange.mock.calls.map((c) => c[0])).toEqual([ false, true, false, true, ]); expect(a.isOpen.value).toBe(true); expect(b.isOpen.value).toBe(false); }); it("preserves parent authority and supports reactive controlled mode changes", () => { const value = ref(false); const changed = vi.fn(); const state = useOverlayState({ isOpen: value, defaultOpen: true, onOpenChange: changed, }); state.open(); expect(changed).toHaveBeenLastCalledWith(true); expect(state.isOpen.value).toBe(false); value.value = true; expect(state.isOpen.value).toBe(true); state.close(); expect(state.isOpen.value).toBe(true); value.value = undefined; expect(state.isOpen.value).toBe(true); state.close(); expect(state.isOpen.value).toBe(false); value.value = false; state.toggle(); expect(state.isOpen.value).toBe(false); }); }); describe("dialog infrastructure", () => { it("teleports to provider target, associates labels, and releases locks on unmount", async () => { const target = document.createElement("div"); target.id = "portal-target"; document.body.append(target); const oldOverflow = document.body.style.overflow; const oldPointer = document.body.style.pointerEvents; const w = fixture( `open${content}`, ); await w.get("button").trigger("click"); await settle(); const dialog = target.querySelector("[role=dialog]")!; expect(dialog).not.toBeNull(); expect(dialog.getAttribute("aria-modal")).toBe("true"); expect( document.getElementById(dialog.getAttribute("aria-labelledby")!) ?.textContent, ).toBe("Title"); expect( document.getElementById(dialog.getAttribute("aria-describedby")!) ?.textContent, ).toBe("Hint"); expect(document.body.style.overflow).toBe("hidden"); w.unmount(); wrappers.splice(wrappers.indexOf(w), 1); await settle(); expect(target.querySelector("[role=dialog]")).toBeNull(); expect(document.body.style.overflow).toBe(oldOverflow); expect(document.body.style.pointerEvents).toBe(oldPointer); }); it("keeps controlled rejection open and notifies each request", async () => { const open = ref(true); const requests = vi.fn(); const w = fixture( `${content}`, () => ({ open, requests }), ); await settle(); const close = document.querySelector( "[role=dialog] button", ) as HTMLButtonElement; close.click(); await settle(); expect(requests).toHaveBeenCalledWith(false); expect(document.querySelector("[role=dialog]")).not.toBeNull(); open.value = false; await settle(); expect(document.querySelector("[role=dialog]")).toBeNull(); expect(w.emitted()).toBeDefined(); }); it("honors dynamic Escape policy and caller cancellation", async () => { const blocked = ref(true); const cancel = ref(false); fixture( `${content}`, () => ({ blocked, cancel }), ); await settle(); const escape = async () => { window.dispatchEvent( new KeyboardEvent("keydown", { key: "Escape", bubbles: true, cancelable: true, }), ); await settle(); }; await escape(); expect(document.querySelector("[role=dialog]")).not.toBeNull(); blocked.value = false; cancel.value = true; await nextTick(); await escape(); expect(document.querySelector("[role=dialog]")).not.toBeNull(); cancel.value = false; await nextTick(); await escape(); expect(document.querySelector("[role=dialog]")).toBeNull(); }); it("uses state over open prop and reacts to source changes", async () => { const state = useOverlayState(); fixture( `${content}`, () => ({ state }), ); await settle(); expect(document.querySelector("[role=dialog]")).toBeNull(); state.open(); await settle(); expect(document.querySelector("[role=dialog]")?.id).toBeTruthy(); state.close(); await settle(); expect(document.querySelector("[role=dialog]")).toBeNull(); }); it("keeps explicit ARIA and dynamic attrs including key removal", async () => { const attrs = ref>({}); fixture( `${content}`, () => ({ attrs }), ); await settle(); attrs.value = { "aria-label": "Explicit", "aria-describedby": "custom", class: "custom", "data-extra": "a", }; await settle(); let dialog = document.querySelector("[role=dialog]")!; expect(dialog.getAttribute("aria-label")).toBe("Explicit"); expect(dialog.getAttribute("aria-describedby")).toBe("custom"); expect(dialog.classList.contains("custom")).toBe(true); attrs.value = {}; await settle(); dialog = document.querySelector("[role=dialog]")!; expect(dialog.hasAttribute("aria-label")).toBe(false); expect(dialog.classList.contains("custom")).toBe(false); expect(dialog.hasAttribute("data-extra")).toBe(false); }); it("requires root context instead of silently creating an isolated layer", () => { const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); try { expect(() => mount(OverlayDialogContent)).toThrow("OverlayDialogRoot"); } finally { warn.mockRestore(); } }); }); it("closes only the highest modal and retains the parent lock", async () => { const parent = `ParentParent hintnestedNestedNested hint`; fixture( `${parent}`, ); await settle(); (document.querySelector("[data-nested]") as HTMLButtonElement).click(); await settle(); expect(document.querySelectorAll("[role=dialog]")).toHaveLength(2); window.dispatchEvent( new KeyboardEvent("keydown", { key: "Escape", bubbles: true, cancelable: true, }), ); await settle(); expect(document.querySelectorAll("[role=dialog]")).toHaveLength(1); expect(document.body.style.overflow).toBe("hidden"); window.dispatchEvent( new KeyboardEvent("keydown", { key: "Escape", bubbles: true, cancelable: true, }), ); await settle(); expect(document.querySelectorAll("[role=dialog]")).toHaveLength(0); expect(document.body.style.overflow).not.toBe("hidden"); }); it("preserves direction and language through teleport with reactive explicit overrides", async () => { const dir = ref<"ltr" | "rtl">("rtl"); const locale = ref("ar"); const attrs = ref>({}); fixture( `${content}`, () => ({ dir, locale, attrs }), ); await settle(); const panel = () => document.querySelector("[role=dialog]")!; expect(panel().getAttribute("dir")).toBe("rtl"); expect(panel().getAttribute("lang")).toBe("ar"); dir.value = "ltr"; locale.value = "en-US"; await settle(); expect(panel().getAttribute("dir")).toBe("ltr"); expect(panel().getAttribute("lang")).toBe("en-US"); attrs.value = { dir: "rtl", lang: "he" }; await settle(); expect(panel().getAttribute("dir")).toBe("rtl"); expect(panel().getAttribute("lang")).toBe("he"); attrs.value = {}; await settle(); expect(panel().getAttribute("lang")).toBe("en-US"); }); it("forwards cancelable auto-focus events without duplicate callbacks", async () => { const onOpen = vi.fn((event: Event) => { event.preventDefault(); (document.getElementById("custom-focus") as HTMLElement)?.focus(); }); const onClose = vi.fn((event: Event) => event.preventDefault()); const w = fixture( `openTitleHintclose`, () => ({ onOpen, onClose }), ); await w.get("button").trigger("click"); await settle(); expect(document.activeElement?.id).toBe("custom-focus"); expect(onOpen).toHaveBeenCalledTimes(1); ( document.querySelector( "[role=dialog] button:last-child", ) as HTMLButtonElement ).click(); await settle(); expect(onClose).toHaveBeenCalledTimes(1); expect(document.querySelector("[role=dialog]")).toBeNull(); }); it("keeps non-modal content interactive outside without claiming a modal lock", async () => { const dismissable = ref(false); fixture( `${content}`, () => ({ dismissable }), ); await settle(); expect( document.querySelector("[role=dialog]")?.hasAttribute("aria-modal"), ).toBe(false); expect(document.querySelector("[data-backdrop]")).toBeNull(); expect(document.body.style.overflow).not.toBe("hidden"); const outside = document.querySelector("[data-outside]") as HTMLButtonElement; outside.focus(); await settle(); expect(document.querySelector("[role=dialog]")).not.toBeNull(); (document.querySelector("[role=dialog] input") as HTMLInputElement).focus(); dismissable.value = true; await settle(); outside.focus(); await settle(); expect(document.querySelector("[role=dialog]")).toBeNull(); }); describe("floating overlay animation lifecycle", () => { it("clears an entrance cancelled by an ancestor reduced-motion change", async () => { const { useOverlayAnimation } = await import("../packages/vue/src/components/overlay/animation"); const element = ref(); const reduced = ref(false); let phase!: ReturnType; const realStyle = getComputedStyle; const css = vi .spyOn(window, "getComputedStyle") .mockImplementation((el) => { if (el === element.value) return { animationName: el.closest('[data-reduce-motion="true"]') ? "none" : "enter", } as CSSStyleDeclaration; return realStyle(el); }); try { fixture( '
', () => { phase = useOverlayAnimation(ref(true), element); return { element, reduced }; }, ); await settle(); expect(phase.entering.value).toBe(true); reduced.value = true; await settle(); // No synthetic animationend: CSS cancels the entrance entirely. expect(phase.entering.value).toBe(false); } finally { css.mockRestore(); } }); async function animationFixture(transform: string, animated = true) { const { useOverlayAnimation } = await import("../packages/vue/src/components/overlay/animation"); const element = ref(); const open = ref(true); const position = ref(transform); const measuring = ref(true); let phase!: ReturnType; const w = fixture( '
', () => { phase = useOverlayAnimation(open, element, true); return { element, phase, transform: position, measuring, animated, }; }, ); await settle(); return { w, phase, open, element, position, measuring }; } it.each(["translate(0, -200%)", "translate(0px, -200%)"])( "waits through Popper measurement serialized as %s", async (transform) => { const { phase, element, position, measuring } = await animationFixture(transform); expect(phase.entering.value).toBe(true); // Positioning and removing Reka's temporary animation:none are separate mutations. position.value = "translate(100px, 150px)"; measuring.value = false; await settle(); expect(phase.entering.value).toBe(true); phase.onAnimationEnd({ target: element.value, currentTarget: element.value, animationName: "enter", } as unknown as AnimationEvent); expect(phase.entering.value).toBe(false); }, ); it("clears a genuinely absent animation after placement without a timeout", async () => { const { phase, position, measuring } = await animationFixture( "translate(0px, -200%)", false, ); expect(phase.entering.value).toBe(true); position.value = "translate(100px, 150px)"; measuring.value = false; await settle(); expect(phase.entering.value).toBe(false); }); });