390 lines
13 KiB
TypeScript
390 lines
13 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);
|
|
});
|
|
|
|
it("default action activation closes its dropdown", async () => {
|
|
await render({}, () => h(DropdownItem, { id: "default" }, () => "Action"));
|
|
(document.querySelector('[data-slot="menu-item"]') as HTMLElement).click();
|
|
await flushPromises();
|
|
expect(document.querySelector('[role="menu"]')).toBeNull();
|
|
});
|
|
it.each([
|
|
["single", false, true],
|
|
["multiple", true, false],
|
|
] as const)(
|
|
"menu-level close policy for %s is honoured",
|
|
async (selectionMode, shouldCloseOnSelect, staysOpen) => {
|
|
await render({ selectionMode, shouldCloseOnSelect }, () =>
|
|
h(DropdownItem, { id: "policy" }, () => "Policy"),
|
|
);
|
|
(document.querySelector('[data-slot="menu-item"]') as HTMLElement).click();
|
|
await flushPromises();
|
|
expect(!!document.querySelector('[role="menu"]')).toBe(staysOpen);
|
|
},
|
|
);
|
|
it("root popover preserves a user keydown callback", async () => {
|
|
const callback = vi.fn();
|
|
const w = mount(
|
|
defineComponent({
|
|
setup: () => () =>
|
|
h(DropdownRoot, { defaultOpen: true, modal: false }, () =>
|
|
h(DropdownPopover, { onKeydown: callback }, () =>
|
|
h(DropdownMenu, {}, () => h(DropdownItem, { id: "a" }, () => "A")),
|
|
),
|
|
),
|
|
}),
|
|
{ attachTo: document.body },
|
|
);
|
|
cleanup.push(() => w.unmount());
|
|
await flushPromises();
|
|
document.querySelector('[role="menu"]')!.dispatchEvent(
|
|
new KeyboardEvent("keydown", {
|
|
key: "a",
|
|
bubbles: true,
|
|
cancelable: true,
|
|
}),
|
|
);
|
|
await flushPromises();
|
|
expect(callback).toHaveBeenCalledTimes(1);
|
|
});
|
|
async function openConfiguredSubmenu(props: Record<string, unknown>) {
|
|
await render({}, () =>
|
|
h(DropdownSubmenuTrigger, {}, () => [
|
|
h(DropdownItem, { id: "sub" }, () => [
|
|
"Sub",
|
|
h(DropdownSubmenuIndicator),
|
|
]),
|
|
h(DropdownPopover, props, () =>
|
|
h(DropdownMenu, {}, () =>
|
|
h(DropdownItem, { id: "inner" }, () => "Inner"),
|
|
),
|
|
),
|
|
]),
|
|
);
|
|
(document.querySelector('[data-key="sub"]') as HTMLElement).click();
|
|
await flushPromises();
|
|
return document.querySelector("[data-submenu-content]") as HTMLElement;
|
|
}
|
|
it("submenu preserves the panel's data and style attributes", async () => {
|
|
const panel = await openConfiguredSubmenu({
|
|
"data-custom": "kept",
|
|
style: { borderTopWidth: "7px" },
|
|
});
|
|
expect(panel.getAttribute("data-custom")).toBe("kept");
|
|
expect(panel.style.borderTopWidth).toBe("7px");
|
|
});
|
|
it("submenu honours isKeyboardDismissDisabled", async () => {
|
|
const panel = await openConfiguredSubmenu({
|
|
isKeyboardDismissDisabled: true,
|
|
});
|
|
const item = panel.querySelector('[data-slot="menu-item"]') as HTMLElement;
|
|
item.focus();
|
|
item.dispatchEvent(
|
|
new KeyboardEvent("keydown", {
|
|
key: "Escape",
|
|
bubbles: true,
|
|
cancelable: true,
|
|
}),
|
|
);
|
|
await flushPromises();
|
|
expect(document.querySelector("[data-submenu-content]")).not.toBeNull();
|
|
});
|
|
it("standalone Menu keeps its own accessible name", async () => {
|
|
const w = mount(Menu, {
|
|
attrs: { "aria-label": "Standalone actions" },
|
|
slots: { default: () => h(DropdownItem, { id: "a" }, () => "A") },
|
|
attachTo: document.body,
|
|
});
|
|
cleanup.push(() => w.unmount());
|
|
await flushPromises();
|
|
expect(
|
|
document.querySelector('[role="menu"]')?.getAttribute("aria-label"),
|
|
).toBe("Standalone actions");
|
|
});
|
|
|
|
it("submenu keeps a directly supplied panel accessible name", async () => {
|
|
const panel = await openConfiguredSubmenu({
|
|
"aria-label": "Direct submenu name",
|
|
"aria-describedby": "explicit-description",
|
|
});
|
|
expect(panel.getAttribute("aria-label")).toBe("Direct submenu name");
|
|
expect(panel.getAttribute("aria-describedby")).toBe("explicit-description");
|
|
});
|
|
it("submenu preserves a caller's openAutoFocus callback", async () => {
|
|
const callback = vi.fn();
|
|
await openConfiguredSubmenu({ onOpenAutoFocus: callback });
|
|
expect(callback).toHaveBeenCalledTimes(1);
|
|
});
|
|
it("submenu respects the menu's shouldFocusWrap navigation", async () => {
|
|
await render({}, () =>
|
|
h(DropdownSubmenuTrigger, {}, () => [
|
|
h(DropdownItem, { id: "sub" }, () => [
|
|
"Sub",
|
|
h(DropdownSubmenuIndicator),
|
|
]),
|
|
h(DropdownPopover, {}, () =>
|
|
h(DropdownMenu, { shouldFocusWrap: true }, () => [
|
|
h(DropdownItem, { id: "first", textValue: "First" }, () => "First"),
|
|
h(DropdownItem, { id: "last", textValue: "Last" }, () => "Last"),
|
|
]),
|
|
),
|
|
]),
|
|
);
|
|
(document.querySelector('[data-key="sub"]') as HTMLElement).click();
|
|
await flushPromises();
|
|
const panel = document.querySelector("[data-submenu-content]")!;
|
|
const first = panel.querySelector('[data-key="first"]') as HTMLElement;
|
|
const last = panel.querySelector('[data-key="last"]') as HTMLElement;
|
|
first.focus();
|
|
await flushPromises();
|
|
first.dispatchEvent(
|
|
new KeyboardEvent("keydown", {
|
|
key: "ArrowUp",
|
|
bubbles: true,
|
|
cancelable: true,
|
|
}),
|
|
);
|
|
await flushPromises();
|
|
expect(document.activeElement).toBe(last);
|
|
});
|