Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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: (
<SuggestionMenu
items={items}
loadingState="loaded"
selectedIndex={undefined}
/>
),
});
// 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: (
<SuggestionMenu
items={items}
loadingState="loaded"
selectedIndex={selectedIndex}
/>
),
});
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: (
<SuggestionMenu
items={items}
loadingState="loaded"
selectedIndex={1}
onItemClick={onItemClick}
/>
),
});
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: (
<SuggestionMenu
items={items}
loadingState="loaded"
selectedIndex={1}
/>
),
});
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: (
<SuggestionMenu
items={[]}
loadingState={loadingState}
selectedIndex={undefined}
/>
),
});
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: (
<SuggestionMenu
items={items}
loadingState="loaded"
selectedIndex={0}
/>
),
});
const menu = page.getByRole("listbox");
await expect.element(menu.getByRole("option")).toHaveLength(3);
expect(
menu.element().querySelector(".bn-suggestion-menu-loader"),
).toBeNull();
rerender({
children: (
<SuggestionMenu
items={items}
loadingState="loading"
selectedIndex={0}
/>
),
});
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: (
<SuggestionMenu
items={[items[1]]}
loadingState="loaded"
selectedIndex={0}
/>
),
});
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: (
<SuggestionMenu
items={items}
loadingState="loaded"
selectedIndex={undefined}
/>
),
});
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 }) => (
<SuggestionMenu
items={getDefaultReactSlashMenuItems(editor)}
loadingState="loaded"
selectedIndex={undefined}
onItemClick={(item) => 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: (
<SuggestionMenu
items={items}
loadingState="loaded"
selectedIndex={1}
/>
),
});
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: (
<SuggestionMenu
items={items}
loadingState="loaded"
selectedIndex={0}
/>
),
});
const menu = page.getByRole("listbox");
await expect.element(menu).toBeVisible();
expect((await runAxe(menu)).violations).toEqual([]);
});
});
});

This file was deleted.

Loading
Loading