import "@testing-library/jest-dom"; import React from "react"; import { fireEvent, render, screen, within } from "@testing-library/react"; import { createMemoryRouter, RouterProvider } from "react-router"; import { beforeAll, describe, expect, it, vi } from "vitest"; import { UI_ACTION_IDS } from "~/application/ui/uiRegistry"; import { SidebarProvider } from "~/components/ui/sidebar"; import { NavActions } from "./nav-actions"; const fetcherSubmit = vi.fn(); beforeAll(() => { Object.defineProperty(window, "matchMedia", { writable: true, value: vi.fn().mockImplementation((query: string) => ({ matches: false, media: query, onchange: null, addEventListener: vi.fn(), removeEventListener: vi.fn(), addListener: vi.fn(), removeListener: vi.fn(), dispatchEvent: vi.fn(), })), }); }); vi.mock("react-i18next", () => ({ useTranslation: () => ({ t: (value: string) => value }), })); vi.mock("~/utils/data/useRootData", () => ({ useRootData: () => ({ appConfiguration: { app: { features: { tenantFeedback: true } }, notifications: { enabled: false }, onboarding: { enabled: false }, }, chatWebsiteId: null, userSession: { lightOrDarkMode: "light" }, }), })); vi.mock("~/utils/data/useAppOrAdminData", () => ({ useAppOrAdminData: () => ({ currentTenant: { id: "tenant-1", slug: "acme" }, entities: [], permissions: ["app.settings.subscription.view", "app.settings.subscription.update"], user: { id: "user-1" }, onboardingSession: null, }), })); vi.mock("~/utils/data/useAppData", () => ({ useAppData: () => ({ mySubscription: { products: [] } }), })); vi.mock("react-router", async () => { const actual = await vi.importActual("react-router"); return { ...actual, useFetcher: () => ({ data: undefined, submit: fetcherSubmit, Form: (props: React.ComponentProps<"form">) =>
, }), }; }); function renderNavActions({ layout, initialEntry }: { layout: "admin" | "app" | "docs"; initialEntry: string }) { const element = ( ); const router = createMemoryRouter( [ { path: "/:tenant/*", element }, { path: "/*", element }, ], { initialEntries: [initialEntry] } ); return render(); } describe("NavActions UI IDs", () => { it("renders stable header action IDs for search and more", () => { renderNavActions({ layout: "app", initialEntry: "/acme/dashboard" }); expect(document.querySelector(`[data-ui-id="${UI_ACTION_IDS.search}"]`)).toBeInTheDocument(); expect(document.querySelector(`[data-ui-id="${UI_ACTION_IDS.more}"]`)).toBeInTheDocument(); }); it("keeps icon-only header actions named, focusable, and mobile-scroll safe", () => { renderNavActions({ layout: "app", initialEntry: "/acme/dashboard" }); const search = screen.getByRole("button", { name: "shared.search" }); const more = screen.getByRole("button", { name: "shared.openMenu" }); const feedback = screen.getByRole("button", { name: "feedback.title" }); const actionStrip = search.parentElement; expect(actionStrip).toHaveClass("min-w-0", "max-w-[calc(100vw-4rem)]", "overflow-x-auto", "md:overflow-visible"); expect(search).toHaveClass("size-10", "shrink-0", "focus-visible:ring-[3px]"); expect(more).toHaveClass("size-10", "shrink-0", "data-[state=open]:bg-accent"); expect(feedback).toHaveClass("size-10", "shrink-0"); // The decorative search icon (a Subframe Feather icon: ) // is hidden from assistive tech at the icon wrapper, which contains the svg. const hiddenSearchIcon = search.querySelector("[aria-hidden='true']"); expect(hiddenSearchIcon).not.toBeNull(); expect(hiddenSearchIcon?.querySelector("svg")).not.toBeNull(); }); }); describe("NavActions host-aware links", () => { it("renders tenant profile and subscription paths without legacy app prefix", () => { renderNavActions({ layout: "app", initialEntry: "/acme/dashboard" }); fireEvent.click(document.querySelector(`[data-ui-id="${UI_ACTION_IDS.more}"]`)!); const profile = screen.getByRole("link", { name: /app\.navbar\.profile/i }); const subscription = screen.getByRole("link", { name: /app\.navbar\.subscription/i }); expect(profile).toHaveAttribute("href", "/acme/settings/profile"); expect(subscription).toHaveAttribute("href", "/acme/settings/subscription"); expect(profile.getAttribute("href")).not.toContain("/app/"); expect(subscription.getAttribute("href")).not.toContain("/app/"); }); it("renders admin profile path without legacy admin prefix", () => { renderNavActions({ layout: "admin", initialEntry: "/dashboard" }); fireEvent.click(document.querySelector(`[data-ui-id="${UI_ACTION_IDS.more}"]`)!); const profile = screen.getByRole("link", { name: /app\.navbar\.profile/i }); expect(profile).toHaveAttribute("href", "/settings/profile"); expect(profile.getAttribute("href")).not.toContain("/admin/settings"); }); it("submits feedback to the tenant-root action without legacy app prefix", () => { renderNavActions({ layout: "app", initialEntry: "/acme/dashboard" }); fireEvent.click(document.querySelector(`[data-ui-id="${UI_ACTION_IDS.feedback}"]`)!); fireEvent.change(screen.getByPlaceholderText("feedback.placeholder"), { target: { value: "Helpful feedback" } }); fireEvent.submit(within(screen.getByRole("dialog")).getByRole("button", { name: "feedback.send" }).closest("form")!); expect(fetcherSubmit).toHaveBeenCalledWith(expect.any(HTMLFormElement), { method: "post", action: "/acme", }); expect(fetcherSubmit.mock.calls[0][1].action).not.toContain("/app/"); }); });