import React from "react"; import userEvent from "@testing-library/user-event"; import { render } from "@testing-library/react"; import { Accordion, AccordionItem, AccordionItemTitle, AccordionItemContent, AccordionItemTitleInteractive } from "./"; import { getById } from "../../testHelper/test.utilities"; describe("Accordion", () => { describe("rendering", () => { it("renders with no expanded items", () => { const { asFragment } = render( Panel 1 Content 1 Panel 2 Content 2 Panel 3 Content 3 ); expect(asFragment()).toMatchSnapshot(); }); it("renders an item with interactive content", () => { const { asFragment } = render( {({ getHeading }) => ( <> {getHeading("Panel 1")} )} Content 1 {({ headingId, toggleButtonProps }) => ( <>

Panel 1

)}
Content 2
{({ getHeading }) => ( <> {getHeading("Panel 1")} )} Content 1
); expect(asFragment()).toMatchSnapshot(); }); it("renders with expanded items", () => { const { asFragment } = render( Panel 1 Content 1 Panel 2 Content 2 Panel 3 Content 3 ); expect(asFragment()).toMatchSnapshot(); }); }); describe("interaction", () => { it("expands and collapses accordion items", async () => { const user = userEvent.setup(); const { getByText } = render( Panel 1 Content 1 Panel 2 Content 2 Panel 3 Content 3 ); expect(getByText("Content 2")).toBeInTheDocument(); expect(getByText("Content 2")).not.toBeVisible(); await user.click(getByText("Panel 1")); expect(getByText("Content 1")).toBeVisible(); expect(getByText("Content 2")).not.toBeVisible(); await user.click(getByText("Panel 2")); expect(getByText("Content 1")).not.toBeVisible(); expect(getByText("Content 2")).toBeVisible(); }); it("does not expand or collapses accordion items with a disabled title", async () => { const user = userEvent.setup(); const { container, getByText } = render( Panel 1 Content 1 Panel 2 Content 2 Panel 3 Content 3 ); const element = getById(container, "disabled-content"); expect(element).toBeInTheDocument(); expect(getByText("Content 1")).not.toBeVisible(); await user.click(getByText("Panel 1")); expect(getByText("Content 1")).not.toBeVisible(); }); it("expands and collapses accordion items - allowMultipleExpanded", async () => { const user = userEvent.setup(); const { getByText } = render( Panel 1 Content 1 Panel 2 Content 2 Panel 3 Content 3 ); expect(getByText("Content 1")).not.toBeVisible(); await user.click(getByText("Panel 1")); expect(getByText("Content 1")).toBeVisible(); expect(getByText("Content 2")).not.toBeVisible(); await user.click(getByText("Panel 2")); expect(getByText("Content 2")).toBeVisible(); }); it("allows interactive title children without expanding or collapsing the item", async () => { const user = userEvent.setup(); const { getByText } = render( {({ getHeading }) => ( <> {getHeading("Panel 1")} )} Content 1 Panel 2 Content 2 Panel 3 Content 3 ); expect(getByText("Content 1")).not.toBeVisible(); expect(getByText("Panel 1")).toBeInTheDocument(); await user.click(getByText("test")); expect(getByText("Content 1")).not.toBeVisible(); }); it("calls onChange with array of expanded item IDs", async () => { const user = userEvent.setup(); const onChange = jest.fn(); const { getByText } = render( Panel 1 Content 1 Panel 2 Content 2 Panel 3 Content 3 ); await user.click(getByText("Panel 1")); expect(onChange).toHaveBeenCalledWith(["panel1"]); await user.click(getByText("Panel 2")); expect(onChange).toHaveBeenCalledWith(["panel1", "panel2"]); }); }); });