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, 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); });