import { createRef } from "react";
import { fireEvent, render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import type { AccordionLinkElementType } from "./AccordionLink";
import { Accordion } from ".";
import { mockMatchMedia } from "../../utils/mockMatchMedia";
const CONTAINER_CLASS_NAME = "mobius-accordion";
const CONTENT_CONTAINER_CLASS_NAME = "mobius-accordion__content-container";
const HEADER_CLASS_NAME = "mobius-accordion__header";
const LINK_CLASS_NAME = "mobius-accordion__link";
const OPEN_CLASS_NAME = "--is-open";
const OPEN_TEXT = "See more";
const CLOSE_TEXT = "See less";
describe("Accordion", () => {
beforeEach(() => {
mockMatchMedia(true);
});
it("should render without errors", () => {
const view = render(test);
expect(view).toBeTruthy();
});
it("should open when clicked", () => {
const { container } = render(test content);
const toggle = screen.getByText(OPEN_TEXT);
const accordionContainer = container.firstChild;
expect(accordionContainer).not.toHaveClass(OPEN_CLASS_NAME);
expect(toggle).toBeInTheDocument();
fireEvent.click(toggle);
expect(accordionContainer).toHaveClass(OPEN_CLASS_NAME);
expect(screen.getByText(CLOSE_TEXT)).toBeInTheDocument();
fireEvent.click(toggle);
expect(accordionContainer).not.toHaveClass(OPEN_CLASS_NAME);
expect(toggle).toBeInTheDocument();
});
it("should render chevron after link", () => {
render(test);
const toggle = screen.getByRole("button");
expect(toggle.firstChild).toHaveTextContent(OPEN_TEXT);
expect(toggle.lastChild).toHaveAttribute("data-icon", "chevron-down");
});
describe("when headerPosition is set to top", () => {
it("renders the toggle above all other content", () => {
const { container } = render(
test content,
);
const accordion = container.firstChild;
expect(accordion?.firstChild).toHaveClass(LINK_CLASS_NAME);
expect(accordion?.lastChild).toHaveClass(CONTENT_CONTAINER_CLASS_NAME);
});
});
describe("when headerPosition is set to bottom", () => {
it("renders the toggle underneath all other content", () => {
const { container } = render(
test content,
);
const accordion = container.firstChild;
expect(accordion?.firstChild).toHaveClass(CONTENT_CONTAINER_CLASS_NAME);
expect(accordion?.lastChild).toHaveClass(LINK_CLASS_NAME);
});
});
it("respects startOpen prop and is expanded", () => {
render(test content);
expect(screen.getByText(CLOSE_TEXT)).toBeInTheDocument();
});
describe("uses correct class names", () => {
it("uses mobius class", () => {
const { container } = render(test content);
expect(container.firstChild).toHaveClass("mobius");
});
it("uses base Mobius class", () => {
const { container } = render(Sample text);
expect(container.firstChild).toHaveClass(CONTAINER_CLASS_NAME);
});
it("includes custom class name", () => {
const customClassName = "my-class";
const { container } = render(
Sample Text,
);
expect(container.firstChild).toHaveClass(customClassName);
expect(container.firstChild).toHaveClass(CONTAINER_CLASS_NAME);
});
});
describe("given headerChildren prop has content", () => {
it("renders the content", () => {
const buttonText = "Go";
const { container } = render(
{buttonText}}>
Sample Text
,
);
const headerContainer =
container.getElementsByClassName(HEADER_CLASS_NAME);
expect(screen.getByText(buttonText)).toBeInTheDocument();
expect(headerContainer).toHaveLength(1);
});
});
describe("given headerChildren is not defined", () => {
it("it does not render the header component", () => {
const { container } = render(Sample Text);
const headerContainer =
container.getElementsByClassName(HEADER_CLASS_NAME);
expect(headerContainer).toHaveLength(0);
});
});
it("renders with aria-expanded attribute", () => {
render(Sample Text);
const button = screen.getByRole("button");
expect(button).toHaveAttribute("aria-expanded", "false");
fireEvent.click(button);
expect(button).toHaveAttribute("aria-expanded", "true");
});
describe("providing that startOpen prop changes over time", () => {
it("renders the correct state", () => {
const { rerender } = render(Sample Text);
const seeLess = screen.getByText(CLOSE_TEXT);
expect(seeLess).toBeInTheDocument();
rerender(Sample Text);
const seeMore = screen.getByText(OPEN_TEXT);
expect(seeMore).toBeInTheDocument();
});
});
describe("events", () => {
it("calls onChange when accordion is toggled", () => {
const onChange = vi.fn();
render(Sample Text);
const button = screen.getByRole("button");
fireEvent.click(button);
expect(onChange).toHaveBeenCalledWith(true);
fireEvent.click(button);
expect(onChange).toHaveBeenCalledWith(false);
});
it("calls onOpen when accordion is opened", () => {
const onOpen = vi.fn();
const onClose = vi.fn();
render(
Sample Text
,
);
const button = screen.getByRole("button");
fireEvent.click(button);
expect(onOpen).toHaveBeenCalled();
expect(onClose).not.toHaveBeenCalled();
});
it("calls onClose when accordion is closed", () => {
const onOpen = vi.fn();
const onClose = vi.fn();
render(
Sample Text
,
);
const button = screen.getByRole("button");
fireEvent.click(button);
expect(onClose).toHaveBeenCalled();
expect(onOpen).not.toHaveBeenCalled();
});
});
describe("given user prefers reduced motion", () => {
it("runs no animations", () => {
const { container } = render(Sample Text);
expect(container.firstChild).not.toHaveClass("--should-animate");
});
});
describe("keyboard navigation", () => {
it("should open on Enter key", async () => {
const toggle = vi.fn();
render(Sample text);
await userEvent.tab();
await userEvent.keyboard("[Enter]");
expect(toggle).toHaveBeenCalled();
});
it("should open on Space key", async () => {
const toggle = vi.fn();
render(Sample text);
await userEvent.tab();
await userEvent.keyboard("[Space]");
expect(toggle).toHaveBeenCalled();
});
it("should not open on Tab key", async () => {
const toggle = vi.fn();
render(Sample text);
await userEvent.tab();
await userEvent.tab();
expect(toggle).not.toHaveBeenCalled();
});
});
describe("semantic heading", () => {
it("uses span by default", () => {
const toggle = vi.fn();
const { container } = render(
Sample text,
);
const linkContainer = container.getElementsByClassName(LINK_CLASS_NAME);
const link = linkContainer[0].firstChild;
expect(link instanceof HTMLSpanElement).toBe(true);
});
it.each(["h1", "h2", "h3", "h4", "h5", "h6"])(
"uses heading type %s",
headingType => {
const toggle = vi.fn();
const { container } = render(
Sample text
,
);
const linkContainer = container.getElementsByClassName(LINK_CLASS_NAME);
const link = linkContainer[0].firstChild;
expect(link instanceof HTMLHeadingElement).toBe(true);
expect(link?.nodeName).toBe(headingType.toUpperCase());
},
);
});
describe("showText and hideText with ReactNode", () => {
it("renders showText as a ReactNode when closed", () => {
const showTextNode = (
Expand this section
);
render(Sample text);
expect(screen.getByTestId("custom-show")).toBeInTheDocument();
expect(screen.getByText("Expand")).toBeInTheDocument();
expect(screen.getByText("this section")).toBeInTheDocument();
});
it("renders hideText as a ReactNode when open", () => {
const hideTextNode = (
Collapse this section
);
render(
Sample text
,
);
expect(screen.getByTestId("custom-hide")).toBeInTheDocument();
expect(screen.getByText("Collapse")).toBeInTheDocument();
expect(screen.getByText("this section")).toBeInTheDocument();
});
it("toggles between ReactNode showText and hideText", () => {
const showTextNode = Show more;
const hideTextNode = Show less;
render(
Sample text
,
);
// Initially closed - should show showText
expect(screen.getByTestId("custom-show")).toBeInTheDocument();
expect(screen.queryByTestId("custom-hide")).not.toBeInTheDocument();
// Click to open - should show hideText
const button = screen.getByRole("button");
fireEvent.click(button);
expect(screen.getByTestId("custom-hide")).toBeInTheDocument();
expect(screen.queryByTestId("custom-show")).not.toBeInTheDocument();
// Click to close - should show showText again
fireEvent.click(button);
expect(screen.getByTestId("custom-show")).toBeInTheDocument();
expect(screen.queryByTestId("custom-hide")).not.toBeInTheDocument();
});
it("renders showText with icon as ReactNode", () => {
const showTextWithIcon = (
More details
+
);
render(Sample text);
expect(screen.getByTestId("show-with-icon")).toBeInTheDocument();
expect(screen.getByText("More details")).toBeInTheDocument();
expect(screen.getByText("+")).toBeInTheDocument();
});
it("renders hideText with icon as ReactNode", () => {
const hideTextWithIcon = (
Less details
-
);
render(
Sample text
,
);
expect(screen.getByTestId("hide-with-icon")).toBeInTheDocument();
expect(screen.getByText("Less details")).toBeInTheDocument();
expect(screen.getByText("-")).toBeInTheDocument();
});
it("works with string values for backward compatibility", () => {
const customShow = "Expand content";
const customHide = "Collapse content";
render(
Sample text
,
);
// Initially closed - should show string showText
expect(screen.getByText(customShow)).toBeInTheDocument();
// Click to open - should show string hideText
const button = screen.getByRole("button");
fireEvent.click(button);
expect(screen.getByText(customHide)).toBeInTheDocument();
expect(screen.queryByText(customShow)).not.toBeInTheDocument();
});
});
it("forwards ref", () => {
const ref = createRef();
render(Text);
expect(ref.current).toBeInstanceOf(HTMLDivElement);
});
});