36 lines
1.2 KiB
TypeScript
36 lines
1.2 KiB
TypeScript
import { screen, waitFor } from "@testing-library/react";
|
|
import { http, HttpResponse } from "msw";
|
|
import { expect, test } from "vitest";
|
|
import { Route, Routes, useLocation } from "react-router-dom";
|
|
|
|
import { server } from "../test/server";
|
|
import { renderApp } from "../test/render";
|
|
import { RequireAuth } from "./require-auth";
|
|
|
|
function LoginStub() {
|
|
const location = useLocation();
|
|
return <div>login page {location.search}</div>;
|
|
}
|
|
|
|
function tree() {
|
|
return (
|
|
<Routes>
|
|
<Route path="/login" element={<LoginStub />} />
|
|
<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 unauthenticated users to /login carrying the attempted path", async () => {
|
|
server.use(http.get("/api/admin/me", () => new HttpResponse(null, { status: 401 })));
|
|
renderApp(tree(), { route: "/objects" });
|
|
await waitFor(() => expect(screen.getByText(/from=%2Fobjects/)).toBeInTheDocument());
|
|
});
|