aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/client/components/ProtectedRoute.test.tsx
blob: 64a067896f8cdb8afcd5731b76611340576c9e46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
 * @vitest-environment jsdom
 */
import { cleanup, render, screen } from "@testing-library/react";
import { createStore, Provider } from "jotai";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { Router } from "wouter";
import { memoryLocation } from "wouter/memory-location";
import { authLoadingAtom } from "../atoms";
import { ProtectedRoute } from "./ProtectedRoute";

vi.mock("../api/client", () => ({
	apiClient: {
		login: vi.fn(),
		logout: vi.fn(),
		isAuthenticated: vi.fn(),
		getTokens: vi.fn(),
		onSessionExpired: vi.fn(() => vi.fn()),
	},
	ApiClientError: class ApiClientError extends Error {
		constructor(
			message: string,
			public status: number,
			public code?: string,
		) {
			super(message);
			this.name = "ApiClientError";
		}
	},
}));

import { apiClient } from "../api/client";

function renderWithProvider(
	path: string,
	atomValues: { isAuthenticated: boolean; isLoading: boolean },
) {
	// Mock the apiClient.isAuthenticated to control isAuthenticatedAtom value
	vi.mocked(apiClient.isAuthenticated).mockReturnValue(
		atomValues.isAuthenticated,
	);

	const { hook } = memoryLocation({ path });
	const store = createStore();
	store.set(authLoadingAtom, atomValues.isLoading);

	return render(
		<Provider store={store}>
			<Router hook={hook}>
				<ProtectedRoute>
					<div data-testid="protected-content">Protected Content</div>
				</ProtectedRoute>
			</Router>
		</Provider>,
	);
}

beforeEach(() => {
	vi.clearAllMocks();
});

afterEach(() => {
	cleanup();
	vi.restoreAllMocks();
});

describe("ProtectedRoute", () => {
	it("shows loading state while auth is loading", () => {
		renderWithProvider("/", { isAuthenticated: false, isLoading: true });

		expect(screen.queryByTestId("protected-content")).toBeNull();
		// Loading spinner should be visible
		expect(screen.getByRole("status")).toBeDefined();
	});

	it("renders children when authenticated", () => {
		renderWithProvider("/", { isAuthenticated: true, isLoading: false });

		expect(screen.getByTestId("protected-content")).toBeDefined();
		expect(screen.getByText("Protected Content")).toBeDefined();
	});

	it("redirects to login when not authenticated", () => {
		renderWithProvider("/", { isAuthenticated: false, isLoading: false });

		// Should not show protected content
		expect(screen.queryByTestId("protected-content")).toBeNull();
	});
});