31 lines
1.2 KiB
JavaScript
31 lines
1.2 KiB
JavaScript
import React from "react";
|
|
import PermissionGuard from "../src/components/PermissionGuard";
|
|
import { AuthContext } from "../src/AuthProvider";
|
|
import { render, screen } from "@testing-library/react";
|
|
describe("PermissionGuard", () => {
|
|
it("should render children for users with required roles", () => {
|
|
const roles = ["admin"];
|
|
render(
|
|
<AuthContext.Provider value={{ roles }}>
|
|
<PermissionGuard rolesRequired={["admin"]}>
|
|
<div data-testid="protected-content">Protected Content</div>
|
|
</PermissionGuard>
|
|
</AuthContext.Provider>
|
|
);
|
|
|
|
expect(screen.getByTestId("protected-content")).toBeInTheDocument();
|
|
});
|
|
|
|
it("should not render children for users without required roles", () => {
|
|
const roles = ["user"];
|
|
render(
|
|
<AuthContext.Provider value={{ roles }}>
|
|
<PermissionGuard rolesRequired={["admin"]}>
|
|
<div data-testid="protected-content">Protected Content</div>
|
|
</PermissionGuard>
|
|
</AuthContext.Provider>
|
|
);
|
|
|
|
expect(screen.queryByTestId("protected-content")).not.toBeInTheDocument();
|
|
});
|
|
}); |