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.

3.7 KB
Raw Blame
import { cleanup, fireEvent, render, screen } from "@testing-library/react";
import {
  afterEach,
  beforeEach,
  describe,
  expect,
  it,
  vi,
} from "vite-plus/test";
import { TooltipProvider } from "@/components/ui/tooltip";
import { TaskProgressBadges } from "./task-progress-badges";

const preferences = vi.hoisted(() => ({ showTaskItemCounts: true }));
vi.mock("@/store/user-preferences", () => ({
  useUserPreferencesStore: () => preferences,
}));
vi.mock("react-i18next", () => ({
  useTranslation: () => ({
    t: (key: string, values: { completed: number; total: number }) =>
      `${values.completed} of ${values.total} ${key.includes("subtasks") ? "subtasks" : "checklist items"} completed`,
  }),
}));

afterEach(cleanup);

beforeEach(() => {
  preferences.showTaskItemCounts = true;
});

describe("TaskProgressBadges", () => {
  it("keeps checklist and subtask completion independent", () => {
    render(
      <TaskProgressBadges
        task={{
          description: "- [x] One\n- [ ] Two\n- [ ] Three",
          subtaskCounts: { completed: 2, total: 2 },
        }}
      />,
    );
    const subtasks = screen.getByRole("button", {
      name: "2 of 2 subtasks completed",
    });
    const checklist = screen.getByRole("button", {
      name: "1 of 3 checklist items completed",
    });
    expect(subtasks).toHaveTextContent("2/2");
    expect(subtasks).toHaveClass("text-success-foreground");
    expect(checklist).toHaveTextContent("1/3");
    expect(checklist).not.toHaveClass("text-success-foreground");
  });

  it("shows subtasks when description checklist counts are disabled", () => {
    preferences.showTaskItemCounts = false;
    render(
      <TaskProgressBadges
        task={{
          description: "- [ ] One",
          subtaskCounts: { completed: 0, total: 3 },
        }}
      />,
    );
    expect(
      screen.getByRole("button", { name: "0 of 3 subtasks completed" }),
    ).toBeVisible();
    expect(screen.getAllByRole("button")).toHaveLength(1);
  });

  it("hides absent and empty counters", () => {
    const { rerender } = render(
      <TaskProgressBadges task={{ description: null }} />,
    );
    expect(screen.queryByRole("button")).not.toBeInTheDocument();
    rerender(
      <TaskProgressBadges
        task={{
          description: "Plain text",
          subtaskCounts: { completed: 0, total: 0 },
        }}
      />,
    );
    expect(screen.queryByRole("button")).not.toBeInTheDocument();
  });

  it("provides a tooltip and preserves the card's open action", async () => {
    const open = vi.fn();
    render(
      <TooltipProvider delay={0}>
        {/* eslint-disable-next-line jsx-a11y/no-static-element-interactions -- reproduces the task card click boundary */}
        <div onClick={open} role="presentation">
          <TaskProgressBadges
            task={{
              description: null,
              subtaskCounts: { completed: 2, total: 5 },
            }}
          />
        </div>
      </TooltipProvider>,
    );
    const badge = screen.getByRole("button", {
      name: "2 of 5 subtasks completed",
    });
    fireEvent.mouseEnter(badge);
    expect(await screen.findByText("2 of 5 subtasks completed")).toBeVisible();
    fireEvent.click(badge);
    expect(open).toHaveBeenCalledOnce();
  });
});

it.each([
  { description: "- [x] Truncated", descriptionDeferred: true },
  { description: "- [x] One\n".repeat(10000), descriptionDeferred: false },
])(
  "does not show partial checklist counts for bounded descriptions",
  (task) => {
    render(
      <TaskProgressBadges
        task={{ ...task, subtaskCounts: { completed: 1, total: 2 } }}
      />,
    );
    expect(screen.getAllByRole("button")).toHaveLength(1);
    expect(
      screen.getByRole("button", { name: "1 of 2 subtasks completed" }),
    ).toBeVisible();
  },
);