55 lines
1.6 KiB
TypeScript
55 lines
1.6 KiB
TypeScript
import { render, screen } from "@testing-library/react";
|
|
|
|
import { useAccessStore } from "@/store/accessStore";
|
|
import { useAuthStore } from "@/store/authStore";
|
|
import { RoutePermissionGuard } from "./RoutePermissionGuard";
|
|
|
|
jest.mock("next/navigation", () => ({
|
|
usePathname: () => "/network-simulation",
|
|
}));
|
|
|
|
describe("RoutePermissionGuard", () => {
|
|
beforeEach(() => {
|
|
useAccessStore.setState({
|
|
context: null,
|
|
permissions: [],
|
|
loading: false,
|
|
});
|
|
useAuthStore.setState({
|
|
accessToken: null,
|
|
sessionExpired: false,
|
|
sessionExpiryReason: null,
|
|
});
|
|
});
|
|
|
|
it("prioritizes an expired session over a missing route permission", () => {
|
|
useAuthStore.setState({
|
|
sessionExpired: true,
|
|
sessionExpiryReason: "unauthorized",
|
|
});
|
|
|
|
render(
|
|
<RoutePermissionGuard>
|
|
<div>受保护内容</div>
|
|
</RoutePermissionGuard>,
|
|
);
|
|
|
|
expect(screen.getByText("登录状态已失效")).toBeInTheDocument();
|
|
expect(screen.getByText("正在跳转到登录页面…")).toBeInTheDocument();
|
|
expect(screen.queryByText("无权访问此功能")).not.toBeInTheDocument();
|
|
expect(screen.queryByText(/simulation\.view/)).not.toBeInTheDocument();
|
|
expect(screen.queryByText("受保护内容")).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("shows the permission error when the session is still valid", () => {
|
|
render(
|
|
<RoutePermissionGuard>
|
|
<div>受保护内容</div>
|
|
</RoutePermissionGuard>,
|
|
);
|
|
|
|
expect(screen.getByText("无权访问此功能")).toBeInTheDocument();
|
|
expect(screen.getByText(/simulation\.view/)).toBeInTheDocument();
|
|
});
|
|
});
|