test: cover keyboard item activation and cancellation for menu items
This commit is contained in:
@@ -580,3 +580,73 @@ it("Escape on a pointer-opened submenu trigger closes only that level", async ()
|
||||
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();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user