From 7eb221329b04ed63d51409da8347338dfe753805 Mon Sep 17 00:00:00 2001 From: Matthew Lipski Date: Mon, 30 Nov 2026 15:40:01 +0100 Subject: [PATCH] Added suggestion menu tests --- .../SuggestionMenu.browser.test.tsx | 349 +++++++++++++++++ .../SuggestionMenu/SuggestionMenu.test.ts | 40 -- .../SuggestionMenuController.browser.test.tsx | 362 ++++++++++++++++++ .../SuggestionMenuWrapper.browser.test.tsx | 234 ++++++++--- tests/vite.config.browser.ts | 3 + 5 files changed, 901 insertions(+), 87 deletions(-) create mode 100644 packages/react/src/components/SuggestionMenu/SuggestionMenu.browser.test.tsx delete mode 100644 packages/react/src/components/SuggestionMenu/SuggestionMenu.test.ts create mode 100644 packages/react/src/components/SuggestionMenu/SuggestionMenuController.browser.test.tsx diff --git a/packages/react/src/components/SuggestionMenu/SuggestionMenu.browser.test.tsx b/packages/react/src/components/SuggestionMenu/SuggestionMenu.browser.test.tsx new file mode 100644 index 0000000000..a4c844d1ed --- /dev/null +++ b/packages/react/src/components/SuggestionMenu/SuggestionMenu.browser.test.tsx @@ -0,0 +1,349 @@ +import { assert, describe, expect, vi } from "vite-plus/test"; + +import { getDefaultReactSlashMenuItems } from "./getDefaultReactSlashMenuItems.js"; +import { SuggestionMenu } from "./SuggestionMenu.js"; +import type { DefaultReactSuggestionItem } from "./types.js"; + +const props = { slashMenu: false, sideMenu: false, formattingToolbar: false }; +const items: DefaultReactSuggestionItem[] = [ + { + title: "First", + subtext: "First description", + group: "Basic", + onItemClick() {}, + }, + { title: "Second", group: "Basic", onItemClick() {} }, + { title: "Third", group: "Advanced", onItemClick() {} }, +]; + +describe("SuggestionMenu", () => { + describe("unit", () => { + it("visually highlights only the selectedIndex item and clears the previous highlight", async ({ + setupEditor, + page, + }) => { + const { rerender } = await setupEditor({ + props, + children: ( + + ), + }); + // Keep pointer hover from contributing to the selection styling. + await page.getByRole("textbox").hover(); + const options = ["First First description", "Second", "Third"].map( + (name) => page.getByRole("option", { name, exact: true }), + ); + const backgrounds = options.map( + (option) => getComputedStyle(option.element()).backgroundColor, + ); + for (const selectedIndex of [0, 1, 2, undefined]) { + rerender({ + children: ( + + ), + }); + for (const [index, option] of options.entries()) { + if (index === selectedIndex) { + await expect + .element(option) + .toHaveAttribute("aria-selected", "true"); + await expect + .poll(() => getComputedStyle(option.element()).backgroundColor) + .not.toBe(backgrounds[index]); + expect(getComputedStyle(option.element()).backgroundColor).not.toBe( + "rgba(0, 0, 0, 0)", + ); + } else { + await expect + .element(option) + .not.toHaveAttribute("aria-selected", "true"); + await expect + .poll(() => getComputedStyle(option.element()).backgroundColor) + .toBe(backgrounds[index]); + } + } + } + }); + + it.for(items)( + "passes the clicked $title item to the supplied callback without moving editor focus", + async (item, { setupEditor, page }) => { + const onItemClick = vi.fn(); + const { editor } = await setupEditor({ + props, + children: ( + + ), + }); + editor.focus(); + const before = editor.document; + const option = page.getByRole("option", { + name: [item.title, item.subtext].filter(Boolean).join(" "), + exact: true, + }); + await option.hover(); + await option.click(); + expect(onItemClick).toHaveBeenCalledExactlyOnceWith(item); + await expect.element(page.getByRole("textbox")).toHaveFocus(); + expect(editor.document).toEqual(before); + }, + ); + + it("groups items in order", async ({ setupEditor, page }) => { + await setupEditor({ + props, + children: ( + + ), + }); + const menu = page.getByRole("listbox"); + await expect.element(menu.getByRole("option")).toHaveLength(3); + await expect + .element(menu.getByText("Basic", { exact: true })) + .toHaveLength(1); + await expect + .element(menu.getByText("Advanced", { exact: true })) + .toHaveLength(1); + }); + + it.for([ + { loadingState: "loading-initial", loader: true, empty: false }, + { loadingState: "loading", loader: true, empty: true }, + { loadingState: "loaded", loader: false, empty: true }, + ] as const)( + "renders an empty $loadingState menu", + async ({ loadingState, loader, empty }, { setupEditor, page }) => { + const { editor } = await setupEditor({ + props, + children: ( + + ), + }); + const menu = page.getByRole("listbox"); + await expect.element(menu).toBeVisible(); + const loaderElement = menu + .element() + .querySelector(".bn-suggestion-menu-loader"); + if (loader) { + assert(loaderElement); + await expect + .element(page.elementLocator(loaderElement)) + .toBeVisible(); + } else { + expect(loaderElement).toBeNull(); + } + const emptyItem = menu.getByText( + editor.dictionary.suggestion_menu.no_items_title, + { exact: true }, + ); + if (empty) { + await expect.element(emptyItem).toBeVisible(); + } else { + await expect.element(emptyItem).not.toBeInTheDocument(); + } + await expect.element(menu.getByRole("option")).toHaveLength(0); + }, + ); + + it("keeps existing items visible while reloading and removes the loader when loaded", async ({ + setupEditor, + page, + }) => { + const { editor, rerender } = await setupEditor({ + props, + children: ( + + ), + }); + const menu = page.getByRole("listbox"); + await expect.element(menu.getByRole("option")).toHaveLength(3); + expect( + menu.element().querySelector(".bn-suggestion-menu-loader"), + ).toBeNull(); + rerender({ + children: ( + + ), + }); + const loader = menu.element().querySelector(".bn-suggestion-menu-loader"); + assert(loader); + await expect.element(page.elementLocator(loader)).toBeVisible(); + await expect.element(menu.getByRole("option")).toHaveLength(3); + await expect + .element( + menu.getByText(editor.dictionary.suggestion_menu.no_items_title), + ) + .not.toBeInTheDocument(); + rerender({ + children: ( + + ), + }); + await expect.element(menu.getByRole("option")).toHaveLength(1); + await expect + .element(menu.getByRole("option", { name: "Second", exact: true })) + .toBeVisible(); + expect( + menu.element().querySelector(".bn-suggestion-menu-loader"), + ).toBeNull(); + }); + + it("visually highlights hovered items and clears the highlight when the pointer leaves", async ({ + setupEditor, + page, + }) => { + const { editor } = await setupEditor({ + props, + children: ( + + ), + }); + editor.focus(); + const textbox = page.getByRole("textbox"); + await textbox.hover(); + for (const name of ["First First description", "Second", "Third"]) { + const option = page.getByRole("option", { name, exact: true }); + const background = getComputedStyle(option.element()).backgroundColor; + await option.hover(); + await expect + .poll(() => getComputedStyle(option.element()).backgroundColor) + .not.toBe(background); + expect(getComputedStyle(option.element()).backgroundColor).not.toBe( + "rgba(0, 0, 0, 0)", + ); + await expect.element(textbox).toHaveFocus(); + await textbox.hover(); + await expect + .poll(() => getComputedStyle(option.element()).backgroundColor) + .toBe(background); + } + }); + }); + + describe("integration", () => { + it("clicking an item invokes its editor action", async ({ + setupEditor, + page, + }) => { + const { editor } = await setupEditor({ + initialContent: [ + { id: "target", type: "paragraph" }, + { id: "sibling", type: "paragraph", content: "Keep me" }, + ], + props, + children: ({ editor }) => ( + item.onItemClick()} + /> + ), + }); + editor.setTextCursorPosition("target", "start"); + editor.focus(); + const sibling = editor.getBlock("sibling"); + const option = page.getByRole("option", { name: /^Heading 2 / }); + await option.hover(); + await option.click(); + expect(editor.getBlock("target")).toMatchObject({ + type: "heading", + props: { level: 2 }, + content: [], + }); + expect(editor.getBlock("sibling")).toEqual(sibling); + await expect.element(page.getByRole("textbox")).toHaveFocus(); + }); + }); + + describe("aria", () => { + it("exposes item names, descriptions, grouping, and the selected option", async ({ + setupEditor, + page, + }) => { + await setupEditor({ + props, + children: ( + + ), + }); + await expect.element(page.getByRole("listbox")) + .toMatchAriaInlineSnapshot(` + - listbox "Suggestions": + - text: Basic + - option "First First description": + - paragraph: First + - paragraph: First description + - option "Second" [selected]: + - paragraph: Second + - paragraph + - text: Advanced + - option "Third": + - paragraph: Third + - paragraph + `); + }); + }); + describe("axe", () => { + it("has no automated violations for a short suggestion list", async ({ + setupEditor, + page, + runAxe, + }) => { + await setupEditor({ + props, + children: ( + + ), + }); + const menu = page.getByRole("listbox"); + await expect.element(menu).toBeVisible(); + expect((await runAxe(menu)).violations).toEqual([]); + }); + }); +}); diff --git a/packages/react/src/components/SuggestionMenu/SuggestionMenu.test.ts b/packages/react/src/components/SuggestionMenu/SuggestionMenu.test.ts deleted file mode 100644 index 999b6dd977..0000000000 --- a/packages/react/src/components/SuggestionMenu/SuggestionMenu.test.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { assertType, expect, expectTypeOf, it } from "vite-plus/test"; -import { getSuggestionMenuItemId } from "./getSuggestionMenuItemId.js"; -import type { SuggestionMenuController } from "./SuggestionMenuController.js"; - -type MenuProps Promise> = - Parameters>[0]; - -it("requires a custom renderer only for custom suggestion items", () => { - const getDefaultItems = async () => [ - { title: "hello", onItemClick: () => undefined }, - ]; - assertType>({ - getItems: getDefaultItems, - triggerCharacter: "/", - }); - - const getCustomItems = async () => [{ name: "hello" }]; - const customProps = { - getItems: getCustomItems, - triggerCharacter: "/", - }; - // @ts-expect-error Custom items require a custom renderer. - assertType>({ - ...customProps, - onItemClick: () => undefined, - }); - assertType>({ - ...customProps, - suggestionMenuComponent: () => null, - onItemClick: (item) => { - expectTypeOf(item).toEqualTypeOf<{ name: string }>(); - }, - }); -}); - -it("returns an active descendant id for the first suggestion", () => { - expect(getSuggestionMenuItemId(0)).toBe("bn-suggestion-menu-item-0"); - expect(getSuggestionMenuItemId(2)).toBe("bn-suggestion-menu-item-2"); - expect(getSuggestionMenuItemId(undefined)).toBeUndefined(); -}); diff --git a/packages/react/src/components/SuggestionMenu/SuggestionMenuController.browser.test.tsx b/packages/react/src/components/SuggestionMenu/SuggestionMenuController.browser.test.tsx new file mode 100644 index 0000000000..c918c6cc86 --- /dev/null +++ b/packages/react/src/components/SuggestionMenu/SuggestionMenuController.browser.test.tsx @@ -0,0 +1,362 @@ +import { SuggestionMenu as SuggestionMenuExtension } from "@blocknote/core/extensions"; +import { + assert, + assertType, + describe, + expect, + expectTypeOf, + vi, +} from "vite-plus/test"; + +import { SuggestionMenuController } from "./SuggestionMenuController.js"; + +type MenuProps Promise> = + Parameters>[0]; + +const props = { slashMenu: false, sideMenu: false, formattingToolbar: false }; + +async function getItems(query: string) { + return ["First", "Second", "Third"] + .filter((title) => title.toLowerCase().includes(query.toLowerCase())) + .map((title) => ({ title, onItemClick() {} })); +} + +const controller = ( + +); + +describe("SuggestionMenuController", () => { + describe("unit", () => { + it("requires a custom renderer only for custom suggestion items", () => { + async function getDefaultItems() { + return [{ title: "hello", onItemClick() {} }]; + } + assertType>({ + getItems: getDefaultItems, + triggerCharacter: "/", + }); + + async function getCustomItems() { + return [{ name: "hello" }]; + } + const customProps = { + getItems: getCustomItems, + triggerCharacter: "/", + }; + // @ts-expect-error Custom items require a custom renderer. + assertType>({ + ...customProps, + onItemClick: () => undefined, + }); + assertType>({ + ...customProps, + suggestionMenuComponent: () => null, + onItemClick: (item) => { + expectTypeOf(item).toEqualTypeOf<{ name: string }>(); + }, + }); + }); + + it("supports a custom trigger, items, renderer, and activation callback", async ({ + setupEditor, + page, + userEvent, + pressKey, + }) => { + const onItemClick = vi.fn(); + const item = { name: "Mention" }; + const { editor } = await setupEditor({ + props, + children: ( + [item]} + onItemClick={onItemClick} + suggestionMenuComponent={({ items, selectedIndex }) => ( +
{items[selectedIndex ?? 0]?.name}
+ )} + /> + ), + }); + editor.focus(); + await userEvent.keyboard("@"); + await expect + .element(page.getByText("Mention", { exact: true })) + .toBeVisible(); + await pressKey("Enter"); + expect(onItemClick).toHaveBeenCalledExactlyOnceWith(item); + await expect + .element(page.getByText("Mention", { exact: true })) + .not.toBeInTheDocument(); + await expect.element(page.getByRole("textbox")).toHaveFocus(); + }); + }); + + describe("integration", () => { + it("waits for the minimum query length before showing suggestions", async ({ + setupEditor, + page, + userEvent, + }) => { + const { editor } = await setupEditor({ + props, + children: ( + + ), + }); + editor.focus(); + await userEvent.keyboard("/s"); + await expect.element(page.getByRole("listbox")).not.toBeInTheDocument(); + await userEvent.keyboard("e"); + await expect + .element(page.getByRole("option", { name: "Second", exact: true })) + .toBeVisible(); + }); + + it("renders into the supplied portal", async ({ + setupEditor, + page, + onTestFinished, + userEvent, + }) => { + const portal = document.createElement("div"); + document.body.append(portal); + onTestFinished(() => portal.remove()); + const { editor } = await setupEditor({ + props, + children: ( + + ), + }); + editor.focus(); + await userEvent.keyboard("/"); + const menu = page.getByRole("listbox"); + await expect.element(menu).toBeVisible(); + expect(portal.contains(menu.element())).toBe(true); + }); + + it("respects read-only mode when opening and after editability changes", async ({ + setupEditor, + page, + }) => { + const { editor, rerender } = await setupEditor({ + props: { ...props, editable: false }, + children: controller, + }); + const extension = editor.getExtension(SuggestionMenuExtension); + assert(extension); + const before = editor.document; + extension.openSuggestionMenu("/"); + await expect.element(page.getByRole("listbox")).not.toBeInTheDocument(); + rerender({ editable: true }); + editor.focus(); + extension.openSuggestionMenu("/"); + await expect.element(page.getByRole("listbox")).toBeVisible(); + rerender({ editable: false }); + await expect.element(page.getByRole("listbox")).not.toBeInTheDocument(); + await expect + .element(page.getByRole("textbox")) + .not.toHaveAttribute("aria-controls"); + await expect + .element(page.getByRole("textbox")) + .not.toHaveAttribute("aria-activedescendant"); + rerender({ editable: true }); + await expect.element(page.getByRole("listbox")).not.toBeInTheDocument(); + editor.focus(); + extension.openSuggestionMenu("/"); + await expect.element(page.getByRole("listbox")).toBeVisible(); + expect(editor.document).toEqual(before); + }); + }); + + describe("axe", () => { + it("has only the expected scroll-region false positive on the open menu", async ({ + setupEditor, + page, + userEvent, + runAxe, + }) => { + const { editor } = await setupEditor({ + props, + children: , + domAttributes: { editor: { "aria-label": "Document" } }, + }); + const textbox = page.getByRole("textbox"); + expect((await runAxe(textbox)).violations).toEqual([]); + editor.focus(); + await userEvent.keyboard("/"); + const menu = page.getByRole("listbox"); + await expect.element(menu.getByRole("option").first()).toBeVisible(); + expect((await runAxe(textbox)).violations).toEqual([]); + // axe only exempts combobox popups from this rule. Our textbox drives + // scrolling via aria-activedescendant; the keyboard tests below verify + // that navigation scrolls the selected option into view. + // https://github.com/dequelabs/axe-core/blob/v4.13.0/lib/rules/scrollable-region-focusable-matches.js + const { violations } = await runAxe(menu); + expect( + violations.map(({ id, nodes }) => ({ + id, + nodes: nodes.map(({ target, any, all, none }) => ({ + target, + any: any.map(({ id }) => id), + all: all.map(({ id }) => id), + none: none.map(({ id }) => id), + })), + })), + ).toEqual([ + { + id: "scrollable-region-focusable", + nodes: [ + { + target: ["#bn-suggestion-menu"], + any: ["focusable-content", "focusable-element"], + all: [], + none: [], + }, + ], + }, + ]); + }); + }); + + describe("keyboard", () => { + it("Enter inserts the selected default block and clears the query", async ({ + setupEditor, + page, + userEvent, + pressKey, + }) => { + const { editor } = await setupEditor({ + initialContent: [ + { id: "target", type: "paragraph" }, + { id: "sibling", type: "paragraph", content: "Keep me" }, + ], + props, + children: , + }); + editor.setTextCursorPosition("target", "start"); + editor.focus(); + const sibling = editor.getBlock("sibling"); + await userEvent.keyboard("/heading 2"); + const option = page.getByRole("option", { name: /^Heading 2 / }); + await expect.element(option).toBeVisible(); + await pressKey("Enter"); + await expect.element(page.getByRole("listbox")).not.toBeInTheDocument(); + expect(editor.getBlock("target")).toMatchObject({ + type: "heading", + props: { level: 2 }, + content: [], + }); + expect(editor.getBlock("sibling")).toEqual(sibling); + await expect.element(page.getByRole("textbox")).toHaveFocus(); + }); + + it("scrolls the first and last options into view without moving editor focus", async ({ + setupEditor, + page, + userEvent, + pressKey, + }) => { + const { editor } = await setupEditor({ + props, + children: , + }); + editor.focus(); + await userEvent.keyboard("/"); + const menu = page.getByRole("listbox"); + const options = menu.getByRole("option"); + await expect.element(options.first()).toBeVisible(); + expect(menu.element().scrollHeight).toBeGreaterThan( + menu.element().clientHeight, + ); + const before = editor.document; + for (const [key, option] of [ + ["PageDown", options.last()], + ["PageUp", options.first()], + ] as const) { + await pressKey(key); + await expect.element(option).toHaveAttribute("aria-selected", "true"); + await expect + .poll(() => { + const itemRect = option.element().getBoundingClientRect(); + const menuRect = menu.element().getBoundingClientRect(); + return ( + itemRect.top >= menuRect.top && itemRect.bottom <= menuRect.bottom + ); + }) + .toBe(true); + await expect.element(page.getByRole("textbox")).toHaveFocus(); + expect(editor.document).toEqual(before); + } + }); + + it("dismisses with Escape without changing content and removes ARIA references", async ({ + setupEditor, + page, + userEvent, + pressKey, + }) => { + const { editor } = await setupEditor({ props, children: controller }); + editor.focus(); + await userEvent.keyboard("/s"); + await expect.element(page.getByRole("listbox")).toBeVisible(); + const before = editor.document; + await pressKey("Escape"); + const textbox = page.getByRole("textbox"); + await expect.element(page.getByRole("listbox")).not.toBeInTheDocument(); + await expect.element(textbox).toHaveFocus(); + await expect.element(textbox).not.toHaveAttribute("aria-expanded"); + await expect.element(textbox).not.toHaveAttribute("aria-controls"); + await expect + .element(textbox) + .not.toHaveAttribute("aria-activedescendant"); + expect(editor.document).toEqual(before); + }); + + it("allows Tab to leave the editor without focusing or activating an option", async ({ + setupEditor, + page, + userEvent, + pressKey, + }) => { + const onItemClick = vi.fn(); + const { editor } = await setupEditor({ + props, + children: ( + + ), + outside: ({ children }) => ( + <> + {children} + + + ), + }); + editor.focus(); + await userEvent.keyboard("/"); + await expect.element(page.getByRole("option")).toHaveLength(3); + const before = editor.document; + await pressKey("Tab"); + await expect + .element( + page.getByRole("button", { name: "After editor", exact: true }), + ) + .toHaveFocus(); + await expect.element(page.getByRole("listbox")).not.toBeInTheDocument(); + expect(onItemClick).not.toHaveBeenCalled(); + expect(editor.document).toEqual(before); + }); + }); +}); diff --git a/packages/react/src/components/SuggestionMenu/SuggestionMenuWrapper.browser.test.tsx b/packages/react/src/components/SuggestionMenu/SuggestionMenuWrapper.browser.test.tsx index 36e6caecd4..af84a12d48 100644 --- a/packages/react/src/components/SuggestionMenu/SuggestionMenuWrapper.browser.test.tsx +++ b/packages/react/src/components/SuggestionMenu/SuggestionMenuWrapper.browser.test.tsx @@ -1,15 +1,22 @@ -import { describe, expect } from "vite-plus/test"; +import { describe, expect, vi } from "vite-plus/test"; import { SuggestionMenu } from "./SuggestionMenu.js"; import { SuggestionMenuWrapper } from "./SuggestionMenuWrapper.js"; +const props = { + slashMenu: false, + sideMenu: false, + formattingToolbar: false, + emojiPicker: false, +}; + async function getItems(query: string) { - return query === "x" - ? [] - : ["First", "Second", "Third"].map((title) => ({ - title, - onItemClick: doNothing, - })); + return ["First", "Second", "Third"] + .filter((title) => title.toLowerCase().includes(query.toLowerCase())) + .map((title) => ({ + title, + onItemClick: doNothing, + })); } function doNothing() {} @@ -27,52 +34,185 @@ function renderWrapper(query: string) { } describe("SuggestionMenuWrapper", () => { - it("references the active option and removes its references when closed", async ({ - setupEditor, - page, - pressKey, - }) => { - const { editor, rootNode, rerender } = await setupEditor({ - props: { slashMenu: false, emojiPicker: false }, - children: renderWrapper(""), - }); - const editable = page.elementLocator(rootNode).getByRole("textbox"); - const menu = page.getByRole("listbox"); + describe("aria", () => { + it("references the active option and removes its references when closed", async ({ + setupEditor, + page, + }) => { + const { rootNode, rerender } = await setupEditor({ + props, + children: renderWrapper(""), + }); + const editable = page.elementLocator(rootNode).getByRole("textbox"); + const menu = page.getByRole("listbox"); - async function expectActiveOption(name: string) { - const option = menu.getByRole("option", { name, exact: true }); - await expect.element(option).toHaveAttribute("aria-selected", "true"); - expect(option.element().id).not.toBe(""); + async function expectActiveOption(name: string) { + const option = menu.getByRole("option", { name, exact: true }); + await expect.element(option).toHaveAttribute("aria-selected", "true"); + expect(option.element().id).not.toBe(""); + await expect + .poll(() => editable.element().getAttribute("aria-activedescendant")) + .toBe(option.element().id); + } + + await expectActiveOption("First"); + expect(menu.element().id).not.toBe(""); await expect - .poll(() => editable.element().getAttribute("aria-activedescendant")) - .toBe(option.element().id); - } + .element(editable) + .toHaveAttribute("aria-controls", menu.element().id); - await expectActiveOption("First"); - expect(menu.element().id).not.toBe(""); - await expect - .element(editable) - .toHaveAttribute("aria-controls", menu.element().id); + rerender({ children: renderWrapper("x") }); + await expect + .element(editable) + .not.toHaveAttribute("aria-activedescendant"); + await expect.element(menu.getByRole("option")).not.toBeInTheDocument(); + // An empty result clears the selection without unmounting the wrapper. + await expect + .element(editable) + .toHaveAttribute("aria-controls", menu.element().id); - editor.focus(); - await pressKey("PageDown"); - await expectActiveOption("Third"); - await expect.element(editable).toHaveFocus(); + rerender({ children: renderWrapper("") }); + await expectActiveOption("First"); - rerender({ children: renderWrapper("x") }); - await expect.element(editable).not.toHaveAttribute("aria-activedescendant"); - await expect.element(menu.getByRole("option")).not.toBeInTheDocument(); - // An empty result clears the selection without unmounting the wrapper. - await expect.element(editable).toHaveAttribute("aria-expanded", "true"); + // Remove just the wrapper so the editor stays available to observe cleanup. + rerender({ children: null }); + await expect.element(menu).not.toBeInTheDocument(); + await expect + .element(editable) + .not.toHaveAttribute("aria-activedescendant"); + await expect.element(editable).not.toHaveAttribute("aria-controls"); + await expect.element(editable).not.toHaveAttribute("aria-expanded"); + }); + }); - rerender({ children: renderWrapper("") }); - await expectActiveOption("First"); + describe("keyboard", () => { + it("navigates and wraps the active descendant while focus and selection stay in the editor", async ({ + setupEditor, + page, + pressKey, + readAriaSnapshot, + }) => { + const { editor } = await setupEditor({ + props, + children: renderWrapper(""), + }); + editor.focus(); + const textbox = page.getByRole("textbox"); + const menu = page.getByRole("listbox"); + await expect.element(menu.getByRole("option")).toHaveLength(3); + await expect + .element(textbox) + .toHaveAttribute("aria-controls", menu.element().id); + const before = editor.document; + const selection = window.getSelection(); + const anchorNode = selection?.anchorNode; + const anchorOffset = selection?.anchorOffset; + const sequence: string[] = []; + for (const [key, name] of [ + [undefined, "First"], + ["ArrowDown", "Second"], + ["ArrowDown", "Third"], + ["ArrowDown", "First"], + ["ArrowUp", "Third"], + ["PageUp", "First"], + ["PageDown", "Third"], + ] as const) { + if (key) { + await pressKey(key); + } + const option = menu.getByRole("option", { name, exact: true }); + await expect.element(option).toHaveAttribute("aria-selected", "true"); + await expect + .element(menu.getByRole("option", { selected: true })) + .toHaveLength(1); + await expect + .element(textbox) + .toHaveAttribute("aria-activedescendant", option.element().id); + await expect.element(textbox).toHaveFocus(); + expect(window.getSelection()?.anchorNode).toBe(anchorNode); + expect(window.getSelection()?.anchorOffset).toBe(anchorOffset); + expect(editor.document).toEqual(before); + sequence.push(readAriaSnapshot(option)); + } + expect(sequence).toEqual( + ["First", "Second", "Third", "First", "Third", "First", "Third"].map( + (name) => + `- option "${name}" [selected]:\n - paragraph: ${name}\n - paragraph`, + ), + ); + }); - // Remove just the wrapper so the editor stays available to observe cleanup. - rerender({ children: null }); - await expect.element(menu).not.toBeInTheDocument(); - await expect.element(editable).not.toHaveAttribute("aria-activedescendant"); - await expect.element(editable).not.toHaveAttribute("aria-controls"); - await expect.element(editable).toHaveAttribute("aria-expanded", "false"); + it("resets the active option when the query changes", async ({ + setupEditor, + page, + pressKey, + }) => { + const { editor, rerender } = await setupEditor({ + props, + children: renderWrapper(""), + }); + editor.focus(); + await expect.element(page.getByRole("option")).toHaveLength(3); + await pressKey("PageDown"); + await expect + .element(page.getByRole("option", { name: "Third", exact: true })) + .toHaveAttribute("aria-selected", "true"); + const before = editor.document; + rerender({ children: renderWrapper("s") }); + await expect.element(page.getByRole("option")).toHaveLength(2); + const first = page.getByRole("option", { name: "First", exact: true }); + await expect.element(first).toHaveAttribute("aria-selected", "true"); + await expect + .element(page.getByRole("textbox")) + .toHaveAttribute("aria-activedescendant", first.element().id); + await expect.element(page.getByRole("textbox")).toHaveFocus(); + expect(editor.document).toEqual(before); + }); + it("Enter closes and clears the query before activating the selected item", async ({ + setupEditor, + page, + pressKey, + }) => { + const calls: string[] = []; + const closeMenu = vi.fn(() => { + calls.push("close"); + }); + const clearQuery = vi.fn(() => { + calls.push("clear"); + }); + const onItemClick = vi.fn(() => { + calls.push("activate"); + }); + const { editor } = await setupEditor({ + props, + children: ( + + ), + }); + editor.focus(); + const before = editor.document; + await expect.element(page.getByRole("option")).toHaveLength(3); + await pressKey("ArrowDown"); + await expect + .element(page.getByRole("option", { name: "Second", exact: true })) + .toHaveAttribute("aria-selected", "true"); + expect(calls).toEqual([]); + await pressKey("Enter"); + expect(onItemClick).toHaveBeenCalledExactlyOnceWith( + expect.objectContaining({ title: "Second" }), + ); + expect(closeMenu).toHaveBeenCalledExactlyOnceWith(); + expect(clearQuery).toHaveBeenCalledExactlyOnceWith(); + expect(calls).toEqual(["close", "clear", "activate"]); + await expect.element(page.getByRole("textbox")).toHaveFocus(); + expect(editor.document).toEqual(before); + }); }); }); diff --git a/tests/vite.config.browser.ts b/tests/vite.config.browser.ts index 50868a0673..683aa39b20 100644 --- a/tests/vite.config.browser.ts +++ b/tests/vite.config.browser.ts @@ -83,6 +83,9 @@ const LOCAL_BROWSER_FILES = [ "../packages/react/src/components/FormattingToolbar/DefaultButtons/ColorStyleButton.browser.test.tsx", "../packages/react/src/components/FormattingToolbar/DefaultButtons/CreateLinkButton.browser.test.tsx", "../packages/react/src/components/FormattingToolbar/DefaultButtons/TextAlignButton.browser.test.tsx", + "../packages/react/src/components/SuggestionMenu/SuggestionMenu.browser.test.tsx", + "../packages/react/src/components/SuggestionMenu/SuggestionMenuController.browser.test.tsx", + "../packages/react/src/components/SuggestionMenu/SuggestionMenuWrapper.browser.test.tsx", "../packages/react/src/hooks/useSelectedBlocks.browser.test.tsx", "../packages/react/src/schema/ReactBlockSpec.browser.test.tsx", "../packages/react/src/components/Versioning/VersioningSidebar.browser.test.tsx",