215 lines
8.2 KiB
TypeScript
215 lines
8.2 KiB
TypeScript
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(
|
|
<MemoryRouter initialEntries={["/acme/g/sales/contacts"]}>
|
|
<SidebarProvider>
|
|
<NavMain
|
|
items={[
|
|
{
|
|
id: "ui.nav.app.primary",
|
|
title: "Primary",
|
|
type: "main",
|
|
items: [
|
|
{
|
|
id: UI_NAV_IDS.appDashboard,
|
|
title: "Dashboard",
|
|
path: "/acme/dashboard",
|
|
},
|
|
{
|
|
id: "ui.nav.app.entityGroup.sales",
|
|
uiIdPattern: UI_ID_PATTERNS.appEntityGroup,
|
|
uiIdParams: { groupSlug: "sales" },
|
|
title: "Sales",
|
|
path: "/acme/g/sales",
|
|
isActive: true,
|
|
items: [
|
|
{
|
|
id: "ui.nav.app.entityGroup.sales.entity.contacts",
|
|
uiIdPattern: UI_ID_PATTERNS.appEntityGroupEntity,
|
|
uiIdParams: { groupSlug: "sales", entityName: "contacts" },
|
|
title: "Contacts",
|
|
path: "/acme/g/sales/contacts",
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
]}
|
|
/>
|
|
</SidebarProvider>
|
|
</MemoryRouter>
|
|
);
|
|
|
|
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(
|
|
<MemoryRouter initialEntries={["/acme/accounts/contacts"]}>
|
|
<SidebarProvider>
|
|
<NavMain
|
|
items={[
|
|
{
|
|
id: "ui.nav.app.primary",
|
|
title: "Primary",
|
|
type: "main",
|
|
items: [
|
|
{
|
|
id: "ui.nav.app.accounts",
|
|
title: "Accounts",
|
|
path: "/acme/accounts",
|
|
isActive: true,
|
|
items: [
|
|
{
|
|
id: "ui.nav.app.accounts.contacts",
|
|
title: "Contacts",
|
|
path: "/acme/accounts/contacts",
|
|
isActive: true,
|
|
},
|
|
],
|
|
},
|
|
{
|
|
id: "ui.nav.app.accounting",
|
|
title: "Accounting",
|
|
path: "/acme/accounting",
|
|
},
|
|
],
|
|
},
|
|
]}
|
|
/>
|
|
</SidebarProvider>
|
|
</MemoryRouter>
|
|
);
|
|
|
|
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(
|
|
<MemoryRouter initialEntries={["/admin/settings/general"]}>
|
|
<SidebarProvider>
|
|
<NavMain
|
|
items={[
|
|
{
|
|
id: "ui.nav.admin.primary",
|
|
title: "Primary",
|
|
type: "main",
|
|
items: [
|
|
{
|
|
id: "ui.nav.admin.settings",
|
|
title: "Settings",
|
|
path: "/settings",
|
|
isActive: true,
|
|
isExpandedByHref: true,
|
|
items: [
|
|
{
|
|
id: "ui.nav.admin.settings.general",
|
|
title: "General",
|
|
path: "/settings/general",
|
|
},
|
|
],
|
|
},
|
|
],
|
|
},
|
|
]}
|
|
/>
|
|
</SidebarProvider>
|
|
</MemoryRouter>
|
|
);
|
|
|
|
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(
|
|
<MemoryRouter initialEntries={["/settings/profile"]}>
|
|
<SidebarProvider>
|
|
<NavMain
|
|
items={[
|
|
{
|
|
title: "Primary",
|
|
type: "main",
|
|
items: [
|
|
{
|
|
title: "Settings",
|
|
path: "/settings",
|
|
isActive: true,
|
|
items: [
|
|
{ title: "Profile", path: "/settings", redirectTo: "/settings/profile" },
|
|
{ title: "Disabled Plugin", path: "/disabled-plugin", disabled: true },
|
|
],
|
|
},
|
|
{ title: "Disabled Root", path: "/disabled-root", disabled: true },
|
|
],
|
|
},
|
|
]}
|
|
/>
|
|
</SidebarProvider>
|
|
</MemoryRouter>
|
|
);
|
|
|
|
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(
|
|
<MemoryRouter initialEntries={["/settings"]}>
|
|
<SidebarProvider>
|
|
<NavMain
|
|
items={[
|
|
{
|
|
title: "Primary",
|
|
type: "main",
|
|
items: [
|
|
{
|
|
title: "Settings",
|
|
path: "/settings",
|
|
items: [{ title: "Disabled Plugin", path: "/settings/plugin", disabled: true }],
|
|
},
|
|
],
|
|
},
|
|
]}
|
|
/>
|
|
</SidebarProvider>
|
|
</MemoryRouter>
|
|
);
|
|
|
|
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();
|
|
});
|
|
});
|