[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",