Files
heroui-vue/tests/b01-complete-review.probe.test.ts
T
maoyingjie 53b4639b0e fix: portal dropdown surfaces and close the review capability gaps
- Root and submenu surfaces render through the dropdown portal with the overlay
  z-index, so clipping/isolation ancestors no longer cut the menu; panel height
  is capped by the floating available height and the animation origin maps the
  real transform origin.
- Standalone Menu is a static in-flow list: no scroll lock, no outside pointer
  blocking, no floating placement and no focus stealing.
- Selection ownership is isolated per level: section disabled keys add to the
  menu list, and a nested Menu resets the section owner.
- closeOnSelect resolves item > menu policy > per-mode default for selection
  items; actions and radio items close, checkboxes stay open.
- Business keys no longer become DOM ids (instance ids + data-key), and submenu
  surfaces label themselves from the real trigger.
- Long-press keydown runs the caller's handlers first and only opens when not
  cancelled; submenu panels merge caller keydown with the internal Escape.
- Link items render a real anchor, aria labels land on the role=menu node,
  submenu panels forward placement/offset/collision props and caller attrs, and
  the popover provides the default Surface variant.
- Wrapper parts declare real props/emits, plus the missing official capability
  props (disallowEmptySelection, escapeKeyBehavior, shouldFocusWrap, autoFocus,
  shouldCloseOnSelect, close, empty slot, isKeyboardDismissDisabled,
  shouldCloseOnInteractOutside, placement/offset family, scoped slots).
- Ship an independent consumer fixture for browser-level verification.
2026-10-24 22:56:44 +08:00

244 lines
7.8 KiB
TypeScript

import Surface from "../packages/vue/src/components/surface/Surface.vue";
import { surfaceContextKey } from "../packages/vue/src/components/surface/context";
import { afterEach, expect, it, vi } from "vitest";
import { mount, flushPromises } from "@vue/test-utils";
import { defineComponent, h, ref, inject } from "vue";
import {
Button,
DropdownRoot,
DropdownTrigger,
DropdownPopover,
DropdownMenu,
DropdownSection,
DropdownSubmenuTrigger,
DropdownSubmenuIndicator,
DropdownItem,
Menu,
} from "../packages/vue/src";
const cleanup: (() => void)[] = [];
afterEach(() => {
cleanup
.splice(0)
.reverse()
.forEach((fn) => fn());
vi.useRealTimers();
document.body.innerHTML = "";
});
async function render(
menuProps: Record<string, unknown>,
children: () => unknown,
) {
const w = mount(
defineComponent({
setup: () => () =>
h(DropdownRoot, { defaultOpen: true, modal: false }, () =>
h(DropdownPopover, {}, () =>
h(DropdownMenu, menuProps, { default: children }),
),
),
}),
{ attachTo: document.body },
);
cleanup.push(() => w.unmount());
await flushPromises();
return w;
}
it("section disabledKeys work when selection is inherited", async () => {
await render({ selectionMode: "multiple" }, () =>
h(DropdownSection, { disabledKeys: ["x"] }, () =>
h(DropdownItem, { id: "x" }, () => "X"),
),
);
expect(
document
.querySelector('[data-slot="menu-item"]')
?.getAttribute("aria-disabled"),
).toBe("true");
});
it("root disabledKeys remain effective in a section with its own selection", async () => {
await render({ disabledKeys: ["x"] }, () =>
h(DropdownSection, { selectionMode: "multiple" }, () =>
h(DropdownItem, { id: "x" }, () => "X"),
),
);
expect(
document
.querySelector('[data-slot="menu-item"]')
?.getAttribute("aria-disabled"),
).toBe("true");
});
it("single-selection closeOnSelect=false keeps the menu open", async () => {
await render({ selectionMode: "single" }, () =>
h(DropdownItem, { id: "x", closeOnSelect: false }, () => "X"),
);
(document.querySelector('[data-slot="menu-item"]') as HTMLElement).click();
await flushPromises();
expect(document.querySelector('[data-slot="menu-item"]')).not.toBeNull();
});
it("multiple-selection closeOnSelect=true closes the menu", async () => {
await render({ selectionMode: "multiple" }, () =>
h(DropdownItem, { id: "x", closeOnSelect: true }, () => "X"),
);
(document.querySelector('[data-slot="menu-item"]') as HTMLElement).click();
await flushPromises();
expect(document.querySelector('[data-slot="menu-item"]')).toBeNull();
});
it("typed numeric and string keys never create duplicate DOM ids", async () => {
await render({ selectionMode: "multiple" }, () => [
h(DropdownItem, { id: 0 }, () => "Number"),
h(DropdownItem, { id: "0" }, () => "String"),
]);
const nodes = Array.from(
document.querySelectorAll('[data-slot="menu-item"]'),
);
expect(new Set(nodes.map((n) => n.id)).size).toBe(nodes.length);
});
it("standalone Menu does not lock body scrolling or disable outside controls", async () => {
const outside = document.createElement("button");
document.body.append(outside);
const w = mount(Menu, {
slots: { default: () => h(DropdownItem, { id: "x" }, () => "X") },
attachTo: document.body,
});
cleanup.push(() => w.unmount());
await flushPromises();
expect(document.body.style.overflow).not.toBe("hidden");
expect(document.body.style.pointerEvents).not.toBe("none");
expect(outside.getAttribute("aria-hidden")).not.toBe("true");
});
it("native link menu items retain anchor href semantics", async () => {
await render({}, () =>
h(
DropdownItem,
{ id: "link", href: "/target", target: "_blank" },
() => "Go",
),
);
const item = document.querySelector('[data-slot="menu-item"]')!;
expect(item.tagName).toBe("A");
expect(item.getAttribute("href")).toBe("/target");
});
it("longPress cannot open a disabled actual trigger", async () => {
vi.useFakeTimers();
const w = mount(
defineComponent({
setup: () => () =>
h(DropdownRoot, { trigger: "longPress", modal: false }, () => [
h(DropdownTrigger, {}, () =>
h(Button, { isDisabled: true }, () => "Hold"),
),
h(DropdownPopover, {}, () =>
h(DropdownMenu, {}, () => h(DropdownItem, { id: "x" }, () => "X")),
),
]),
}),
{ attachTo: document.body },
);
cleanup.push(() => w.unmount());
await w.get("button").trigger("pointerdown", {
pointerType: "mouse",
button: 0,
isPrimary: true,
});
await vi.advanceTimersByTimeAsync(600);
await flushPromises();
expect(document.querySelector('[data-slot="menu-item"]')).toBeNull();
});
it("cancelled trigger keydown does not open longPress menu", async () => {
const w = mount(
defineComponent({
setup: () => () =>
h(DropdownRoot, { trigger: "longPress", modal: false }, () => [
h(
DropdownTrigger,
{ onKeydown: (e: KeyboardEvent) => e.preventDefault() },
() => h(Button, {}, () => "Hold"),
),
h(DropdownPopover, {}, () =>
h(DropdownMenu, {}, () => h(DropdownItem, { id: "x" }, () => "X")),
),
]),
}),
{ attachTo: document.body },
);
cleanup.push(() => w.unmount());
await w.get("button").trigger("keydown", { key: "Enter" });
await flushPromises();
expect(document.querySelector('[data-slot="menu-item"]')).toBeNull();
});
it("root menu's accessible name follows its labelled DropdownMenu", async () => {
await render({ "aria-label": "File actions" }, () =>
h(DropdownItem, { id: "x" }, () => "X"),
);
const menu = document.querySelector('[role="menu"]')!;
expect(menu.getAttribute("aria-label")).toBe("File actions");
});
it("submenu Menu owns its selection instead of inheriting a parent section owner", async () => {
await render({}, () =>
h(
DropdownSection,
{ selectionMode: "multiple", selectedKeys: ["outer"] },
() =>
h(DropdownMenu, { selectionMode: "none" }, () =>
h(DropdownItem, { id: "inner" }, () => "Inner"),
),
),
);
expect(
document.querySelector('[data-slot="menu-item"]')?.getAttribute("role"),
).toBe("menuitem");
});
it("dropdown popover resets inherited Surface context like the fixed upstream", async () => {
const Inspector = defineComponent({
setup() {
const ctx = inject(surfaceContextKey, null);
return () =>
h("span", { "data-test": "surface" }, ctx?.variant.value ?? "none");
},
});
const w = mount(
defineComponent({
setup: () => () =>
h(Surface, { variant: "secondary" }, () =>
h(DropdownRoot, { defaultOpen: true, modal: false }, () =>
h(DropdownPopover, {}, () => h(Inspector)),
),
),
}),
{ attachTo: document.body },
);
cleanup.push(() => w.unmount());
await flushPromises();
expect(document.querySelector('[data-test="surface"]')?.textContent).toBe(
"default",
);
});
it("submenu panel preserves a caller keydown listener", async () => {
const keydown = vi.fn();
await render({}, () =>
h(DropdownSubmenuTrigger, {}, () => [
h(DropdownItem, { id: "sub" }, () => [
"Sub",
h(DropdownSubmenuIndicator),
]),
h(DropdownPopover, { onKeydown: keydown }, () =>
h(DropdownMenu, {}, () =>
h(DropdownItem, { id: "inner" }, () => "Inner"),
),
),
]),
);
(document.querySelector('[data-slot="menu-item"]') as HTMLElement).click();
await flushPromises();
const panel = document.querySelector("[data-submenu-content]")!;
expect(panel).not.toBeNull();
panel.dispatchEvent(
new KeyboardEvent("keydown", { key: "a", bubbles: true, cancelable: true }),
);
await flushPromises();
expect(keydown).toHaveBeenCalledTimes(1);
});