163 lines
5.0 KiB
TypeScript
163 lines
5.0 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();
|
|
});
|