feat(web): TanStack Query hooks + session-guarded routes
Installs @tanstack/react-query and react-router-dom; adds typed query hooks (useMe, useObjectsPage, useObject, useFieldDefinitions, useLogin, useLogout), a QueryClient+MemoryRouter test render helper, and RequireAuth — a layout route that blocks unauthenticated access and redirects to /login. All 7 tests pass, typecheck/lint/build clean. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
import { screen, waitFor } from "@testing-library/react";
|
||||
import { http, HttpResponse } from "msw";
|
||||
import { expect, test } from "vitest";
|
||||
import { Route, Routes } from "react-router-dom";
|
||||
|
||||
import { server } from "../test/server";
|
||||
import { renderApp } from "../test/render";
|
||||
import { RequireAuth } from "./require-auth";
|
||||
|
||||
function tree() {
|
||||
return (
|
||||
<Routes>
|
||||
<Route path="/login" element={<div>login page</div>} />
|
||||
<Route element={<RequireAuth />}>
|
||||
<Route path="/objects" element={<div>secret objects</div>} />
|
||||
</Route>
|
||||
</Routes>
|
||||
);
|
||||
}
|
||||
|
||||
test("renders children when authenticated", async () => {
|
||||
renderApp(tree(), { route: "/objects" });
|
||||
expect(await screen.findByText("secret objects")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
test("redirects to /login when unauthenticated", async () => {
|
||||
server.use(http.get("/api/admin/me", () => new HttpResponse(null, { status: 401 })));
|
||||
renderApp(tree(), { route: "/objects" });
|
||||
await waitFor(() => expect(screen.getByText("login page")).toBeInTheDocument());
|
||||
});
|
||||
Reference in New Issue
Block a user