Skip to content

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

Go
9.4 KB
Raw Blame
import type { ReactNode } from "react";
import { fireEvent, screen, waitFor } from "@testing-library/react";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { useSidebarTreeStore } from "@multica/core/issues/stores/sidebar-tree-store";
import { useActiveIssueViewStore } from "@multica/core/issue-views/active-view-store";
import { renderWithI18n } from "../../test/i18n";
import { NavigationProvider, type NavigationAdapter } from "../../navigation";
import { ViewsSection } from "./views-section";
import type { IssueViewActions } from "../../issue-views/use-view-actions";

const actions = vi.hoisted(() => ({
  sidebar: { items: [] as unknown[], reorder: vi.fn(), unstar: vi.fn() },
  create: vi.fn(),
  edit: vi.fn(),
  remove: vi.fn(),
  canManage: vi.fn(() => true),
  setVisibility: vi.fn(),
}));

vi.mock("../../issue-views/use-view-actions", () => ({ useIssueViewActions: () => actions }));
vi.mock("../../issue-views/delete-view-dialog", () => ({ DeleteViewDialog: () => null }));
vi.mock("@multica/ui/components/common/tooltip-hint", () => ({
  TooltipHint: ({ children }: { children: ReactNode }) => <>{children}</>,
}));
vi.mock("@multica/core/paths", async (importOriginal) => {
  const actual = await importOriginal<typeof import("@multica/core/paths")>();
  return { ...actual, useWorkspacePaths: () => actual.paths.workspace("acme") };
});
vi.mock("@multica/core/hooks", async (importOriginal) => ({
  ...(await importOriginal<typeof import("@multica/core/hooks")>()),
  useWorkspaceId: () => "ws-1",
}));
vi.mock("@multica/core/issue-statuses/hooks", async () => {
  const { buildIssueStatusCatalog } = await import("@multica/core/issue-statuses/queries");
  return { useIssueStatuses: () => buildIssueStatusCatalog([]) };
});
vi.mock("@tanstack/react-query", async (importOriginal) => ({
  ...(await importOriginal<typeof import("@tanstack/react-query")>()),
  useQuery: () => ({ data: [] }),
  useQueryClient: () => ({}),
}));

function makeAdapter(overrides: Partial<NavigationAdapter> = {}): NavigationAdapter {
  return {
    push: vi.fn(),
    replace: vi.fn(),
    back: vi.fn(),
    pathname: "/acme/issues",
    searchParams: new URLSearchParams(),
    hash: "",
    getShareableUrl: (path) => path,
    ...overrides,
  };
}

function renderSection(adapter = makeAdapter()) {
  return renderWithI18n(
    <NavigationProvider value={adapter}>
      <ViewsSection
        wsId="ws-1"
        pathname="/acme/issues"
        plainRowsLit
        issueInAllIssues={false}
        activeIssueId={null}
        litSource={null}
        actions={actions as unknown as IssueViewActions}
      />
    </NavigationProvider>,
  );
}

const WORKSPACE_KEY = "ws-1:workspace";
const SAVED_VIEW = {
  id: "v1",
  name: "Not done",
  scope_type: "workspace",
  scope_id: null,
  scope_variant: null,
  query: { statusFilters: ["done"], negatedFilters: ["status"], relationFilters: ["assigned"] },
  display: {},
};

beforeEach(() => {
  actions.create.mockReset();
  actions.edit.mockReset();
  actions.setVisibility.mockReset();
  actions.canManage.mockImplementation(() => true);
  actions.sidebar.items = [];
  useSidebarTreeStore.setState({ viewsCollapsed: false });
  useActiveIssueViewStore.setState({ active: {} });
});

describe("ViewsSection", () => {
  it("collapses from the section label", async () => {
    renderSection();
    expect(screen.getByText("All views")).toBeInTheDocument();

    fireEvent.click(screen.getByRole("button", { name: "Views" }));
    expect(useSidebarTreeStore.getState().viewsCollapsed).toBe(true);
    expect(screen.getByRole("button", { name: "Views" })).toHaveAttribute("aria-expanded", "false");
    await waitFor(() => expect(screen.queryByText("All views")).toBeNull());
  });

  it("opens again to show a new view, named in the page header", () => {
    useSidebarTreeStore.setState({ viewsCollapsed: true });
    renderSection();

    fireEvent.click(screen.getByRole("button", { name: "New view" }));
    expect(useSidebarTreeStore.getState().viewsCollapsed).toBe(false);
    expect(actions.create).toHaveBeenCalledWith(expect.objectContaining({ renameIn: "header" }));
  });

  it("lets every view expand, built-in or saved, whatever its filters", () => {
    actions.sidebar.items = [
      { id: "builtin:all_issues", kind: "builtin", key: "all_issues" },
      { id: "view:v1", kind: "view", view: SAVED_VIEW },
    ];
    renderSection();
    expect(screen.getByRole("button", { name: "Issues in All issues" })).toHaveAttribute("aria-expanded", "false");
    expect(screen.getByRole("button", { name: "Issues in Not done" })).toHaveAttribute("aria-expanded", "false");
  });

  it("edits a saved view from its right-click menu", async () => {
    actions.sidebar.items = [{ id: "view:v1", kind: "view", view: SAVED_VIEW }];
    renderSection();

    fireEvent.contextMenu(screen.getByRole("link", { name: "Not done" }));
    fireEvent.click(await screen.findByRole("menuitem", { name: "Edit view" }));
    expect(actions.edit).toHaveBeenCalledWith(SAVED_VIEW);
  });

  it("offers no Edit view on a view the user cannot manage", async () => {
    actions.canManage.mockImplementation(() => false);
    actions.sidebar.items = [{ id: "view:v1", kind: "view", view: SAVED_VIEW }];
    renderSection();

    fireEvent.contextMenu(screen.getByRole("link", { name: "Not done" }));
    expect(await screen.findByRole("menuitem", { name: "Duplicate" })).toBeInTheDocument();
    expect(screen.queryByRole("menuitem", { name: "Edit view" })).toBeNull();
  });

  it("shares a saved project view from its right-click menu", async () => {
    const view = { ...SAVED_VIEW, scope_type: "project", scope_id: "project-1", visibility: "private" };
    actions.sidebar.items = [{ id: "view:v1", kind: "view", view }];
    renderSection();

    fireEvent.contextMenu(screen.getByRole("link", { name: "Not done" }));
    fireEvent.click(await screen.findByRole("menuitem", { name: /^Visible to\s*Only me/ }));
    fireEvent.click(await screen.findByRole("menuitemradio", { name: "Workspace members" }));

    expect(actions.setVisibility).toHaveBeenCalledWith(view, "workspace");
    expect(useActiveIssueViewStore.getState().active).toEqual({});
  });

  // dnd-kit stops the release's click at the document without cancelling
  // it. Where React listens below the document (Desktop), it never sees that
  // click, so the row must not wait for it before opening on the next one.
  it("does not open the view released at the end of a drag, and opens it on the next click", async () => {
    actions.sidebar.items = [{ id: "view:v1", kind: "view", view: SAVED_VIEW }];
    const push = vi.fn();
    renderSection(makeAdapter({ push }));
    const row = screen.getByRole("link", { name: "Not done" });
    const pointer = { isPrimary: true, button: 0, clientX: 30, clientY: 100 };
    fireEvent.pointerDown(row, pointer);
    fireEvent.pointerMove(document, { ...pointer, clientY: 110 });
    fireEvent.pointerUp(document, { ...pointer, clientY: 110 });

    expect(fireEvent.click(row)).toBe(false);
    expect(push).not.toHaveBeenCalled();

    await new Promise((resolve) => setTimeout(resolve, 60));
    fireEvent.click(row);
    expect(push).toHaveBeenCalledWith("/acme/issues?view=v1");
  });

  describe("view state follows where the click actually navigates", () => {
    beforeEach(() => {
      actions.sidebar.items = [
        { id: "builtin:all_issues", kind: "builtin", key: "all_issues" },
        { id: "view:v1", kind: "view", view: SAVED_VIEW },
      ];
    });

    it("leaves the open view alone when All issues or a view opens in a new tab", () => {
      useActiveIssueViewStore.setState({ active: { [WORKSPACE_KEY]: "v-open" } });
      const openInNewTab = vi.fn();
      renderSection(makeAdapter({ openInNewTab }));

      fireEvent.click(screen.getByRole("link", { name: "All issues" }), { metaKey: true });
      fireEvent.click(screen.getByRole("link", { name: "Not done" }), { metaKey: true });

      expect(openInNewTab).toHaveBeenCalledWith("/acme/issues", "All issues");
      expect(openInNewTab).toHaveBeenCalledWith("/acme/issues?view=v1", "Not done");
      expect(useActiveIssueViewStore.getState().active[WORKSPACE_KEY]).toBe("v-open");
    });

    it("closes the open view for a desktop shift-click on All issues, which navigates in place", () => {
      useActiveIssueViewStore.setState({ active: { [WORKSPACE_KEY]: "v1" } });
      const push = vi.fn();
      renderSection(makeAdapter({ push, openInNewTab: vi.fn() }));

      fireEvent.click(screen.getByRole("link", { name: "All issues" }), { shiftKey: true });

      expect(push).toHaveBeenCalledWith("/acme/issues");
      expect(useActiveIssueViewStore.getState().active[WORKSPACE_KEY]).toBeUndefined();
    });

    it("opens a view for a desktop shift-click", () => {
      renderSection(makeAdapter({ openInNewTab: vi.fn() }));
      fireEvent.click(screen.getByRole("link", { name: "Not done" }), { shiftKey: true });
      expect(useActiveIssueViewStore.getState().active[WORKSPACE_KEY]).toBe("v1");
    });

    it("leaves state alone for a web shift-click, which the browser opens in a new window", () => {
      useActiveIssueViewStore.setState({ active: { [WORKSPACE_KEY]: "v-open" } });
      const push = vi.fn();
      renderSection(makeAdapter({ push }));

      fireEvent.click(screen.getByRole("link", { name: "All issues" }), { shiftKey: true });
      fireEvent.click(screen.getByRole("link", { name: "Not done" }), { shiftKey: true });

      expect(push).not.toHaveBeenCalled();
      expect(useActiveIssueViewStore.getState().active[WORKSPACE_KEY]).toBe("v-open");
    });
  });
});