import React from "react";
import { MobileLinkGroups } from "./index";
import { fireEvent, render, screen } from "@testing-library/react";
jest.mock("@shared/components/material-icon", () => ({
MaterialIcon: ({ name, className }: any) => (
{name}
),
}));
jest.mock("@shared/contentful/blocks/button", () => ({
Button: ({
children,
buttonClassName,
onClick,
linkClassName,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
linkVariant,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
showButtonAs,
...rest
}: any) => (
),
}));
describe("MobileLinkGroups", () => {
it("renders nothing when link is undefined", () => {
const { container } = render();
expect(container.innerHTML).toBe("");
});
it("renders single Button when link has href (isButton)", () => {
const link = { href: "/shop", buttonLabel: "Shop", anchorId: "shop1" };
render();
expect(screen.getByTestId("button")).toHaveTextContent("Shop");
});
describe("Group rendering", () => {
const groupLink = {
anchorId: "grp-1",
title: "Services",
items: {
items: [
{ buttonLabel: "Internet", href: "/internet", anchorId: "s1" },
{ buttonLabel: "TV", href: "/tv", anchorId: "s2" },
],
},
};
it("renders group title button", () => {
render();
expect(screen.getByText("Services")).toBeInTheDocument();
});
it("renders down arrow icon when closed", () => {
render();
expect(
screen.getByTestId("icon-keyboard_arrow_down")
).toBeInTheDocument();
});
it("toggles open state and shows up arrow when open", () => {
render();
const toggleBtn = screen.getAllByTestId("button")[0];
fireEvent.click(toggleBtn);
expect(screen.getByTestId("icon-keyboard_arrow_up")).toBeInTheDocument();
});
it("renders submenu items when open", () => {
render();
fireEvent.click(screen.getAllByTestId("button")[0]);
expect(screen.getByText("Internet")).toBeInTheDocument();
expect(screen.getByText("TV")).toBeInTheDocument();
});
it("renders submenu as list items", () => {
render();
fireEvent.click(screen.getAllByTestId("button")[0]);
const listItems = screen.getAllByRole("listitem");
expect(listItems).toHaveLength(2);
});
it("closes on second toggle click", () => {
render();
const toggleBtn = screen.getAllByTestId("button")[0];
fireEvent.click(toggleBtn);
fireEvent.click(toggleBtn);
expect(
screen.getByTestId("icon-keyboard_arrow_down")
).toBeInTheDocument();
});
it("does not render submenu when closed", () => {
render();
expect(screen.queryByRole("list")).not.toBeInTheDocument();
});
it("handles null items", () => {
const nullItems = { ...groupLink, items: null };
render();
fireEvent.click(screen.getAllByTestId("button")[0]);
// No list rendered since subMenu is empty
expect(screen.queryByRole("list")).not.toBeInTheDocument();
});
it("handles empty items array", () => {
const emptyItems = { ...groupLink, items: { items: [] } };
render();
fireEvent.click(screen.getAllByTestId("button")[0]);
expect(screen.queryByRole("list")).not.toBeInTheDocument();
});
it("renders null title when title is missing", () => {
const noTitle = { ...groupLink, title: null };
render();
// Should render without error, button exists
expect(screen.getAllByTestId("button")[0]).toBeInTheDocument();
});
});
});