Bisher gingen "/" und unbekannte URLs immer erst zu /dashboard, das dann bei fehlendem Login zu /login weiterleitete - unnoetiger Umweg. Beide pruefen jetzt selbst den Auth-Status (gleiches Muster wie (app)/layout.tsx: waehrend isLoading nichts tun, danach je nach User direkt zu /dashboard oder /login).
38 lines
1.1 KiB
TypeScript
38 lines
1.1 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
import { render } from "@testing-library/react";
|
|
import NotFound from "./not-found";
|
|
|
|
const replaceMock = vi.fn();
|
|
vi.mock("next/navigation", () => ({
|
|
useRouter: () => ({ replace: replaceMock }),
|
|
}));
|
|
|
|
let mockAuth: { user: { id: string } | null; isLoading: boolean };
|
|
vi.mock("@/lib/auth/auth-context", () => ({
|
|
useAuth: () => mockAuth,
|
|
}));
|
|
|
|
describe("NotFound page", () => {
|
|
beforeEach(() => {
|
|
replaceMock.mockReset();
|
|
});
|
|
|
|
it("does not redirect while the auth check is still loading", () => {
|
|
mockAuth = { user: null, isLoading: true };
|
|
render(<NotFound />);
|
|
expect(replaceMock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("redirects to the dashboard when logged in", () => {
|
|
mockAuth = { user: { id: "user-1" }, isLoading: false };
|
|
render(<NotFound />);
|
|
expect(replaceMock).toHaveBeenCalledWith("/dashboard");
|
|
});
|
|
|
|
it("redirects to login when not logged in", () => {
|
|
mockAuth = { user: null, isLoading: false };
|
|
render(<NotFound />);
|
|
expect(replaceMock).toHaveBeenCalledWith("/login");
|
|
});
|
|
});
|