import Accordion from "./index";
import { AccordionItem } from "./types";
import { fireEvent, render, screen } from "@testing-library/react";
const items: AccordionItem[] = [
{ anchorId: "item-1", title: "First Question", description: "Answer one" },
{ anchorId: "item-2", title: "Second Question", description: "Answer two" },
{ anchorId: "item-3", title: "Third Question", description: "Answer three" },
];
describe("Contentful Accordion", () => {
beforeEach(() => {
Object.defineProperty(window, "location", {
writable: true,
value: { hash: "" },
});
});
it("renders the title as h2 by default", () => {
render();
const heading = screen.getByText("FAQ");
expect(heading.tagName).toBe("H2");
});
it("renders the title as h1 when enableHeading is true", () => {
render();
const heading = screen.getByText("FAQ");
expect(heading.tagName).toBe("H1");
});
it("renders all accordion item titles", () => {
render();
expect(screen.getByText("First Question")).toBeInTheDocument();
expect(screen.getByText("Second Question")).toBeInTheDocument();
expect(screen.getByText("Third Question")).toBeInTheDocument();
});
it("renders item descriptions", () => {
render();
expect(screen.getByText("Answer one")).toBeInTheDocument();
expect(screen.getByText("Answer two")).toBeInTheDocument();
expect(screen.getByText("Answer three")).toBeInTheDocument();
});
it("applies anchorId to the section", () => {
const { container } = render(
);
expect(container.querySelector("#faq-section")).toBeInTheDocument();
});
it("applies white background by default", () => {
const { container } = render();
expect(container.querySelector(".bg-white")).toBeInTheDocument();
});
it("applies navy background", () => {
const { container } = render(
);
expect(
container.querySelector('section[class*="bg-[#00002D]"]')
).toBeInTheDocument();
});
it("applies blue background", () => {
const { container } = render(
);
expect(
container.querySelector('section[class*="bg-[#07B2E2]"]')
).toBeInTheDocument();
});
it("applies green background", () => {
const { container } = render(
);
expect(
container.querySelector('section[class*="bg-[#26B170]"]')
).toBeInTheDocument();
});
it("applies yellow background", () => {
const { container } = render(
);
expect(
container.querySelector('section[class*="bg-[#F5FF1E]"]')
).toBeInTheDocument();
});
it("applies purple background", () => {
const { container } = render(
);
expect(
container.querySelector('section[class*="bg-[#931D69]"]')
).toBeInTheDocument();
});
it("applies cream500 background", () => {
const { container } = render(
);
expect(
container.querySelector('section[class*="bg-[#FFFEEF]"]')
).toBeInTheDocument();
});
it("applies maxWidth class by default", () => {
const { container } = render();
expect(container.querySelector(".max-w-120")).toBeInTheDocument();
});
it("does not apply maxWidth class when maxWidth is false", () => {
const { container } = render(
);
expect(container.querySelector(".max-w-120")).not.toBeInTheDocument();
});
it("renders dividers between items but not after the last", () => {
const { container } = render();
const dividers = container.querySelectorAll(
".bg-bg-surface-tertiary-active"
);
expect(dividers).toHaveLength(items.length - 1);
});
it("expands items in initialExpandedItems", () => {
render(
);
const arrowsUp = screen.getAllByText("keyboard_arrow_up");
expect(arrowsUp.length).toBeGreaterThanOrEqual(1);
});
it("auto-expands item matching URL hash when hashAutoExpand is true", () => {
Object.defineProperty(window, "location", {
writable: true,
value: { hash: "#item-2" },
});
render();
expect(document.getElementById("item-2")).toBeInTheDocument();
});
it("does not auto-expand when hashAutoExpand is false", () => {
Object.defineProperty(window, "location", {
writable: true,
value: { hash: "#item-2" },
});
render();
const arrowsDown = screen.getAllByText("keyboard_arrow_down");
expect(arrowsDown.length).toBe(items.length);
});
it("does not duplicate item in expandedItems if already present", () => {
Object.defineProperty(window, "location", {
writable: true,
value: { hash: "#item-1" },
});
render(
);
expect(document.getElementById("item-1")).toBeInTheDocument();
});
it("applies itemContainerClassName", () => {
const { container } = render(
);
expect(container.querySelector(".gap-4")).toBeInTheDocument();
});
it("applies descriptionMaxWidth class", () => {
const { container } = render(
);
expect(container.querySelector(".max-w-\\[500px\\]")).toBeInTheDocument();
});
it("sets id on items with anchorId", () => {
render();
expect(document.getElementById("item-1")).toBeInTheDocument();
expect(document.getElementById("item-2")).toBeInTheDocument();
});
it("handles items without anchorId using index as key", () => {
const noAnchorItems: AccordionItem[] = [
{ title: "Q1", description: "A1" },
{ title: "Q2", description: "A2" },
];
render();
expect(screen.getByText("Q1")).toBeInTheDocument();
expect(screen.getByText("Q2")).toBeInTheDocument();
});
it("toggles expanded state when an accordion item button is clicked", () => {
render(
);
const buttons = screen.getAllByRole("button");
fireEvent.click(buttons[0]);
fireEvent.click(buttons[0]);
expect(screen.getByText("First Question")).toBeInTheDocument();
});
});