Files
heroui-vue/tests/b01-review.probe.test.ts
T
maoyingjie e797c25d46 fix: real trigger owns long press, Vue click chain and layered Esc
- DropdownTrigger takes over the longPress press lifecycle in capture phase and
  cancels Reka's own open/toggle through its defaultPrevented guard; DropdownRoot
  no longer wraps the whole menu content. Short click, Enter and Space now open
  exactly once, and release cannot close the menu again.
- MenuItem hands user handlers plus activation to Vue as one DOM onClick array,
  so preventDefault, capture and stopImmediatePropagation keep native semantics
  instead of a hand-rolled handler loop.
- DropdownSubmenuTrigger owns its own open state and closes only its level on
  Escape, restoring focus to the submenu trigger; the parent menu stays open and
  a second Escape closes the root.
2026-10-24 21:42:25 +08:00

163 lines
4.9 KiB
TypeScript

import { afterEach, expect, it, vi } from "vitest";
import { Button, DropdownTrigger } from "../packages/vue/src";
import { mount, flushPromises } from "@vue/test-utils";
import { defineComponent, h } from "vue";
import {
DropdownRoot,
DropdownPopover,
DropdownMenu,
DropdownItem,
DropdownItemIndicator,
DropdownSubmenuTrigger,
DropdownSubmenuIndicator,
Menu,
MenuItem,
} from "../packages/vue/src";
const cleanup: (() => void)[] = [];
afterEach(() => {
cleanup
.splice(0)
.reverse()
.forEach((fn) => fn());
document.body.innerHTML = "";
});
async function renderMenu(
menuProps = {},
itemProps = {},
children = () => "A",
) {
const w = mount(
defineComponent({
setup: () => () =>
h(DropdownRoot, { defaultOpen: true }, () =>
h(DropdownPopover, {}, () =>
h(DropdownMenu, menuProps, () =>
h(
DropdownItem,
{ id: "a", textValue: "A", ...itemProps },
children,
),
),
),
),
}),
{ attachTo: document.body },
);
cleanup.push(() => w.unmount());
await flushPromises();
return document.querySelector('[data-slot="menu-item"]') as HTMLElement;
}
it("selected multiple item exposes checked menu semantics and source CSS selected state", async () => {
const node = await renderMenu(
{ selectionMode: "multiple", selectedKeys: ["a"] },
{},
() => [h(DropdownItemIndicator, { type: "dot" }), "A"] as never,
);
expect(node.getAttribute("role")).toBe("menuitemcheckbox");
expect(node.getAttribute("aria-checked")).toBe("true");
});
it("preventing click prevents action and default close", async () => {
const action = vi.fn();
const node = await renderMenu(
{ onAction: action },
{ onClick: (e: MouseEvent) => e.preventDefault() },
);
node.click();
await flushPromises();
expect(action).not.toHaveBeenCalled();
expect(document.querySelector('[data-slot="menu-item"]')).not.toBeNull();
});
it("submenu trigger has one menuitem DOM node rather than nested interactive items", async () => {
const w = mount(
defineComponent({
setup: () => () =>
h(DropdownRoot, { defaultOpen: true }, () =>
h(DropdownPopover, {}, () =>
h(DropdownMenu, {}, () =>
h(DropdownSubmenuTrigger, {}, () => [
h(DropdownItem, { id: "sub", textValue: "Sub" }, () => [
"Sub",
h(DropdownSubmenuIndicator),
]),
h(DropdownPopover, {}, () =>
h(DropdownMenu, {}, () =>
h(DropdownItem, { id: "child" }, () => "Child"),
),
),
]),
),
),
),
}),
{ attachTo: document.body },
);
cleanup.push(() => w.unmount());
await flushPromises();
expect(
document.querySelector('[role="menuitem"] [role="menuitem"]'),
).toBeNull();
});
it("standalone exported Menu and MenuItem do not require an internal DropdownMenuRoot", () => {
expect(() => {
const w = mount(Menu, {
slots: { default: () => h(MenuItem, { id: "a" }, () => "A") },
});
cleanup.push(() => w.unmount());
}).not.toThrow();
});
it("stopImmediatePropagation stops subsequent user handlers and activation", async () => {
const action = vi.fn();
const second = vi.fn();
const node = await renderMenu(
{ onAction: action },
{
onClick: [(e: MouseEvent) => e.stopImmediatePropagation(), second],
},
);
node.click();
await flushPromises();
expect(second).not.toHaveBeenCalled();
expect(action).not.toHaveBeenCalled();
});
it("longPress trigger does not open on a short click of the actual button", async () => {
const w = mount(
defineComponent({
setup: () => () =>
h(DropdownRoot, { trigger: "longPress" }, () => [
h(DropdownTrigger, {}, () => h(Button, {}, () => "Hold")),
h(DropdownPopover, {}, () =>
h(DropdownMenu, {}, () => h(DropdownItem, { id: "a" }, () => "A")),
),
]),
}),
{ attachTo: document.body },
);
cleanup.push(() => w.unmount());
await flushPromises();
(w.get("button").element as HTMLElement).click();
await flushPromises();
expect(document.querySelector('[data-slot="menu-item"]')).toBeNull();
});
it("longPress keyboard Enter opens instead of toggling twice", async () => {
const w = mount(
defineComponent({
setup: () => () =>
h(DropdownRoot, { trigger: "longPress" }, () => [
h(DropdownTrigger, {}, () => h(Button, {}, () => "Hold")),
h(DropdownPopover, {}, () =>
h(DropdownMenu, {}, () => h(DropdownItem, { id: "a" }, () => "A")),
),
]),
}),
{ attachTo: document.body },
);
cleanup.push(() => w.unmount());
await flushPromises();
await w.get("button").trigger("keydown", { key: "Enter" });
await flushPromises();
expect(document.querySelector('[data-slot="menu-item"]')).not.toBeNull();
});