import { afterEach, expect, it, vi } from "vitest"; import { mount, flushPromises } from "@vue/test-utils"; import { defineComponent, nextTick, ref } from "vue"; import { AlertDialog, AlertDialogBackdrop, AlertDialogBody, AlertDialogClose, AlertDialogCloseTrigger, AlertDialogContainer, AlertDialogDialog, AlertDialogFooter, AlertDialogHeader, AlertDialogHeading, AlertDialogIcon, AlertDialogTrigger, Button, useOverlayState, } from "../packages/vue/src"; const wrappers: ReturnType[] = []; afterEach(async () => { for (const w of wrappers.splice(0)) w.unmount(); await flushPromises(); document.body.innerHTML = ""; }); const components = { AlertDialog, AlertDialogBackdrop, AlertDialogContainer, AlertDialogDialog, AlertDialogCloseTrigger, AlertDialogHeader, AlertDialogHeading, AlertDialogIcon, AlertDialogBody, AlertDialogFooter, AlertDialogClose, AlertDialogTrigger, Button, }; const panel = '要删除吗?内容'; function view(template: string, setup = () => ({})) { const w = mount(defineComponent({ components, template, setup }), { attachTo: document.body, }); wrappers.push(w); return w; } it("opens with role=alertdialog and real accessible name", async () => { const w = view(`${panel}`); await w.get("button").trigger("click"); await flushPromises(); const dialog = document.querySelector('[role="alertdialog"]')!; expect(dialog).not.toBeNull(); expect(dialog.getAttribute("data-slot")).toBe("alert-dialog-dialog"); const labelled = dialog.getAttribute("aria-labelledby")!; expect(document.getElementById(labelled)?.textContent).toContain("要删除吗"); }); it("defaults to non-dismissable outside click and disabled Escape", async () => { const w = view(`${panel}`); await w.get("button").trigger("click"); await flushPromises(); const dialog = document.querySelector('[role="alertdialog"]')!; expect(dialog).not.toBeNull(); // Escape must not close when keyboard dismiss is disabled (default). window.dispatchEvent( new KeyboardEvent("keydown", { key: "Escape", bubbles: true, cancelable: true, }), ); await flushPromises(); expect(document.querySelector('[role="alertdialog"]')).not.toBeNull(); }); it("allows Escape and outside dismiss only when explicitly enabled", async () => { const w = view( `标题`, ); await w.get("button").trigger("click"); await flushPromises(); expect(document.querySelector('[role="alertdialog"]')).not.toBeNull(); window.dispatchEvent( new KeyboardEvent("keydown", { key: "Escape", bubbles: true, cancelable: true, }), ); await flushPromises(); await nextTick(); expect(document.querySelector('[role="alertdialog"]')).toBeNull(); }); it("close button and AlertDialogClose close the dialog", async () => { const w = view(`${panel}`); await w.get("button").trigger("click"); await flushPromises(); expect(document.querySelector('[role="alertdialog"]')).not.toBeNull(); ( document.querySelector( "[data-slot=alert-dialog-close-trigger]", ) as HTMLElement ).click(); await flushPromises(); await nextTick(); expect(document.querySelector('[role="alertdialog"]')).toBeNull(); await w.get("button").trigger("click"); await flushPromises(); const cancel = [...document.querySelectorAll("button")].find( (b) => b.textContent?.trim() === "取消", )!; cancel.click(); await flushPromises(); await nextTick(); expect(document.querySelector('[role="alertdialog"]')).toBeNull(); }); it("controlled parent rejects open request and allows retry", async () => { const opens = vi.fn(); const w = view( `${panel}`, () => ({ opens }), ); await w.get("button").trigger("click"); await flushPromises(); expect(opens).toHaveBeenCalled(); expect(document.querySelector('[role="alertdialog"]')).toBeNull(); // Same request again still emits. const before = opens.mock.calls.length; await w.get("button").trigger("click"); await flushPromises(); expect(opens.mock.calls.length).toBeGreaterThan(before); }); it("supports useOverlayState controlled mode", async () => { const w = view( `
状态
`, () => ({ state: useOverlayState() }), ); await w.get("button").trigger("click"); await flushPromises(); expect(document.querySelector('[role="alertdialog"]')).not.toBeNull(); }); it("icon status variants and custom icon slot", async () => { const w = view( `X成功`, ); await w.get("button").trigger("click"); await flushPromises(); const icon = document.querySelector('[data-slot="alert-dialog-icon"]')!; expect(icon.className).toContain("hv-alert-dialog__icon--success"); expect(icon.className).toContain("custom-icon"); expect(icon.querySelector(".custom")?.textContent).toBe("X"); }); it("placement and size land on container and dialog", async () => { const w = view( `标题`, ); await w.get("button").trigger("click"); await flushPromises(); const container = document.querySelector( '[data-slot="alert-dialog-container"]', )!; expect(container.getAttribute("data-placement")).toBe("bottom"); expect(container.className).toContain("hv-alert-dialog__container"); const dialog = document.querySelector('[role="alertdialog"]')!; expect(dialog.className).toContain("hv-alert-dialog__dialog--lg"); expect(dialog.getAttribute("data-placement")).toBe("bottom"); }); it("explicit aria-labelledby wins over computed titles", async () => { const w = view( `内部标题`, ); await w.get("button").trigger("click"); await flushPromises(); const dialog = document.querySelector('[role="alertdialog"]')!; expect(dialog.getAttribute("aria-labelledby")).toBe("custom-title"); }); it("all 14 demos mount without Vue warnings", async () => { const names = [ "default", "statuses", "placements", "sizes", "controlled", "custom-icon", "custom-trigger", "backdrop-variants", "custom-backdrop", "dismiss-behavior", "close-methods", "custom-animations", "custom-portal", "custom-styles", ]; expect(names).toHaveLength(14); const warnings: string[] = []; const spy = vi.spyOn(console, "warn").mockImplementation((...args) => { warnings.push(args.map(String).join(" ")); }); for (const name of names) { const mod = await import(`../apps/docs/demos/alert-dialog-${name}.vue`); const w = mount(mod.default, { attachTo: document.body }); await nextTick(); expect(w.html().length).toBeGreaterThan(0); w.unmount(); } spy.mockRestore(); expect(warnings).toEqual([]); }); it("outside pointer remains blocked by default and closes only when enabled", async () => { const dismissable = ref(false); const w = view( `保护操作`, () => ({ dismissable }), ); await w.get("button").trigger("click"); await flushPromises(); const outside = () => { const backdrop = document.querySelector( '[data-slot="alert-dialog-backdrop"]', )!; backdrop.dispatchEvent( new MouseEvent("pointerdown", { bubbles: true, cancelable: true }), ); backdrop.dispatchEvent(new MouseEvent("click", { bubbles: true })); }; outside(); await flushPromises(); expect(document.querySelector('[role="alertdialog"]')).not.toBeNull(); dismissable.value = true; await nextTick(); outside(); await flushPromises(); expect(document.querySelector('[role="alertdialog"]')).toBeNull(); });