|
| 1 | +import { AppRoutes } from "@/app/AppRoutes"; |
| 2 | +import { render, screen } from "@testing-library/react"; |
| 3 | +import { MemoryRouter } from "react-router-dom"; |
| 4 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 5 | + |
| 6 | +const authState = vi.hoisted(() => ({ |
| 7 | + value: { |
| 8 | + user: null as { id: string; email: string } | null, |
| 9 | + isLoading: false, |
| 10 | + }, |
| 11 | +})); |
| 12 | + |
| 13 | +vi.mock("@/domains/auth/application/AuthContext", () => ({ |
| 14 | + useAuth: () => authState.value, |
| 15 | +})); |
| 16 | + |
| 17 | +vi.mock("@/shared/ui/Loading", () => ({ |
| 18 | + default: () => <div role="status">Carregando...</div>, |
| 19 | +})); |
| 20 | + |
| 21 | +vi.mock("@/app/AuthenticatedLayout", async () => { |
| 22 | + const { Outlet } = |
| 23 | + await vi.importActual<typeof import("react-router-dom")>( |
| 24 | + "react-router-dom", |
| 25 | + ); |
| 26 | + |
| 27 | + return { |
| 28 | + AuthenticatedLayout: () => ( |
| 29 | + <div> |
| 30 | + <span>Authenticated shell</span> |
| 31 | + <Outlet /> |
| 32 | + </div> |
| 33 | + ), |
| 34 | + }; |
| 35 | +}); |
| 36 | + |
| 37 | +vi.mock("@/domains/marketing/presentation/pages/LandingPage", () => ({ |
| 38 | + default: () => <main>Landing route</main>, |
| 39 | +})); |
| 40 | + |
| 41 | +vi.mock("@/domains/auth/presentation/pages/LoginPage", () => ({ |
| 42 | + default: () => <main>Login route</main>, |
| 43 | +})); |
| 44 | + |
| 45 | +vi.mock("@/domains/auth/presentation/pages/RegisterPage", () => ({ |
| 46 | + default: () => <main>Register route</main>, |
| 47 | +})); |
| 48 | + |
| 49 | +vi.mock("@/domains/auth/presentation/pages/AuthCallbackPage", () => ({ |
| 50 | + default: () => <main>Callback route</main>, |
| 51 | +})); |
| 52 | + |
| 53 | +vi.mock("@/domains/jobs/presentation/pages/JobsPage", () => ({ |
| 54 | + default: () => <main>Jobs route</main>, |
| 55 | +})); |
| 56 | + |
| 57 | +vi.mock("@/app/NotFound", () => ({ |
| 58 | + default: () => <main>Not found route</main>, |
| 59 | +})); |
| 60 | + |
| 61 | +function renderRoute(path: string) { |
| 62 | + return render( |
| 63 | + <MemoryRouter initialEntries={[path]}> |
| 64 | + <AppRoutes /> |
| 65 | + </MemoryRouter>, |
| 66 | + ); |
| 67 | +} |
| 68 | + |
| 69 | +describe("AppRoutes", () => { |
| 70 | + beforeEach(() => { |
| 71 | + authState.value = { |
| 72 | + user: null, |
| 73 | + isLoading: false, |
| 74 | + }; |
| 75 | + }); |
| 76 | + |
| 77 | + it("renderiza a landing page na rota pública inicial", () => { |
| 78 | + renderRoute("/"); |
| 79 | + |
| 80 | + expect(screen.getByText("Landing route")).toBeInTheDocument(); |
| 81 | + }); |
| 82 | + |
| 83 | + it("mostra loading em rota protegida enquanto a sessão está carregando", () => { |
| 84 | + authState.value = { |
| 85 | + user: null, |
| 86 | + isLoading: true, |
| 87 | + }; |
| 88 | + |
| 89 | + renderRoute("/home"); |
| 90 | + |
| 91 | + expect(screen.getByRole("status")).toHaveTextContent("Carregando..."); |
| 92 | + }); |
| 93 | + |
| 94 | + it("redireciona rota protegida para login quando não há usuário", () => { |
| 95 | + renderRoute("/home"); |
| 96 | + |
| 97 | + expect(screen.getByText("Login route")).toBeInTheDocument(); |
| 98 | + expect(screen.queryByText("Jobs route")).not.toBeInTheDocument(); |
| 99 | + }); |
| 100 | + |
| 101 | + it("renderiza layout autenticado e conteúdo protegido quando há usuário", () => { |
| 102 | + authState.value = { |
| 103 | + user: { id: "1", email: "otavio@example.com" }, |
| 104 | + isLoading: false, |
| 105 | + }; |
| 106 | + |
| 107 | + renderRoute("/vagas"); |
| 108 | + |
| 109 | + expect(screen.getByText("Authenticated shell")).toBeInTheDocument(); |
| 110 | + expect(screen.getByText("Jobs route")).toBeInTheDocument(); |
| 111 | + }); |
| 112 | + |
| 113 | + it("mostra loading em rota pública enquanto a sessão está carregando", () => { |
| 114 | + authState.value = { |
| 115 | + user: null, |
| 116 | + isLoading: true, |
| 117 | + }; |
| 118 | + |
| 119 | + renderRoute("/register"); |
| 120 | + |
| 121 | + expect(screen.getByRole("status")).toHaveTextContent("Carregando..."); |
| 122 | + }); |
| 123 | + |
| 124 | + it("redireciona usuário autenticado para home ao acessar login", () => { |
| 125 | + authState.value = { |
| 126 | + user: { id: "1", email: "otavio@example.com" }, |
| 127 | + isLoading: false, |
| 128 | + }; |
| 129 | + |
| 130 | + renderRoute("/login"); |
| 131 | + |
| 132 | + expect(screen.getByText("Authenticated shell")).toBeInTheDocument(); |
| 133 | + expect(screen.getByText("Jobs route")).toBeInTheDocument(); |
| 134 | + }); |
| 135 | + |
| 136 | + it("mantém callback OAuth fora dos guards de autenticação", () => { |
| 137 | + renderRoute("/auth/callback"); |
| 138 | + |
| 139 | + expect(screen.getByText("Callback route")).toBeInTheDocument(); |
| 140 | + }); |
| 141 | +}); |
0 commit comments