import { afterEach, expect, it, vi } from "vitest"; import { mount, flushPromises } from "@vue/test-utils"; import { defineComponent, h, ref, nextTick } from "vue"; import { Button, DropdownRoot, DropdownTrigger, 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(["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(["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(DropdownTrigger, {}, () => 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(DropdownTrigger, {}, () => 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(); }); function pointer( type: string, props: Record = {}, ): MouseEvent { const event = new MouseEvent(type, { bubbles: true, cancelable: true, ...(props as MouseEventInit), }); for (const key of ["pointerType", "isPrimary"]) { if (key in props) { Object.defineProperty(event, key, { value: props[key] }); } } return event; } function mountLongPress(duration = 300) { const actions: string[] = []; const w = mountApp( () => () => h( DropdownRoot, { trigger: "longPress", longPressDuration: duration }, () => [ h(DropdownTrigger, {}, () => h(Button, { "aria-label": "菜单" }, () => "长按"), ), h(DropdownPopover, {}, () => h(DropdownMenu, { onAction: (k: string) => actions.push(k) }, () => h(DropdownItem, { id: "x", textValue: "X" }, () => h(Label, () => "X"), ), ), ), ], ), ); return { w, actions, button: () => w.get("button").element as HTMLElement }; } function menuState() { return ( document .querySelector('[data-slot="dropdown-popover"]') ?.getAttribute("data-state") ?? null ); } it("longPress: short click on the real trigger does not open", async () => { vi.useFakeTimers(); const { button } = mountLongPress(); await flushPromises(); button().click(); await flushPromises(); await vi.advanceTimersByTimeAsync(600); await nextTick(); expect(menuState()).toBeNull(); vi.useRealTimers(); }); it("longPress: opens at the threshold and stays open after release", async () => { vi.useFakeTimers(); const { button } = mountLongPress(); await flushPromises(); const el = button(); el.dispatchEvent(pointer("pointerdown", { button: 0, pointerType: "mouse" })); await vi.advanceTimersByTimeAsync(200); await nextTick(); expect(menuState()).toBeNull(); await vi.advanceTimersByTimeAsync(150); await nextTick(); expect(menuState()).toBe("open"); el.dispatchEvent(pointer("pointerup", { button: 0, pointerType: "mouse" })); el.click(); await flushPromises(); await nextTick(); expect(menuState()).toBe("open"); vi.useRealTimers(); }); it("longPress: pointercancel and out-of-range movement cancel the pending open", async () => { vi.useFakeTimers(); const { button } = mountLongPress(); await flushPromises(); const el = button(); el.dispatchEvent(pointer("pointerdown", { button: 0, pointerType: "mouse" })); el.dispatchEvent(pointer("pointermove", { clientX: 40, clientY: 0 })); await vi.advanceTimersByTimeAsync(400); await nextTick(); expect(menuState()).toBeNull(); el.dispatchEvent( pointer("pointerdown", { button: 0, clientX: 0, clientY: 0 }), ); el.dispatchEvent(pointer("pointercancel", {})); await vi.advanceTimersByTimeAsync(400); await nextTick(); expect(menuState()).toBeNull(); vi.useRealTimers(); }); it("longPress: non-primary button and non-primary pointer are ignored", async () => { vi.useFakeTimers(); const { button } = mountLongPress(); await flushPromises(); const el = button(); el.dispatchEvent(pointer("pointerdown", { button: 2, pointerType: "mouse" })); await vi.advanceTimersByTimeAsync(400); await nextTick(); expect(menuState()).toBeNull(); el.dispatchEvent(pointer("pointerdown", { button: 0, isPrimary: false })); await vi.advanceTimersByTimeAsync(400); await nextTick(); expect(menuState()).toBeNull(); vi.useRealTimers(); }); it("longPress: Enter and Space open once from the keyboard", async () => { const { w } = mountLongPress(); await flushPromises(); await w.get("button").trigger("keydown", { key: "Enter" }); await flushPromises(); expect(menuState()).toBe("open"); await w.get("button").trigger("keydown", { key: " " }); await flushPromises(); expect(menuState()).toBeNull(); await w.get("button").trigger("keydown", { key: " " }); await flushPromises(); expect(menuState()).toBe("open"); }); it("longPress: unmount clears the pending long-press timer", async () => { vi.useFakeTimers(); const { w } = mountLongPress(); await flushPromises(); (w.get("button").element as HTMLElement).dispatchEvent( pointer("pointerdown", { button: 0 }), ); expect(vi.getTimerCount()).toBeGreaterThan(0); w.unmount(); cleanup.pop(); await nextTick(); expect(vi.getTimerCount()).toBe(0); vi.useRealTimers(); }); function mountSubmenu() { mountApp( () => () => h(DropdownRoot, { defaultOpen: true }, () => [ h(DropdownTrigger, {}, () => 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"), ), ), ), ]), ]), ), ]), ); } function escapeKey(node: Element) { return node.dispatchEvent( new KeyboardEvent("keydown", { key: "Escape", bubbles: true, cancelable: true, }), ); } async function settle() { await flushPromises(); await nextTick(); await new Promise((resolve) => setTimeout(resolve, 30)); await flushPromises(); } async function openSubmenu() { const subTrigger = document.querySelector( '[data-slot="menu-item"][data-has-submenu="true"]', ) as HTMLElement; expect(subTrigger).toBeTruthy(); subTrigger.click(); await flushPromises(); await nextTick(); const subContent = document.querySelector( "[data-submenu-content]", ) as HTMLElement; expect(subContent).toBeTruthy(); return { subTrigger, subContent }; } it("Escape closes the submenu level first, then the root menu", async () => { mountSubmenu(); await flushPromises(); await nextTick(); const { subTrigger, subContent } = await openSubmenu(); const child = subContent.querySelector( '[data-slot="menu-item"]', ) as HTMLElement; child.focus(); expect(document.activeElement).toBe(child); escapeKey(child); await settle(); expect(document.querySelector("[data-submenu-content]")).toBeNull(); expect( document.querySelectorAll('[data-slot="dropdown-popover"]').length, ).toBe(1); expect(document.activeElement).toBe(subTrigger); escapeKey(subTrigger); await settle(); expect(document.querySelector('[data-slot="dropdown-popover"]')).toBeNull(); }); it("Escape on a pointer-opened submenu trigger closes only that level", async () => { mountSubmenu(); await flushPromises(); await nextTick(); const { subTrigger } = await openSubmenu(); // Pointer opening leaves focus on the trigger, outside the sub content. subTrigger.focus(); expect(document.activeElement).toBe(subTrigger); escapeKey(subTrigger); await settle(); expect(document.querySelector("[data-submenu-content]")).toBeNull(); expect( document.querySelectorAll('[data-slot="dropdown-popover"]').length, ).toBe(1); expect(document.activeElement).toBe(subTrigger); escapeKey(subTrigger); await settle(); expect(document.querySelector('[data-slot="dropdown-popover"]')).toBeNull(); }); it("keyboard activation on a focused item fires the action once", async () => { const actions: string[] = []; mountApp( () => () => h(DropdownRoot, { defaultOpen: true }, () => [ h(DropdownTrigger, {}, () => h(Button, () => "菜单")), h(DropdownPopover, {}, () => h(DropdownMenu, { onAction: (k: string) => actions.push(k) }, () => h(DropdownItem, { id: "new-file", textValue: "新建文件" }, () => h(Label, () => "新建文件"), ), ), ), ]), ); await flushPromises(); await nextTick(); const item = document.querySelector('[data-slot="menu-item"]') as HTMLElement; item.focus(); expect(document.activeElement).toBe(item); item.dispatchEvent( new KeyboardEvent("keydown", { key: "Enter", bubbles: true, cancelable: true, }), ); await flushPromises(); await nextTick(); expect(actions).toEqual(["new-file"]); }); it("keyboard activation is cancelled when the item click is prevented", async () => { const actions: string[] = []; mountApp( () => () => h(DropdownRoot, { defaultOpen: true }, () => [ h(DropdownTrigger, {}, () => h(Button, () => "菜单")), h(DropdownPopover, {}, () => h(DropdownMenu, { onAction: (k: string) => actions.push(k) }, () => h( DropdownItem, { id: "new-file", textValue: "新建文件", onClick: (e: MouseEvent) => e.preventDefault(), }, () => h(Label, () => "新建文件"), ), ), ), ]), ); await flushPromises(); await nextTick(); const item = document.querySelector('[data-slot="menu-item"]') as HTMLElement; item.focus(); item.dispatchEvent( new KeyboardEvent("keydown", { key: "Enter", bubbles: true, cancelable: true, }), ); await flushPromises(); await nextTick(); expect(actions).toEqual([]); expect(document.querySelector('[data-slot="menu-item"]')).not.toBeNull(); });