Files
heroui-vue/tests/b01-menu-dropdown.test.ts
T
maoyingjie 1692c6b0ac fix: menu selection ARIA, cancelable activation, submenu and long-press
- Selected items use menuitemradio/menuitemcheckbox + aria-checked + data-selected
- onClick preventDefault cancels action/selection/close (user handlers first)
- Submenu trigger is a single menuitem (asChild); standalone Menu owns Reka root
- DropdownRoot trigger=longPress with threshold, keyboard fallback, cleanup
- Export *Props types; independent tarball ESM/CSS + NodeNext/bundler contracts
- Esc still closes the full submenu stack (recorded gap)
2026-10-24 21:19:04 +08:00

321 lines
9.5 KiB
TypeScript

import { afterEach, expect, it, vi } from "vitest";
import { mount, flushPromises } from "@vue/test-utils";
import { defineComponent, h, ref, nextTick } from "vue";
import {
Button,
DropdownRoot,
DropdownPopover,
DropdownMenu,
DropdownItem,
DropdownItemIndicator,
DropdownSection,
DropdownSubmenuTrigger,
DropdownSubmenuIndicator,
Label,
Menu,
MenuItem,
MenuSection,
} from "../packages/vue/src";
const cleanup: (() => void)[] = [];
afterEach(() => {
cleanup
.splice(0)
.reverse()
.forEach((fn) => fn());
vi.useRealTimers();
document.body.innerHTML = "";
});
function mountApp(setup: () => unknown, render?: () => unknown) {
const w = mount(
defineComponent(
render
? { setup: setup as never, render: render as never }
: { setup: setup as never },
),
{ attachTo: document.body },
);
cleanup.push(() => w.unmount());
return w;
}
it("Dropdown opens and action item fires once", async () => {
const actions: string[] = [];
mountApp(
() => () =>
h(DropdownRoot, { defaultOpen: true }, () => [
h(Button, () => "操作"),
h(DropdownPopover, {}, () =>
h(DropdownMenu, { onAction: (k: string) => actions.push(k) }, () => [
h(DropdownItem, { id: "new-file", textValue: "新建文件" }, () =>
h(Label, () => "新建文件"),
),
h(
DropdownItem,
{ id: "delete-file", textValue: "删除文件", variant: "danger" },
() => h(Label, () => "删除文件"),
),
]),
),
]),
);
await flushPromises();
await nextTick();
const items = document.querySelectorAll('[data-slot="menu-item"]');
expect(items.length).toBe(2);
expect((items[1] as HTMLElement).className).toContain("hv-menu-item--danger");
expect((items[0] as HTMLElement).getAttribute("role")).toBe("menuitem");
(items[0] as HTMLElement).click();
await flushPromises();
expect(actions).toEqual(["new-file"]);
});
it("Menu multiple selection toggles keys", async () => {
const selected = ref<string[]>(["apple"]);
mountApp(
() => () =>
h(DropdownRoot, { defaultOpen: true }, () => [
h(Button, () => "水果"),
h(DropdownPopover, {}, () =>
h(
DropdownMenu,
{
selectionMode: "multiple",
selectedKeys: selected.value,
"onUpdate:selectedKeys": (k: (string | number)[]) => {
selected.value = k as string[];
},
},
() => [
h(DropdownItem, { id: "apple", textValue: "苹果" }, () => [
h(DropdownItemIndicator),
h(Label, () => "苹果"),
]),
h(DropdownItem, { id: "banana", textValue: "香蕉" }, () => [
h(DropdownItemIndicator),
h(Label, () => "香蕉"),
]),
],
),
),
]),
);
await flushPromises();
await nextTick();
const items = document.querySelectorAll('[data-slot="menu-item"]');
expect(items.length).toBe(2);
(items[1] as HTMLElement).click();
await flushPromises();
await nextTick();
expect(selected.value).toContain("banana");
expect(selected.value).toContain("apple");
});
it("MenuSection can own section-level selection", async () => {
const styles = ref<string[]>(["bold"]);
mountApp(
() => () =>
h(DropdownRoot, { defaultOpen: true }, () => [
h(Button, () => "样式"),
h(DropdownPopover, {}, () =>
h(Menu, {}, () =>
h(
MenuSection,
{
selectionMode: "multiple",
selectedKeys: styles.value,
"onUpdate:selectedKeys": (k: (string | number)[]) => {
styles.value = k as string[];
},
},
() => [
h(MenuItem, { id: "bold", textValue: "粗体" }, () =>
h(Label, () => "粗体"),
),
h(MenuItem, { id: "italic", textValue: "斜体" }, () =>
h(Label, () => "斜体"),
),
],
),
),
),
]),
);
await flushPromises();
await nextTick();
const items = document.querySelectorAll('[data-slot="menu-item"]');
expect(items.length).toBe(2);
(items[1] as HTMLElement).click();
await flushPromises();
await nextTick();
expect(styles.value).toEqual(expect.arrayContaining(["bold", "italic"]));
});
it("disabled item is marked disabled", async () => {
mountApp(
() => () =>
h(DropdownRoot, { defaultOpen: true }, () => [
h(Button, () => "操作"),
h(DropdownPopover, {}, () =>
h(Menu, { disabledKeys: ["x"] }, () =>
h(MenuItem, { id: "x", textValue: "X" }, () => h(Label, () => "X")),
),
),
]),
);
await flushPromises();
await nextTick();
const item = document.querySelector('[data-slot="menu-item"]') as HTMLElement;
expect(item).toBeTruthy();
const disabled =
item.hasAttribute("disabled") ||
item.getAttribute("aria-disabled") === "true" ||
item.getAttribute("data-disabled") === "true" ||
(item as HTMLInputElement).disabled === true;
expect(disabled).toBe(true);
});
it("indicator uses checkmark svg and data-visible when selected", async () => {
mountApp(
() => () =>
h(DropdownRoot, { defaultOpen: true }, () => [
h(Button, () => "水果"),
h(DropdownPopover, {}, () =>
h(
DropdownMenu,
{ selectionMode: "multiple", selectedKeys: ["apple"] },
() => [
h(DropdownItem, { id: "apple", textValue: "苹果" }, () => [
h(DropdownItemIndicator),
h(Label, () => "苹果"),
]),
],
),
),
]),
);
await flushPromises();
await nextTick();
const ind = document.querySelector('[data-slot="menu-item-indicator"]');
expect(ind).toBeTruthy();
expect(ind!.getAttribute("data-type")).toBe("checkmark");
expect(
ind!.querySelector('[data-slot="menu-item-indicator--checkmark"]'),
).not.toBeNull();
});
it("submenu trigger opens nested items", async () => {
mountApp(
() => () =>
h(DropdownRoot, { defaultOpen: true }, () => [
h(Button, () => "分享"),
h(DropdownPopover, {}, () =>
h(DropdownMenu, {}, () => [
h(DropdownItem, { id: "copy-link", textValue: "复制链接" }, () =>
h(Label, () => "复制链接"),
),
h(DropdownSubmenuTrigger, {}, () => [
h(DropdownItem, { id: "share", textValue: "分享" }, () => [
h(Label, () => "其他"),
h(DropdownSubmenuIndicator),
]),
h(DropdownPopover, {}, () =>
h(DropdownMenu, {}, () =>
h(
DropdownItem,
{ id: "whatsapp", textValue: "WhatsApp" },
() => h(Label, () => "WhatsApp"),
),
),
),
]),
]),
),
]),
);
await flushPromises();
await nextTick();
const triggers = document.querySelectorAll(
'[data-slot="menu-item"][data-has-submenu="true"], [data-slot="dropdown-submenu-trigger"]',
);
expect(triggers.length).toBeGreaterThanOrEqual(1);
// Open submenu via pointer (Reka SubTrigger)
(triggers[0] as HTMLElement).dispatchEvent(
new MouseEvent("pointerenter", { bubbles: true }),
);
(triggers[0] as HTMLElement).click();
await flushPromises();
await nextTick();
const html = document.body.innerHTML;
expect(html).toContain("WhatsApp");
expect(html).toContain("其他");
});
it("long-press trigger opens on hold and suppresses click", async () => {
vi.useFakeTimers();
const w = mountApp(
() => () =>
h(DropdownRoot, { trigger: "longPress", longPressDuration: 300 }, () => [
h(Button, { "aria-label": "菜单" }, () => "长按"),
h(DropdownPopover, {}, () =>
h(DropdownMenu, {}, () =>
h(DropdownItem, { id: "x", textValue: "X" }, () =>
h(Label, () => "X"),
),
),
),
]),
);
await flushPromises();
const btn = w.get("button");
btn.trigger("pointerdown", { button: 0, clientX: 10, clientY: 10 });
await vi.advanceTimersByTimeAsync(350);
await nextTick();
expect(
document
.querySelector('[data-slot="dropdown-popover"]')
?.getAttribute("data-state"),
).toBe("open");
vi.useRealTimers();
});
it("long-press cancel on move threshold", async () => {
vi.useFakeTimers();
mountApp(
() => () =>
h(DropdownRoot, { trigger: "longPress", longPressDuration: 300 }, () => [
h(Button, () => "长按"),
h(DropdownPopover, {}, () =>
h(DropdownMenu, {}, () =>
h(DropdownItem, { id: "x", textValue: "X" }, () =>
h(Label, () => "X"),
),
),
),
]),
);
await flushPromises();
const btn = document.querySelector("button") as HTMLElement;
btn.dispatchEvent(
new MouseEvent("pointerdown", {
bubbles: true,
button: 0,
clientX: 0,
clientY: 0,
}),
);
btn.dispatchEvent(
new MouseEvent("pointermove", { bubbles: true, clientX: 40, clientY: 0 }),
);
await vi.advanceTimersByTimeAsync(400);
await nextTick();
expect(
document
.querySelector('[data-slot="dropdown-popover"]')
?.getAttribute("data-state"),
).not.toBe("open");
vi.useRealTimers();
});