add: tests
This commit is contained in:
31
tests/PermissionGuard.test.js
Normal file
31
tests/PermissionGuard.test.js
Normal file
@@ -0,0 +1,31 @@
|
||||
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();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user