Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/routes/app.$tenant/settings.workspace-shell.test.tsx
T
2026-07-08 16:15:37 +02:00

156 lines
8.0 KiB
TypeScript

// @vitest-environment jsdom
import "@testing-library/jest-dom";
import { render, screen } from "@testing-library/react";
import { beforeEach, describe, expect, it, vi } from "vitest";
vi.mock("react-i18next", () => ({
useTranslation: () => ({ t: (key: string) => key }),
}));
vi.mock("~/locale/i18next.server", () => ({
getTranslations: vi.fn(async () => ({ t: (key: string) => key })),
}));
vi.mock("~/utils/app/UrlUtils", () => ({
default: {
currentTenantUrl: (params: { tenant?: string }, path: string) => `/${params.tenant}/${path}`,
stripTrailingSlash: (value: string) => value.replace(/\/$/, ""),
},
}));
const navigate = vi.fn();
let pathname = "/acme/settings";
vi.mock("react-router", async () => {
const actual = await vi.importActual<typeof import("react-router")>("react-router");
return {
...actual,
Outlet: () => <div data-testid="settings-outlet" />,
useLoaderData: () => ({ title: "Settings | HiveGrid", isProduction: false }),
useLocation: () => ({ pathname }),
useNavigate: () => navigate,
useParams: () => ({ tenant: "acme" }),
};
});
vi.mock("~/components/ui/layouts/SidebarIconsLayout", () => ({
default: ({ children, items }: { children: React.ReactNode; items: Array<{ name: string; href: string }> }) => (
<nav aria-label="Workspace settings">
{items.map((item) => (
<a key={item.href} href={item.href}>
{item.name}
</a>
))}
{children}
</nav>
),
}));
vi.mock("~/components/ui/icons/settings/CustomerIcon", () => ({ default: () => null }));
vi.mock("~/components/ui/icons/settings/CustomerIconFilled", () => ({ default: () => null }));
vi.mock("~/components/ui/icons/settings/PeopleIcon", () => ({ default: () => null }));
vi.mock("~/components/ui/icons/settings/PeopleIconFilled", () => ({ default: () => null }));
vi.mock("~/components/ui/icons/settings/MembershipCardIcon", () => ({ default: () => null }));
vi.mock("~/components/ui/icons/settings/MembershipCardIconFilled", () => ({ default: () => null }));
vi.mock("~/components/ui/icons/crm/CompanyIcon", () => ({ default: () => null }));
vi.mock("~/components/ui/icons/crm/CompanyIconFilled", () => ({ default: () => null }));
vi.mock("~/components/ui/icons/crud/LinkIcon", () => ({ default: () => null }));
vi.mock("~/components/ui/icons/crud/LinkIconFilled", () => ({ default: () => null }));
vi.mock("~/components/ui/icons/entities/RestApiIcon", () => ({ default: () => null }));
vi.mock("~/components/ui/icons/entities/RestApiIconFilled", () => ({ default: () => null }));
vi.mock("~/components/ui/icons/entities/ActivityHistoryIcon", () => ({ default: () => null }));
vi.mock("~/components/ui/icons/entities/ActivityHistoryIconFilled", () => ({ default: () => null }));
vi.mock("~/components/ui/icons/entities/ModulesIcon", () => ({ default: () => null }));
vi.mock("~/components/ui/icons/entities/ModulesIconFilled", () => ({ default: () => null }));
vi.mock("~/components/ui/icons/CreditsIcon", () => ({ default: () => null }));
vi.mock("~/components/ui/icons/CreditsIconFilled", () => ({ default: () => null }));
vi.mock("lucide-react", () => ({ BellIcon: () => null, FileTextIcon: () => null, PaintbrushIcon: () => null, PlugIcon: () => null, ShieldCheckIcon: () => null }));
let grantedPermissions = new Set<string>();
vi.mock("~/utils/data/useAppOrAdminData", () => ({ useAppOrAdminData: () => ({ isSuperUser: true, permissions: Array.from(grantedPermissions) }) }));
vi.mock("~/utils/data/useRootData", () => ({
useRootData: () => ({
appConfiguration: {
app: {
features: {
tenantApiKeys: true,
tenantEntityCustomization: true,
},
},
},
}),
}));
vi.mock("~/utils/helpers/PermissionsHelper", () => ({ getUserHasPermission: (_data: unknown, permission: string) => grantedPermissions.has(permission) }));
vi.mock("~/modules/usage/dtos/CreditType", () => ({ CreditTypes: [{ name: "credits" }] }));
import SettingsRoute from "./settings";
describe("workspace settings shell", () => {
beforeEach(() => {
navigate.mockClear();
pathname = "/acme/settings/profile";
grantedPermissions = new Set([
"app.settings.members.view",
"app.settings.subscription.view",
"app.settings.account.view",
"app.settings.roles.view",
"app.settings.apiKeys.view",
"app.settings.auditTrails.view",
"app.settings.account.update",
"app.plugins.use",
]);
});
it("renders the tenant settings navigation and outlet", () => {
render(<SettingsRoute />);
expect(screen.getByRole("navigation", { name: "Workspace settings" })).toBeInTheDocument();
expect(screen.getByRole("link", { name: "settings.profile.profileTitle" })).toHaveAttribute("href", "/acme/settings/profile");
expect(screen.getByRole("link", { name: "settings.members.title" })).toHaveAttribute("href", "/acme/settings/members");
expect(screen.getByRole("link", { name: "settings.subscription.title" })).toHaveAttribute("href", "/acme/settings/subscription");
expect(screen.getByRole("link", { name: "billing:usage.title" })).toHaveAttribute("href", "/acme/settings/usage");
expect(screen.getByRole("link", { name: "settings.tenant.title" })).toHaveAttribute("href", "/acme/settings/account");
expect(screen.getByRole("link", { name: "settings.theme.title" })).toHaveAttribute("href", "/acme/settings/theme");
expect(screen.queryByRole("link", { name: "settings.forms.title" })).not.toBeInTheDocument();
// R-P2b (D-P2 S01/S02): roles are a tab INSIDE members — no separate icon tab when members.view is granted.
expect(screen.queryByRole("link", { name: "models.role.plural" })).not.toBeInTheDocument();
expect(screen.getByRole("link", { name: "settings.notifications.title" })).toHaveAttribute("href", "/acme/settings/notifications");
expect(screen.getByRole("link", { name: "settings.policies.title" })).toHaveAttribute("href", "/acme/settings/policies");
expect(screen.getByRole("link", { name: "settings.api.title" })).toHaveAttribute("href", "/acme/settings/api");
expect(screen.getByRole("link", { name: "models.log.plural" })).toHaveAttribute("href", "/acme/settings/logs");
expect(screen.getByRole("link", { name: "settings.plugins.title" })).toHaveAttribute("href", "/acme/settings/plugins");
expect(screen.getByRole("link", { name: "models.entity.plural" })).toHaveAttribute("href", "/acme/settings/entities");
expect(screen.getByTestId("settings-outlet")).toBeInTheDocument();
});
it("keeps roles hidden without role settings permission while preserving public workspace settings tabs", () => {
grantedPermissions = new Set(["app.settings.account.view"]);
render(<SettingsRoute />);
expect(screen.queryByRole("link", { name: "models.role.plural" })).not.toBeInTheDocument();
expect(screen.getByRole("link", { name: "settings.profile.profileTitle" })).toHaveAttribute("href", "/acme/settings/profile");
expect(screen.queryByRole("link", { name: "settings.forms.title" })).not.toBeInTheDocument();
expect(screen.getByRole("link", { name: "settings.notifications.title" })).toHaveAttribute("href", "/acme/settings/notifications");
expect(screen.getByRole("link", { name: "settings.policies.title" })).toHaveAttribute("href", "/acme/settings/policies");
expect(screen.queryByRole("link", { name: "models.entity.plural" })).not.toBeInTheDocument();
});
it("keeps a roles fallback tab reachable when the user may see roles but not members", () => {
grantedPermissions = new Set(["app.settings.roles.view"]);
render(<SettingsRoute />);
// Without members.view the S02 tab bar is unreachable — the fallback icon tab covers DoD B6.
expect(screen.queryByRole("link", { name: "settings.members.title" })).not.toBeInTheDocument();
expect(screen.getByRole("link", { name: "models.role.plural" })).toHaveAttribute("href", "/acme/settings/roles-and-permissions");
});
it("redirects the bare tenant settings path to the profile tab", () => {
pathname = "/acme/settings";
render(<SettingsRoute />);
expect(navigate).toHaveBeenCalledWith("/acme/settings/profile");
});
});