forked from mux-labs/mux-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthGuard.test.tsx
More file actions
96 lines (78 loc) · 2.92 KB
/
Copy pathAuthGuard.test.tsx
File metadata and controls
96 lines (78 loc) · 2.92 KB
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
90
91
92
93
94
95
96
/**
* Tests for #466: Add auth loading skeleton on protected routes.
*/
import { render, screen } from "@testing-library/react";
import { describe, it, expect, vi } from "vitest";
// ---------------------------------------------------------------------------
// Mocks
// ---------------------------------------------------------------------------
const mockReplace = vi.fn();
vi.mock("next/navigation", () => ({
useRouter: () => ({ replace: mockReplace }),
usePathname: () => "/demo/dashboard",
}));
// AuthContext mock — we control isLoading and isAuthenticated per test
const mockUseAuth = vi.fn();
vi.mock("@/context/AuthContext", () => ({
useAuth: () => mockUseAuth(),
}));
// ---------------------------------------------------------------------------
// Import after mocks
// ---------------------------------------------------------------------------
import { AuthGuard, DashboardSkeleton } from "../AuthGuard";
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
describe("AuthGuard — auth loading skeleton on protected routes (#466)", () => {
it("shows the dashboard skeleton while auth is loading", () => {
mockUseAuth.mockReturnValue({
isLoading: true,
isAuthenticated: false,
});
render(
<AuthGuard>
<div data-testid="protected-content">Dashboard content</div>
</AuthGuard>,
);
expect(screen.getByTestId("dashboard-auth-skeleton")).toBeDefined();
expect(screen.queryByTestId("protected-content")).toBeNull();
});
it("renders protected content once auth has loaded and user is authenticated", () => {
mockUseAuth.mockReturnValue({
isLoading: false,
isAuthenticated: true,
});
render(
<AuthGuard>
<div data-testid="protected-content">Dashboard content</div>
</AuthGuard>,
);
expect(screen.getByTestId("protected-content")).toBeDefined();
expect(screen.queryByTestId("dashboard-auth-skeleton")).toBeNull();
});
it("shows skeleton and redirects when auth loaded but user is not authenticated", () => {
mockReplace.mockClear();
window.history.replaceState({}, "", "/dashboard/settings");
mockUseAuth.mockReturnValue({
isLoading: false,
isAuthenticated: false,
});
render(
<AuthGuard>
<div data-testid="protected-content">Dashboard content</div>
</AuthGuard>,
);
expect(screen.getByTestId("dashboard-auth-skeleton")).toBeDefined();
expect(screen.queryByTestId("protected-content")).toBeNull();
// Redirect is delegated to useSessionGuard (#624).
expect(mockReplace).toHaveBeenCalledWith(
"/login?callbackUrl=%2Fdashboard%2Fsettings",
);
});
it("DashboardSkeleton has correct aria attributes", () => {
render(<DashboardSkeleton />);
const skeleton = screen.getByTestId("dashboard-auth-skeleton");
expect(skeleton.getAttribute("aria-busy")).toBe("true");
expect(skeleton.getAttribute("aria-label")).toBe("Loading dashboard");
});
});