Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(deps): bump react-router-dom from 6.28.0 to 7.0.1 in /client #27

Merged
3 changes: 0 additions & 3 deletions .github/workflows/client-test.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
name: Client Test Workflow

on:
push:
branches:
- main
pull_request:
branches:
- main
Expand Down
72 changes: 46 additions & 26 deletions client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"lucide-react": "^0.440.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-router-dom": "^6.26.2",
"react-router-dom": "^7.0.1",
"react-tooltip": "^5.28.0",
"sonner": "^1.5.0",
"zustand": "^5.0.1"
Expand Down
76 changes: 39 additions & 37 deletions client/src/guards/auth-guard.test.tsx
Original file line number Diff line number Diff line change
@@ -1,62 +1,64 @@
import {
createMemoryRouter,
RouteObject,
RouterProvider,
} from "react-router-dom";
import { describe, it, expect, vi, Mock, beforeAll } from "vitest";
import { render } from "@testing-library/react";
import { MemoryRouter, Route, Routes, useLocation } from "react-router-dom";
import { render, waitFor, screen } from "@testing-library/react";
import { describe, it, expect, vi, Mock } from "vitest";

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

import AuthGuard from "./auth-guard";

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

const routes: RouteObject[] = [
{
path: AppRoutes.Root,
element: <div>Root</div>,
},
{
element: <AuthGuard />,
children: [
{
path: AppRoutes.SignIn,
element: <div>Sign In</div>,
},
],
},
];
// Helper to track the current route location
function LocationDisplay() {
const location = useLocation();
return <div data-testid="location">{location.pathname}</div>;
}

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

beforeAll(() => {
it("redirects to the root page when authorized", async () => {
(useAuthStore as unknown as Mock).mockReturnValueOnce({
authorized: true,
});

router = createMemoryRouter(routes, {
initialEntries: [AppRoutes.SignIn],
});
render(
<MemoryRouter initialEntries={[AppRoutes.SignIn]}>
<Routes>
<Route path={AppRoutes.Root} element={<div>Root</div>} />
<Route element={<AuthGuard />}>
<Route path={AppRoutes.SignIn} element={<div>Sign In</div>} />
</Route>
</Routes>
<LocationDisplay /> {/* Track current location */}
</MemoryRouter>
);

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

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

it("renders the outlet when not authorized", () => {
it("stays on /sign-in when not authorized", async () => {
(useAuthStore as unknown as Mock).mockReturnValueOnce({
authorized: false,
});

render(<RouterProvider router={router} />);
render(
<MemoryRouter initialEntries={[AppRoutes.SignIn]}>
<Routes>
<Route path={AppRoutes.Root} element={<div>Root</div>} />
<Route element={<AuthGuard />}>
<Route path={AppRoutes.SignIn} element={<div>Sign In</div>} />
</Route>
</Routes>
<LocationDisplay /> {/* Track current location */}
</MemoryRouter>
);

expect(router);
await waitFor(() => {
expect(screen.getByTestId("location").textContent).toBe(AppRoutes.SignIn);
});
});
});
Loading