|
| 1 | +/** |
| 2 | + * @jest-environment jsdom |
| 3 | + */ |
| 4 | + |
| 5 | +import { render, screen, waitFor } from "@testing-library/react" |
| 6 | +import WorkspacePage from "@/app/workspaces/[workspaceId]/page" |
| 7 | + |
| 8 | +const mockRouterReplace = jest.fn() |
| 9 | +const mockUseScopeCheck = jest.fn<boolean | undefined, [string]>() |
| 10 | +let mockScopes: Record<string, boolean | undefined> = {} |
| 11 | + |
| 12 | +jest.mock("next/navigation", () => ({ |
| 13 | + useRouter: () => ({ replace: mockRouterReplace }), |
| 14 | +})) |
| 15 | + |
| 16 | +jest.mock("next/image", () => ({ |
| 17 | + __esModule: true, |
| 18 | + default: (props: { |
| 19 | + src: string |
| 20 | + alt: string |
| 21 | + className?: string |
| 22 | + }): JSX.Element => <img alt={props.alt} className={props.className} />, |
| 23 | +})) |
| 24 | + |
| 25 | +jest.mock("@/components/auth/scope-guard", () => ({ |
| 26 | + useScopeCheck: (scope: string) => mockUseScopeCheck(scope), |
| 27 | +})) |
| 28 | + |
| 29 | +jest.mock("@/components/loading/spinner", () => ({ |
| 30 | + CenteredSpinner: () => <div>Loading</div>, |
| 31 | +})) |
| 32 | + |
| 33 | +jest.mock("@/hooks", () => ({ |
| 34 | + useEntitlements: () => ({ |
| 35 | + hasEntitlement: () => false, |
| 36 | + isLoading: false, |
| 37 | + }), |
| 38 | +})) |
| 39 | + |
| 40 | +jest.mock("@/providers/workspace-id", () => ({ |
| 41 | + useWorkspaceId: () => "workspace-1", |
| 42 | +})) |
| 43 | + |
| 44 | +describe("WorkspacePage", () => { |
| 45 | + beforeEach(() => { |
| 46 | + mockRouterReplace.mockReset() |
| 47 | + mockUseScopeCheck.mockReset() |
| 48 | + mockScopes = {} |
| 49 | + mockUseScopeCheck.mockImplementation((scope) => mockScopes[scope] ?? false) |
| 50 | + }) |
| 51 | + |
| 52 | + it("does not redirect service-account-only users into the workspace shell", () => { |
| 53 | + mockScopes = { |
| 54 | + "workspace:service_account:read": true, |
| 55 | + "workspace:read": false, |
| 56 | + } |
| 57 | + |
| 58 | + render(<WorkspacePage />) |
| 59 | + |
| 60 | + expect(mockRouterReplace).not.toHaveBeenCalled() |
| 61 | + expect(screen.getByText("No accessible pages")).toBeInTheDocument() |
| 62 | + }) |
| 63 | + |
| 64 | + it("redirects to service accounts when the workspace shell is readable", async () => { |
| 65 | + mockScopes = { |
| 66 | + "workspace:service_account:read": true, |
| 67 | + "workspace:read": true, |
| 68 | + } |
| 69 | + |
| 70 | + render(<WorkspacePage />) |
| 71 | + |
| 72 | + await waitFor(() => { |
| 73 | + expect(mockRouterReplace).toHaveBeenCalledWith( |
| 74 | + "/workspaces/workspace-1/service-accounts" |
| 75 | + ) |
| 76 | + }) |
| 77 | + }) |
| 78 | +}) |
0 commit comments