Skip to content

Commit

Permalink
Add tests for guard components
Browse files Browse the repository at this point in the history
- Created unit tests for `AuthGuard` and `PrivateGuard`
  components to ensure proper functionality.
- Verified that guards correctly redirect unauthorized users
  and allow access for authorized users.
  • Loading branch information
Fingertips18 committed Sep 28, 2024
1 parent 02cd424 commit c140ffc
Show file tree
Hide file tree
Showing 2 changed files with 184 additions and 0 deletions.
62 changes: 62 additions & 0 deletions client/src/guards/auth-guard.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import {
createMemoryRouter,
RouteObject,
RouterProvider,
} from "react-router-dom";
import { describe, it, expect, vi, Mock, beforeAll } from "vitest";
import { render } from "@testing-library/react";

import { useAuthStore } from "@/lib/stores/auth-store";
import { AppRoutes } from "@/constants/routes";

import AuthGuard from "./auth-guard";

vi.mock("../lib/stores/auth-store.ts", () => ({
useAuthStore: vi.fn(),
}));

const routes: RouteObject[] = [
{
path: AppRoutes.Root,
element: <div>Root</div>,
},
{
element: <AuthGuard />,
children: [
{
path: AppRoutes.SignIn,
element: <div>Sign In</div>,
},
],
},
];

describe("Auth Guard", () => {
let router: ReturnType<typeof createMemoryRouter>;

beforeAll(() => {
(useAuthStore as unknown as Mock).mockReturnValueOnce({
authorized: true,
});

router = createMemoryRouter(routes, {
initialEntries: [AppRoutes.SignIn],
});

render(<RouterProvider router={router} />);
});

it("redirects to the root page when authorized", () => {
expect(router.state.location.pathname).toBe(AppRoutes.Root);
});

it("renders the outlet when not authorized", () => {
(useAuthStore as unknown as Mock).mockReturnValueOnce({
authorized: false,
});

render(<RouterProvider router={router} />);

expect(router);
});
});
122 changes: 122 additions & 0 deletions client/src/guards/private-guard.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import {
createMemoryRouter,
RouteObject,
RouterProvider,
} from "react-router-dom";
import { describe, it, expect, vi, Mock } from "vitest";
import { useQuery } from "@tanstack/react-query";
import { render } from "@testing-library/react";
import "@testing-library/jest-dom";

import { useAuthStore } from "@/lib/stores/auth-store";
import { AppRoutes } from "@/constants/routes";

import PrivateGuard from "./private-guard";

vi.mock("@/lib/stores/auth-store", () => ({
useAuthStore: vi.fn().mockReturnValueOnce({
authorized: false,
setAuthorized: vi.fn(),
}),
}));

vi.mock("@tanstack/react-query", () => ({
useQuery: vi.fn().mockReturnValueOnce({
isLoading: false,
isError: false,
isSuccess: false,
}),
}));

const routes: RouteObject[] = [
{
path: AppRoutes.SignIn,
element: <div>Sign In</div>,
},
{
element: <PrivateGuard />,
children: [
{
path: AppRoutes.Root,
element: <div>Root</div>,
},
],
},
];

describe("Private Guard", () => {
it("redirects to sign-in page when not authorized", () => {
const router = createMemoryRouter(routes, {
initialEntries: [AppRoutes.Root],
});

render(<RouterProvider router={router} />);

expect(router.state.location.pathname).toEqual(AppRoutes.SignIn);
});

it("redirects to sign in page when an error occurs", () => {
const router = createMemoryRouter(routes, {
initialEntries: [AppRoutes.Root],
});

(useAuthStore as unknown as Mock).mockReturnValueOnce({
authenticated: true,
setAuthorized: vi.fn(),
});

(useQuery as Mock).mockReturnValueOnce({
isError: true,
isLoading: false,
isSuccess: false,
});

render(<RouterProvider router={router} />);

expect(router.state.location.pathname).toEqual(AppRoutes.SignIn);
});

it("allows access to root page when authorized", () => {
const router = createMemoryRouter(routes, {
initialEntries: [AppRoutes.Root],
});

(useAuthStore as unknown as Mock).mockReturnValueOnce({
authorized: true,
setAuthorized: vi.fn(),
});

(useQuery as Mock).mockReturnValueOnce({
isError: false,
isLoading: false,
isSuccess: true,
});

render(<RouterProvider router={router} />);

expect(router.state.location.pathname).toEqual(AppRoutes.Root);
});

it("renders loading component when query is loading", () => {
const router = createMemoryRouter(routes, {
initialEntries: [AppRoutes.Root],
});

(useAuthStore as unknown as Mock).mockReturnValueOnce({
authorized: true,
setAuthorized: vi.fn(),
});

(useQuery as Mock).mockReturnValueOnce({
isError: false,
isLoading: true,
isSuccess: false,
});

const { container } = render(<RouterProvider router={router} />);

const loader = container.querySelector("svg");

expect(loader).toBeInTheDocument();
});
});

0 comments on commit c140ffc

Please sign in to comment.