forked from Liquifact/Liquifact-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpage.test.tsx
More file actions
249 lines (199 loc) · 5.62 KB
/
Copy pathpage.test.tsx
File metadata and controls
249 lines (199 loc) · 5.62 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import "@testing-library/jest-dom";
import { render, screen, fireEvent, waitFor } from "@testing-library/react";
import Home from "./page";
import { getHealth } from "../lib/api/health";
jest.mock("next/navigation", () => ({
usePathname: () => "/",
}));
jest.mock("../components/WalletStatusLazy", () => ({
__esModule: true,
default: function MockWalletStatusLazy() {
return <button type="button">Connect Wallet</button>;
},
}));
// IMPORTANT: mock before tests
jest.mock("../lib/api/health", () => ({
__esModule: true,
getHealth: jest.fn(),
}));
// Mock NavMenu to avoid WalletStatus pre-existing bugs
jest.mock("../components/NavMenu", () => {
return function MockNavMenu() {
return <div data-testid="nav-menu">NavMenu</div>;
};
});
const mockGetHealth = getHealth as jest.Mock;
describe("Home Page Health Check", () => {
beforeEach(() => {
jest.clearAllMocks();
});
it("verifies mock", () => {
expect(jest.isMockFunction(getHealth)).toBe(true);
});
it("renders the health check button", () => {
render(<Home />);
expect(
screen.getByRole("button", {
name: /check backend health/i,
})
).toBeTruthy();
});
it("disables button while loading", async () => {
mockGetHealth.mockImplementation(
() =>
new Promise((resolve) =>
setTimeout(
() =>
resolve({
status: "connected",
message: "Backend is healthy",
}),
100
)
)
);
render(<Home />);
const button = screen.getByRole("button", {
name: /check backend health/i,
});
fireEvent.click(button);
await waitFor(() => {
expect(button).toBeDisabled();
});
expect(screen.getByText(/checking/i)).toBeTruthy();
});
it("renders connected state with green badge", async () => {
mockGetHealth.mockResolvedValue({
status: "connected",
message: "Backend is healthy",
details: {
status: "ok",
},
});
render(<Home />);
fireEvent.click(
screen.getByRole("button", {
name: /check backend health/i,
})
);
await waitFor(() => {
expect(screen.getByText(/backend is healthy/i)).toBeTruthy();
});
// Assert the "Connected" badge is rendered
expect(screen.getAllByText(/connected/i).length).toBeGreaterThan(0);
});
it("renders degraded state with amber badge", async () => {
mockGetHealth.mockResolvedValue({
status: "degraded",
message: "Backend responded with 500",
details: {
error: "Internal Server Error",
},
});
render(<Home />);
fireEvent.click(
screen.getByRole("button", {
name: /check backend health/i,
})
);
await waitFor(() => {
expect(screen.getByText(/backend responded with 500/i)).toBeTruthy();
});
// Assert the "Degraded" badge is rendered
expect(screen.getAllByText(/degraded/i).length).toBeGreaterThan(0);
});
it("renders unreachable state with red badge", async () => {
mockGetHealth.mockResolvedValue({
status: "unreachable",
message: "Health check timed out",
});
render(<Home />);
fireEvent.click(
screen.getByRole("button", {
name: /check backend health/i,
})
);
await waitFor(() => {
expect(screen.getAllByText(/health check timed out/i).length).toBeGreaterThan(0);
});
// Assert the "Unreachable" badge is rendered
expect(screen.getAllByText(/unreachable/i).length).toBeGreaterThan(0);
});
it("renders raw response details", async () => {
mockGetHealth.mockResolvedValue({
status: "degraded",
message: "Backend responded with 500",
details: {
error: "Internal Server Error",
},
});
render(<Home />);
fireEvent.click(
screen.getByRole("button", {
name: /check backend health/i,
})
);
await waitFor(() => {
expect(screen.getByText(/raw response/i)).toBeTruthy();
});
});
it("renders distinct visual state for each status", async () => {
// Test connected state
mockGetHealth.mockResolvedValue({
status: "connected",
message: "Backend is healthy",
});
render(<Home />);
fireEvent.click(
screen.getByRole("button", {
name: /check backend health/i,
})
);
await waitFor(() => {
expect(screen.getAllByText(/connected/i).length).toBeGreaterThan(0);
});
// Test degraded state
mockGetHealth.mockResolvedValue({
status: "degraded",
message: "Backend responded with 500",
});
fireEvent.click(
screen.getByRole("button", {
name: /check backend health/i,
})
);
await waitFor(() => {
expect(screen.getAllByText(/degraded/i).length).toBeGreaterThan(0);
});
// Test unreachable state
mockGetHealth.mockResolvedValue({
status: "unreachable",
message: "Health check timed out",
});
fireEvent.click(
screen.getByRole("button", {
name: /check backend health/i,
})
);
await waitFor(() => {
expect(screen.getAllByText(/unreachable/i).length).toBeGreaterThan(0);
});
});
it("announces status updates politely", async () => {
mockGetHealth.mockResolvedValue({
status: "connected",
message: "Backend is healthy",
details: {
status: "ok",
},
});
render(<Home />);
fireEvent.click(
screen.getByRole("button", {
name: /check backend health/i,
})
);
const statusRegion = await screen.findByRole("status");
expect(statusRegion.getAttribute("aria-live")).toBe("polite");
});
});