fix: Root und 404 leiten unangemeldete User direkt zu /login
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).
This commit is contained in:
1 parent
b3157484c4
commit
6776bdfe73
4 files changed
+98
-10
No files matched your search
@@ -1,14 +1,37 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { redirect } from "next/navigation";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { render } from "@testing-library/react";
|
||||
import Page from "./page";
|
||||
|
||||
const replaceMock = vi.fn();
|
||||
vi.mock("next/navigation", () => ({
|
||||
redirect: vi.fn(),
|
||||
useRouter: () => ({ replace: replaceMock }),
|
||||
}));
|
||||
|
||||
let mockAuth: { user: { id: string } | null; isLoading: boolean };
|
||||
vi.mock("@/lib/auth/auth-context", () => ({
|
||||
useAuth: () => mockAuth,
|
||||
}));
|
||||
|
||||
describe("Home page", () => {
|
||||
it("redirects to the dashboard", () => {
|
||||
Page();
|
||||
expect(redirect).toHaveBeenCalledWith("/dashboard");
|
||||
beforeEach(() => {
|
||||
replaceMock.mockReset();
|
||||
});
|
||||
|
||||
it("does not redirect while the auth check is still loading", () => {
|
||||
mockAuth = { user: null, isLoading: true };
|
||||
render(<Page />);
|
||||
expect(replaceMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("redirects to the dashboard when logged in", () => {
|
||||
mockAuth = { user: { id: "user-1" }, isLoading: false };
|
||||
render(<Page />);
|
||||
expect(replaceMock).toHaveBeenCalledWith("/dashboard");
|
||||
});
|
||||
|
||||
it("redirects to login when not logged in", () => {
|
||||
mockAuth = { user: null, isLoading: false };
|
||||
render(<Page />);
|
||||
expect(replaceMock).toHaveBeenCalledWith("/login");
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user