Files

159 lines
5.4 KiB
TypeScript

import { afterEach, expect, it, vi } from "vitest";
import { mount, flushPromises } from "@vue/test-utils";
import { defineComponent, ref } from "vue";
import {
Modal,
ModalBackdrop,
ModalContainer,
ModalDialog,
ModalHeading,
ModalBody,
ModalCloseTrigger,
ModalClose,
ModalTrigger,
Button,
} from "../packages/vue/src";
const wrappers: ReturnType<typeof mount>[] = [];
afterEach(async () => {
for (const w of wrappers.splice(0)) w.unmount();
await flushPromises();
document.body.innerHTML = "";
});
const components = {
Modal,
ModalBackdrop,
ModalContainer,
ModalDialog,
ModalHeading,
ModalBody,
ModalCloseTrigger,
ModalClose,
ModalTrigger,
Button,
};
const panel =
"<ModalBackdrop><ModalContainer><ModalDialog><ModalCloseTrigger/><ModalHeading>标题</ModalHeading><ModalBody>内容</ModalBody><ModalClose as-child><Button>完成</Button></ModalClose></ModalDialog></ModalContainer></ModalBackdrop>";
function view(template: string, setup = () => ({})) {
const w = mount(defineComponent({ components, template, setup }), {
attachTo: document.body,
});
wrappers.push(w);
return w;
}
it("opens automatic Button triggers and closes with real named dialog associations", async () => {
const w = view(`<Modal><Button>打开</Button>${panel}</Modal>`);
await w.get("button").trigger("click");
await flushPromises();
const dialog = document.querySelector("[role=dialog]")!;
expect(dialog).not.toBeNull();
expect(
document.getElementById(dialog.getAttribute("aria-labelledby")!)
?.textContent,
).toBe("标题");
expect(w.get("button").attributes("aria-controls")).toBe(dialog.id);
(
Array.from(dialog.querySelectorAll("button")).find((b) =>
b.textContent?.includes("完成"),
) as HTMLElement
).click();
await flushPromises();
expect(document.querySelector("[role=dialog]")).toBeNull();
expect(document.body.style.overflow).not.toBe("hidden");
});
it("supports standalone controlled Backdrop and parent rejection", async () => {
const open = ref(true),
update = vi.fn();
view(
panel.replace(
"<ModalBackdrop>",
'<ModalBackdrop :open="open" @update:open="update">',
),
() => ({ open, update }),
);
await flushPromises();
(
document.querySelector("[data-slot=modal-close-trigger]") as HTMLElement
).click();
await flushPromises();
expect(update).toHaveBeenCalledWith(false);
expect(document.querySelector("[role=dialog]")).not.toBeNull();
open.value = false;
await flushPromises();
expect(document.querySelector("[role=dialog]")).toBeNull();
});
it("preserves preventDefault, disabled loading and custom keyboard trigger semantics", async () => {
const cancel = ref(true),
disabled = ref(false),
click = vi.fn();
const w = view(
`<Modal><ModalTrigger as="span" :is-disabled="disabled" @click="click($event); cancel && $event.preventDefault()">打开</ModalTrigger>${panel}</Modal>`,
() => ({ cancel, disabled, click }),
);
await w.get("[data-slot=modal-trigger]").trigger("click");
await flushPromises();
expect(document.querySelector("[role=dialog]")).toBeNull();
expect(click).toHaveBeenCalledTimes(1);
disabled.value = true;
await flushPromises();
await w.get("[data-slot=modal-trigger]").trigger("click");
expect(click).toHaveBeenCalledTimes(1);
disabled.value = false;
cancel.value = false;
await flushPromises();
await w.get("[data-slot=modal-trigger]").trigger("keydown", { key: " " });
await w.get("[data-slot=modal-trigger]").trigger("keyup", { key: " " });
await flushPromises();
expect(document.querySelector("[role=dialog]")).not.toBeNull();
expect(click).toHaveBeenCalledTimes(2);
});
it("keeps explicit dynamic heading IDs accessible without false primitive warnings", async () => {
const id = ref("named"),
warn = vi.spyOn(console, "warn").mockImplementation(() => {});
try {
const w = view(
`<Modal><Button>打开</Button><ModalBackdrop><ModalContainer><ModalDialog><ModalHeading :id="id">动态名称</ModalHeading></ModalDialog></ModalContainer></ModalBackdrop></Modal>`,
() => ({ id }),
);
await w.get("button").trigger("click");
await flushPromises();
expect(
document.querySelector("[role=dialog]")?.getAttribute("aria-labelledby"),
).toBe("named");
id.value = "renamed";
await flushPromises();
expect(
document.querySelector("[role=dialog]")?.getAttribute("aria-labelledby"),
).toBe("renamed");
expect(document.getElementById("named")).toBeNull();
expect(warn).not.toHaveBeenCalled();
} finally {
warn.mockRestore();
}
});
it("runs asChild handlers once per real click and respects stop/default cancellation", async () => {
const child = vi.fn(),
trigger = vi.fn();
const w = view(
`<Modal><ModalTrigger as-child @click="trigger"><Button @click.stop="child">打开</Button></ModalTrigger>${panel}</Modal>`,
() => ({ child, trigger }),
);
for (let i = 1; i <= 2; i++) {
await w.get("button").trigger("click");
await flushPromises();
expect(child).toHaveBeenCalledTimes(i);
expect(trigger).toHaveBeenCalledTimes(i);
expect(document.querySelector("[role=dialog]")).not.toBeNull();
window.dispatchEvent(
new KeyboardEvent("keydown", {
key: "Escape",
bubbles: true,
cancelable: true,
}),
);
await flushPromises();
expect(document.querySelector("[role=dialog]")).toBeNull();
}
});