Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go
8.0 KB
Raw Blame
import { act, fireEvent, render, screen, waitFor } from "@testing-library/react";
import type { Editor } from "@tiptap/core";
import { useState } from "react";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { ContextMenu, ContextMenuContent, ContextMenuItem } from "@multica/ui/components/ui/context-menu";
import { contextMenuOriginRef } from "@multica/ui/lib/context-menu-origin";

const formatState = vi.hoisted(() => ({ codeBlock: false }));

vi.mock("@floating-ui/dom", () => ({
  autoUpdate: vi.fn(() => vi.fn()),
  computePosition: vi.fn(() =>
    Promise.resolve({
      x: 0,
      y: 0,
      middlewareData: {},
    }),
  ),
  flip: vi.fn(),
  hide: vi.fn(),
  offset: vi.fn(),
  shift: vi.fn(),
}));

vi.mock("@tiptap/react", () => ({
  useEditorState: () => ({
    bold: false,
    italic: false,
    strike: false,
    code: false,
    codeBlock: formatState.codeBlock,
    highlight: false,
    link: false,
    blockquote: false,
    bulletList: false,
    orderedList: false,
    taskList: false,
    headingLevel: undefined,
  }),
}));

vi.mock("@multica/core/issues/mutations", () => ({
  useCreateIssue: () => ({
    mutateAsync: vi.fn(),
  }),
}));

vi.mock("../i18n", async () => {
  const editor = (await import("../locales/en/editor.json")).default;
  return {
    useT: () => ({
      t: (selector: (resources: typeof editor) => string) => selector(editor),
    }),
  };
});

import { EditorBubbleMenu } from "./bubble-menu";

function createEditor(codeBlock = false): Editor {
  const chain = {
    focus: vi.fn(),
    toggleBold: vi.fn(),
    toggleItalic: vi.fn(),
    toggleStrike: vi.fn(),
    toggleCode: vi.fn(),
    toggleHighlight: vi.fn(),
    extendMarkRange: vi.fn(),
    unsetLink: vi.fn(),
    setLink: vi.fn(),
    toggleHeading: vi.fn(),
    setParagraph: vi.fn(),
    toggleBulletList: vi.fn(),
    toggleOrderedList: vi.fn(),
    toggleTaskList: vi.fn(),
    toggleBlockquote: vi.fn(),
    insertContentAt: vi.fn(),
    run: vi.fn(),
  };
  for (const method of Object.keys(chain)) {
    if (method !== "run") {
      chain[method as keyof typeof chain] = vi.fn(() => chain) as never;
    }
  }

  return {
    isEditable: true,
    isDestroyed: false,
    isInitialized: false,
    state: {
      selection: { empty: false, from: 1, to: 2 },
      doc: {
        textBetween: () => "selected text",
        resolve: () => ({ parent: { type: { name: codeBlock ? "codeBlock" : "paragraph" } } }),
      },
    },
    view: {
      dom: document.createElement("div"),
      hasFocus: () => false,
    },
    commands: {
      focus: vi.fn(),
      setTextSelection: vi.fn(),
    },
    chain: () => chain,
    getAttributes: () => ({ href: "https://example.com" }),
    on: vi.fn(),
    off: vi.fn(),
  } as unknown as Editor;
}

describe("EditorBubbleMenu accessibility", () => {
  beforeEach(() => { vi.clearAllMocks(); formatState.codeBlock = false; });

  it("keeps the selection and toolbar on failed capture without resubscribing for new action objects", () => {
    const editor = createEditor();
    Object.defineProperty(editor, "isInitialized", { value: true });
    const { rerender } = render(<EditorBubbleMenu editor={editor} selectionAction={{ label: "Add to comment", onSelect: () => false }} />);
    const transaction = vi.mocked(editor.on).mock.calls.find(([event]) => event === "transaction")?.[1] as (() => void);
    act(() => transaction());
    const registrations = vi.mocked(editor.on).mock.calls.filter(([event]) => event === "transaction").length;
    rerender(<EditorBubbleMenu editor={editor} selectionAction={{ label: "Add to comment", onSelect: () => false }} />);
    expect(vi.mocked(editor.on).mock.calls.filter(([event]) => event === "transaction")).toHaveLength(registrations);
    fireEvent.click(screen.getByRole("button", { name: "Add to comment" }));
    expect(editor.commands.setTextSelection).not.toHaveBeenCalled();
    expect(screen.getByRole("button", { name: "Add to comment" })).toBeVisible();
  });

  it.each([false, true])("exposes a non-editing selection action, including code (code: %s)", (codeBlock) => {
    formatState.codeBlock = codeBlock;
    const editor = createEditor(codeBlock);
    Object.defineProperty(editor, "isInitialized", { value: true });
    const onSelect = vi.fn();
    render(<EditorBubbleMenu editor={editor} selectionAction={{ label: "Add to comment", onSelect }} />);
    const transaction = vi.mocked(editor.on).mock.calls.find(([event]) => event === "transaction")?.[1] as (() => void);
    act(() => transaction());
    const action = screen.getByRole("button", { name: "Add to comment" });
    expect(action).toBeVisible();
    fireEvent.mouseDown(action);
    fireEvent.click(action);
    expect(onSelect).toHaveBeenCalledOnce();
    expect(editor.commands.setTextSelection).toHaveBeenCalledWith(2);
    expect(editor.chain().insertContentAt).not.toHaveBeenCalled();
    expect(editor.chain().toggleBlockquote).not.toHaveBeenCalled();
    if (codeBlock) expect(screen.queryByLabelText("Bold")).not.toBeInTheDocument();
  });

  it("gives every icon-only formatting control an accessible name", () => {
    render(
      <EditorBubbleMenu editor={createEditor()} currentIssueId="issue-parent" />,
    );

    for (const name of [
      "Bold",
      "Italic",
      "Strikethrough",
      "Code",
      "Highlight",
      "Link",
      "List",
      "Task list",
      "Quote",
      "Create sub-issue from selection",
    ]) {
      expect(
        screen.getByLabelText(name, { selector: "button" }),
      ).toBeInTheDocument();
    }
  });

  function showOver(editor: Editor) {
    Object.defineProperty(editor, "isInitialized", { value: true });
    document.body.appendChild(editor.view.dom).textContent = "selected text";
    render(<EditorBubbleMenu editor={editor} />);
    const handler = (event: string) =>
      vi.mocked(editor.on).mock.calls.find(([name]) => name === event)?.[1] as () => void;
    act(() => handler("transaction")());
    return { blur: handler("blur") };
  }

  it("stays up while a context menu opened in the editor takes focus and runs an item", async () => {
    const editor = createEditor();
    const { blur } = showOver(editor);
    const copy = vi.fn();
    function EditMenu() {
      const [open, setOpen] = useState(true);
      return (
        <ContextMenu open={open} onOpenChange={setOpen}>
          <ContextMenuContent ref={contextMenuOriginRef(editor.view.dom)}>
            <ContextMenuItem onClick={copy}>Copy</ContextMenuItem>
          </ContextMenuContent>
        </ContextMenu>
      );
    }
    render(<EditMenu />);

    const menu = await screen.findByRole("menu");
    await waitFor(() => expect(menu.contains(document.activeElement)).toBe(true));
    await act(async () => {
      blur();
      await new Promise((resolve) => setTimeout(resolve, 0));
    });
    const item = screen.getByRole("menuitem", { name: "Copy" });
    fireEvent.pointerDown(item, { button: 0 });
    fireEvent.mouseDown(item, { button: 0 });
    fireEvent.click(item);

    await waitFor(() => expect(screen.queryByRole("menu")).toBeNull());
    expect(copy).toHaveBeenCalledTimes(1);
    expect(screen.getByLabelText("Bold", { selector: "button" })).toBeVisible();
    editor.view.dom.remove();
  });

  it("hides on a press outside the editor", () => {
    const editor = createEditor();
    showOver(editor);
    const outside = document.body.appendChild(document.createElement("button"));

    fireEvent.mouseDown(outside);

    expect(screen.getByLabelText("Bold", { selector: "button" })).not.toBeVisible();
    outside.remove();
    editor.view.dom.remove();
  });

  it("gives every icon-only link editing control an accessible name", () => {
    render(<EditorBubbleMenu editor={createEditor()} />);

    fireEvent.click(screen.getByLabelText("Link", { selector: "button" }));

    expect(
      screen.getByLabelText("Apply link", { selector: "button" }),
    ).toBeInTheDocument();
    expect(
      screen.getByLabelText("Remove link", { selector: "button" }),
    ).toBeInTheDocument();
    expect(
      screen.getByLabelText("Close link editor", { selector: "button" }),
    ).toBeInTheDocument();
  });
});