import "@testing-library/jest-dom"; import { render, screen } from "@testing-library/react"; import { MemoryRouter } from "react-router"; import { describe, expect, it, vi } from "vitest"; import { SidebarProvider } from "~/components/ui/sidebar"; import { UI_ID_PATTERNS, UI_NAV_IDS } from "~/application/ui/uiRegistry"; import { NavMain } from "./nav-main"; vi.mock("~/hooks/use-mobile", () => ({ useIsMobile: () => false, })); describe("NavMain UI IDs", () => { it("renders group, static item and dynamic child data-ui-id attributes", () => { render( ); expect(document.querySelector('[data-ui-id="ui.nav.app.primary"]')).toBeInTheDocument(); expect(screen.getByRole("link", { name: "Dashboard" })).toHaveAttribute("data-ui-id", UI_NAV_IDS.appDashboard); // Subframe flat sidebar: an expandable parent is a non-interactive section header — not a button, not a link. expect(document.querySelector('[data-ui-id="ui.nav.app.entityGroup.sales"]')).toBeInTheDocument(); expect(screen.queryByRole("button", { name: "Sales" })).not.toBeInTheDocument(); expect(screen.queryByRole("link", { name: "Sales" })).not.toBeInTheDocument(); expect(screen.getByText("Sales")).toBeInTheDocument(); expect(screen.getByRole("link", { name: "Contacts" })).toHaveAttribute("data-ui-id", "ui.nav.app.entityGroup.sales.entity.contacts"); expect(screen.getByRole("link", { name: "Contacts" })).toHaveAttribute("data-ui-id-pattern", UI_ID_PATTERNS.appEntityGroupEntity); expect(screen.getByRole("link", { name: "Contacts" })).toHaveAttribute("data-ui-id-param-entityname", "contacts"); expect(screen.getByRole("link", { name: "Contacts" })).toHaveAttribute("data-active", "false"); }); it("keeps active host-aware children expanded without duplicate data-ui-id values", () => { render( ); expect(document.querySelector('[data-ui-id="ui.nav.app.accounts"]')).toBeInTheDocument(); expect(screen.queryByRole("button", { name: "Accounts" })).not.toBeInTheDocument(); expect(screen.queryByRole("link", { name: "Accounts" })).not.toBeInTheDocument(); expect(screen.getByRole("link", { name: "Contacts" })).toHaveAttribute("data-active", "true"); expect(screen.getByRole("link", { name: "Accounting" })).toHaveAttribute("data-active", "false"); const ids = [...document.querySelectorAll("[data-ui-id]")].map((element) => element.getAttribute("data-ui-id")); expect(new Set(ids).size).toBe(ids.length); }); it("renders expandable parents as flat, non-interactive section headers", () => { render( ); expect(screen.queryByRole("button", { name: "Settings" })).not.toBeInTheDocument(); expect(screen.queryByRole("link", { name: "Settings" })).not.toBeInTheDocument(); expect(screen.getByText("Settings")).toBeInTheDocument(); expect(screen.getByRole("link", { name: "General" })).toHaveAttribute("href", "/settings/general"); }); it("hides disabled items and honors redirect targets for subitems", () => { render( ); expect(screen.getByRole("link", { name: "Profile" })).toHaveAttribute("href", "/settings/profile"); expect(screen.queryByRole("link", { name: "Disabled Plugin" })).not.toBeInTheDocument(); expect(screen.queryByRole("link", { name: "Disabled Root" })).not.toBeInTheDocument(); }); it("does not turn parents with only disabled children into links", () => { render( ); expect(screen.queryByRole("link", { name: "Settings" })).not.toBeInTheDocument(); expect(screen.queryByRole("button", { name: /Settings/ })).not.toBeInTheDocument(); expect(screen.queryByRole("link", { name: "Disabled Plugin" })).not.toBeInTheDocument(); }); });