Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

8.4 KB
Raw Blame
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import {
  act,
  cleanup,
  fireEvent,
  render as renderWithoutClient,
  screen,
} from "@testing-library/react";
import type { ReactElement, ReactNode } from "react";
import {
  afterEach,
  beforeEach,
  describe,
  expect,
  it,
  vi,
} from "vite-plus/test";
import useBacklogBulkSelectionStore from "@/store/backlog-bulk-selection";
import useProjectStore from "@/store/project";
import type { ProjectWithTasks } from "@/types/project";
import type Task from "@/types/task";
import BacklogTaskRow from "./backlog-task-row";

const testQueryClient = new QueryClient();
const render = (ui: ReactElement) =>
  renderWithoutClient(ui, {
    wrapper: ({ children }: { children: ReactNode }) => (
      <QueryClientProvider client={testQueryClient}>
        {children}
      </QueryClientProvider>
    ),
  });

const { sortableKeyDown } = vi.hoisted(() => ({ sortableKeyDown: vi.fn() }));
const { useSortable } = vi.hoisted(() => ({
  useSortable: vi.fn((_options: { id: string }) => ({
    attributes: { role: "button", tabIndex: 0 },
    listeners: { onKeyDown: sortableKeyDown },
    setNodeRef: vi.fn(),
    transform: null,
    transition: null,
    isDragging: false,
  })),
}));
vi.mock("@dnd-kit/sortable", () => ({ useSortable }));
vi.mock("@tanstack/react-router", () => ({ useNavigate: () => vi.fn() }));
vi.mock("@/hooks/mutations/task/use-delete-task", () => ({
  useDeleteTask: () => ({ mutateAsync: vi.fn() }),
}));
vi.mock("@/hooks/queries/workspace/use-active-workspace", () => ({
  default: () => ({ data: { id: "workspace-1" } }),
}));
vi.mock(
  "@/hooks/queries/workspace-users/use-get-active-workspace-users",
  () => ({
    useGetActiveWorkspaceUsers: () => ({ data: { members: [] } }),
  }),
);
vi.mock(
  "@/hooks/queries/custom-field/use-get-custom-field-values-by-project",
  () => ({
    default: () => ({ data: [] }),
  }),
);
vi.mock(
  "../kanban-board/task-card-context-menu/task-card-context-menu-content",
  () => ({
    default: () => null,
  }),
);
vi.mock("@/store/user-preferences", () => ({
  useUserPreferencesStore: () => ({ showLabels: true, showTaskNumbers: true }),
}));
vi.mock("react-i18next", () => ({
  useTranslation: () => ({
    t: (key: string) => key,
    i18n: { language: "en-US", resolvedLanguage: "en-US" },
  }),
  initReactI18next: { type: "3rdParty", init: vi.fn() },
}));

const task: Task = {
  id: "task-1",
  title: "First task",
  number: 1,
  description: null,
  status: "planned",
  priority: null,
  startDate: null,
  dueDate: null,
  position: 0,
  createdAt: "2026-09-16T00:00:00.000Z",
  userId: null,
  assigneeId: null,
  assigneeName: null,
  projectId: "project-1",
  labels: [],
};
const otherTask = { ...task, id: "task-2", title: "Second task" };

const project: ProjectWithTasks = {
  id: "project-1",
  workspaceId: "workspace-1",
  name: "Imported project",
  slug: "IMP",
  icon: null,
  description: null,
  isPublic: false,
  columns: [],
  plannedTasks: [task, otherTask],
  archivedTasks: [],
};

beforeEach(() => {
  useProjectStore.setState({ project });
  useBacklogBulkSelectionStore.setState(
    useBacklogBulkSelectionStore.getInitialState(),
  );
  vi.clearAllMocks();
});
afterEach(cleanup);

describe("backlog row subscriptions", () => {
  it.each([
    ["plain click", {}],
    ["Ctrl+click", { ctrlKey: true }],
  ])(
    "selects the visible range after %s and Shift+click",
    (_label, firstClick) => {
      useBacklogBulkSelectionStore
        .getState()
        .setAvailableTasks([task.id, otherTask.id]);
      render(
        <>
          <BacklogTaskRow task={task} />
          <BacklogTaskRow task={otherTask} />
        </>,
      );

      fireEvent.click(
        screen.getByRole("button", { name: /First task/ }),
        firstClick,
      );
      fireEvent.click(screen.getByRole("button", { name: /Second task/ }), {
        shiftKey: true,
      });

      expect(useBacklogBulkSelectionStore.getState().selectedTaskIds).toEqual(
        new Set([task.id, otherTask.id]),
      );
    },
  );

  it("selects the visible range with Shift+Enter", () => {
    useBacklogBulkSelectionStore
      .getState()
      .setAvailableTasks([task.id, otherTask.id]);
    render(
      <>
        <BacklogTaskRow task={task} />
        <BacklogTaskRow task={otherTask} />
      </>,
    );

    fireEvent.click(screen.getByRole("button", { name: /First task/ }));
    fireEvent.keyDown(screen.getByRole("button", { name: /Second task/ }), {
      key: "Enter",
      shiftKey: true,
    });

    expect(useBacklogBulkSelectionStore.getState().selectedTaskIds).toEqual(
      new Set([task.id, otherTask.id]),
    );
    fireEvent.keyDown(screen.getByRole("button", { name: /Second task/ }), {
      key: " ",
      code: "Space",
    });
    expect(sortableKeyDown).not.toHaveBeenCalled();
  });

  it("takes keyboard focus when the row is focused", () => {
    render(<BacklogTaskRow task={task} />);

    fireEvent.keyDown(document, { key: "Tab" });
    act(() => screen.getByRole("button", { name: /First task/ }).focus());

    expect(useBacklogBulkSelectionStore.getState().focusedTaskId).toBe(task.id);
  });

  it("only renders the row whose selection changes, including deselection", () => {
    render(
      <>
        <BacklogTaskRow task={task} />
        <BacklogTaskRow task={otherTask} />
      </>,
    );
    useSortable.mockClear();

    fireEvent.click(screen.getByRole("button", { name: /First task/ }), {
      ctrlKey: true,
    });
    expect(useSortable.mock.calls.map(([options]) => options.id)).toEqual([
      "task-1",
    ]);
    expect(screen.getByRole("button", { name: /First task/ })).toHaveClass(
      "bg-accent/45",
    );

    useSortable.mockClear();
    act(() => useBacklogBulkSelectionStore.getState().clearSelection());
    expect(useSortable.mock.calls.map(([options]) => options.id)).toEqual([
      "task-1",
    ]);
    expect(screen.getByRole("button", { name: /First task/ })).not.toHaveClass(
      "bg-accent/45",
    );
  });

  it("updates focus highlights without rendering rows on available-task changes", () => {
    render(
      <>
        <BacklogTaskRow task={task} />
        <BacklogTaskRow task={otherTask} />
      </>,
    );
    useSortable.mockClear();
    act(() =>
      useBacklogBulkSelectionStore
        .getState()
        .setAvailableTasks([task.id, otherTask.id]),
    );
    expect(useSortable).not.toHaveBeenCalled();

    act(() => useBacklogBulkSelectionStore.getState().setFocusedTask(task.id));
    expect(useSortable.mock.calls.map(([options]) => options.id)).toEqual([
      "task-1",
    ]);
    expect(
      screen
        .getByRole("button", { name: /First task/ })
        .closest(".ring-board-card-focus"),
    ).not.toBeNull();

    useSortable.mockClear();
    act(() =>
      useBacklogBulkSelectionStore.getState().setFocusedTask(otherTask.id),
    );
    expect(
      useSortable.mock.calls.map(([options]) => options.id).sort(),
    ).toEqual(["task-1", "task-2"]);
    expect(
      screen
        .getByRole("button", { name: /First task/ })
        .closest(".ring-board-card-focus"),
    ).toBeNull();
    expect(
      screen
        .getByRole("button", { name: /Second task/ })
        .closest(".ring-board-card-focus"),
    ).not.toBeNull();
  });

  it("ignores unrelated project updates but keeps task numbers in sync with the slug", () => {
    render(
      <>
        <BacklogTaskRow task={task} />
        <BacklogTaskRow task={otherTask} />
      </>,
    );
    useSortable.mockClear();
    act(() =>
      useProjectStore
        .getState()
        .setProject({ ...project, description: "Updated description" }),
    );
    expect(useSortable).not.toHaveBeenCalled();
    act(() =>
      useProjectStore.getState().setProject({ ...project, slug: "NEW" }),
    );
    expect(
      useSortable.mock.calls.map(([options]) => options.id).sort(),
    ).toEqual(["task-1", "task-2"]);
    expect(screen.getAllByText("NEW-1")).toHaveLength(2);
  });

  it("skips unchanged task props but renders refreshed task data", () => {
    const { rerender } = render(<BacklogTaskRow task={task} />);
    useSortable.mockClear();
    rerender(<BacklogTaskRow task={task} />);
    expect(useSortable).not.toHaveBeenCalled();
    rerender(
      <BacklogTaskRow
        task={{
          ...task,
          title: "Updated title",
          labels: [{ id: "label-1", name: "Bug", color: "red" }],
        }}
      />,
    );
    expect(screen.getByText("Updated title")).toBeVisible();
    expect(screen.getByText("Bug")).toBeVisible();
  });
});