add: tests

This commit is contained in:
h z
2024-12-09 08:46:20 +00:00
parent d8da574833
commit 931ade90a3
7 changed files with 3897 additions and 22 deletions

View 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();
});
});