-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 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
1 parent
02cd424
commit c140ffc
Showing
2 changed files
with
184 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |