///
import React from "react";
import { Checklist } from "./index";
import { render, screen } from "@testing-library/react";
jest.mock("@shared/components/list", () => ({
List: ({
children,
className,
}: {
children: React.ReactNode;
className?: string;
}) => (
),
ListItem: ({
children,
className,
}: {
children: React.ReactNode;
className?: string;
}) => (
{children}
),
}));
jest.mock("@shared/components/material-icon", () => ({
MaterialIcon: ({
name,
size,
color,
className,
}: {
name: string;
size?: number;
color?: string;
className?: string;
}) => (
{name}
),
}));
jest.mock("@shared/components/text", () => ({
Text: ({
children,
}: {
children: React.ReactNode;
as?: React.ElementType;
}) => {children}
,
}));
jest.mock("@shared/utils/cx", () => ({
cx: (...args: unknown[]) => args.filter(Boolean).join(" "),
}));
describe("Checklist", () => {
describe("rendering", () => {
it("renders null when items is empty", () => {
const { container } = render();
expect(container.firstChild).toBeNull();
});
it("renders null when items is undefined", () => {
const { container } = render(
);
expect(container.firstChild).toBeNull();
});
it("renders a list with items", () => {
render();
const listItems = screen.getAllByTestId("list-item");
expect(listItems).toHaveLength(3);
});
it("renders item text content", () => {
render();
expect(screen.getByText("First item")).toBeInTheDocument();
expect(screen.getByText("Second item")).toBeInTheDocument();
});
it("renders ReactNode items", () => {
render(
Bold item,
Italic item,
]}
/>
);
expect(screen.getByText("Bold item")).toBeInTheDocument();
expect(screen.getByText("Italic item")).toBeInTheDocument();
});
it("applies displayName correctly", () => {
expect(Checklist.displayName).toBe("Checklist");
});
});
describe("icons", () => {
it("renders check icons by default", () => {
render();
expect(screen.getByTestId("icon-check")).toBeInTheDocument();
});
it("renders custom icon name", () => {
render();
expect(screen.getByTestId("icon-check")).toBeInTheDocument();
});
it("does not render icons when listIconName is disc", () => {
render();
expect(screen.queryByTestId("icon-check")).not.toBeInTheDocument();
expect(screen.queryByTestId("icon-disc")).not.toBeInTheDocument();
});
it("passes correct iconSize to MaterialIcon", () => {
render();
const icon = screen.getByTestId("icon-check");
expect(icon).toHaveAttribute("data-size", "32");
});
it("uses default iconSize of 20", () => {
render();
const icon = screen.getByTestId("icon-check");
expect(icon).toHaveAttribute("data-size", "20");
});
});
describe("icon colors", () => {
it("applies green color by default", () => {
render();
const icon = screen.getByTestId("icon-check");
expect(icon).toHaveAttribute("data-color", "#209A61");
});
it("applies yellow color", () => {
render();
const icon = screen.getByTestId("icon-check");
expect(icon).toHaveAttribute("data-color", "#F5FF36");
});
it("applies blue color", () => {
render();
const icon = screen.getByTestId("icon-check");
expect(icon).toHaveAttribute("data-color", "#0393BA");
});
});
describe("icon position", () => {
it("applies items-center by default (center position)", () => {
render();
const listItem = screen.getByTestId("list-item");
expect(listItem.className).toContain("items-center");
});
it("applies items-start for top position", () => {
render();
const listItem = screen.getByTestId("list-item");
expect(listItem.className).toContain("items-start");
});
it("applies mt-1 to icon when position is top", () => {
render();
const icon = screen.getByTestId("icon-check");
expect(icon.className).toContain("mt-1");
});
it("does not apply mt-1 to icon when position is center", () => {
render();
const icon = screen.getByTestId("icon-check");
expect(icon.className).not.toContain("mt-1");
});
});
describe("classNames", () => {
it("applies listContainerClassName to the List", () => {
render(
);
const list = screen.getByTestId("list");
expect(list.className).toContain("custom-list");
});
it("applies listItemClassName to ListItems", () => {
render(
);
const items = screen.getAllByTestId("list-item");
items.forEach(item => {
expect(item.className).toContain("custom-item");
});
});
it("applies iconClassName to the icon", () => {
render();
const icon = screen.getByTestId("icon-check");
expect(icon.className).toContain("custom-icon");
});
});
describe("list variant", () => {
it("uses unstyled variant when icons are shown", () => {
render();
// With icons (default check), the list items should have flex class
const listItem = screen.getByTestId("list-item");
expect(listItem.className).toContain("flex");
});
it("does not apply flex class when using disc variant", () => {
render();
const listItem = screen.getByTestId("list-item");
expect(listItem.className).not.toContain("flex");
});
});
});