forked from Sorokit/ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDashboard.test.tsx
More file actions
252 lines (217 loc) · 8.83 KB
/
Copy pathDashboard.test.tsx
File metadata and controls
252 lines (217 loc) · 8.83 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
250
251
252
import { fireEvent, render, screen } from "@testing-library/react";
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { NavSection } from "@/components/Sidebar";
import { Dashboard } from "./Dashboard";
// Toggled from within a test to make the mocked TransactionsScreen throw on
// render, then recover once cleared — used by the per-screen ErrorBoundary
// tests below.
const { getTransactionsShouldThrow, setTransactionsShouldThrow } = vi.hoisted(
() => {
let shouldThrow = false;
return {
getTransactionsShouldThrow: () => shouldThrow,
setTransactionsShouldThrow: (value: boolean) => {
shouldThrow = value;
},
};
},
);
// Dashboard composes every screen; stub the chrome and screens so these tests
// cover only Dashboard's own controlled/uncontrolled section logic.
vi.mock("@/components/Sidebar", () => ({
Sidebar: ({
active,
onNavigate,
}: {
active: NavSection;
onNavigate: (s: NavSection) => void;
}) => (
<nav aria-label="Main navigation">
<span data-testid="sidebar-active">{active}</span>
{(["wallet", "transactions", "soroban", "network"] as NavSection[]).map(
(section) => (
<button key={section} onClick={() => onNavigate(section)}>
{section}
</button>
),
)}
</nav>
),
}));
vi.mock("@/components/TopBar", () => ({
TopBar: ({ active }: { active: NavSection }) => (
<div data-testid="topbar-active">{active}</div>
),
}));
vi.mock("@/components/NetworkBanner", () => ({
NetworkBanner: () => null,
}));
function stubScreen(name: string) {
return () => <div data-testid={`screen-${name}`}>{name} screen</div>;
}
vi.mock("@/screens/WalletScreen", () => ({
WalletScreen: stubScreen("wallet"),
}));
vi.mock("@/screens/AccountScreen", () => ({
AccountScreen: stubScreen("account"),
}));
vi.mock("@/screens/TransactionsScreen", () => ({
TransactionsScreen: () => {
if (getTransactionsShouldThrow()) {
throw new Error("boom");
}
return <div data-testid="screen-transactions">transactions screen</div>;
},
}));
vi.mock("@/screens/SorobanScreen", () => ({
SorobanScreen: stubScreen("soroban"),
}));
vi.mock("@/screens/NetworkScreen", () => ({
NetworkScreen: stubScreen("network"),
}));
vi.mock("@/screens/RecoveryScreen", () => ({
RecoveryScreen: stubScreen("recovery"),
}));
vi.mock("@/screens/ChartingScreen", () => ({
ChartingScreen: stubScreen("charts"),
}));
vi.mock("@/screens/YieldFarmingScreen", () => ({
YieldFarmingScreen: stubScreen("farming"),
}));
vi.mock("@/screens/BudgetScreen", () => ({
BudgetScreen: stubScreen("budget"),
}));
vi.mock("@/screens/NFTScreen", () => ({
NFTScreen: stubScreen("nfts"),
}));
describe("Dashboard", () => {
beforeEach(() => {
vi.clearAllMocks();
localStorage.clear();
setTransactionsShouldThrow(false);
});
describe("uncontrolled mode", () => {
it("starts on the Wallet screen by default", () => {
render(<Dashboard />);
expect(screen.getByTestId("screen-wallet")).toBeInTheDocument();
});
it("initialises to defaultSection when provided", async () => {
render(<Dashboard defaultSection="soroban" />);
expect(await screen.findByTestId("screen-soroban")).toBeInTheDocument();
expect(screen.queryByTestId("screen-wallet")).not.toBeInTheDocument();
});
it("changes the rendered screen when a nav item is clicked", async () => {
render(<Dashboard />);
fireEvent.click(screen.getByRole("button", { name: "transactions" }));
expect(
await screen.findByTestId("screen-transactions"),
).toBeInTheDocument();
expect(
screen.getByTestId("screen-wrapper-wallet"),
).toHaveAttribute("hidden");
});
it("does not instantiate a screen before it has been visited", () => {
render(<Dashboard />);
expect(
screen.queryByTestId("screen-wrapper-soroban"),
).not.toBeInTheDocument();
});
it("keeps a previously visited screen mounted (but hidden) after navigating away", async () => {
render(<Dashboard />);
fireEvent.click(screen.getByRole("button", { name: "soroban" }));
expect(await screen.findByTestId("screen-soroban")).toBeInTheDocument();
fireEvent.click(screen.getByRole("button", { name: "wallet" }));
expect(screen.getByTestId("screen-wallet")).toBeInTheDocument();
// Soroban stays mounted so its in-progress form state survives.
expect(
screen.getByTestId("screen-wrapper-soroban"),
).toHaveAttribute("hidden");
});
it("still reports navigation through onSectionChange", async () => {
const onSectionChange = vi.fn();
render(<Dashboard onSectionChange={onSectionChange} />);
fireEvent.click(screen.getByRole("button", { name: "network" }));
expect(onSectionChange).toHaveBeenCalledWith("network");
expect(await screen.findByTestId("screen-network")).toBeInTheDocument();
});
});
describe("controlled mode", () => {
it("renders the screen named by activeSection", () => {
render(<Dashboard activeSection="transactions" />);
expect(screen.getByTestId("screen-transactions")).toBeInTheDocument();
expect(screen.queryByTestId("screen-wallet")).not.toBeInTheDocument();
});
it("fires onSectionChange but does not change the screen itself", () => {
const onSectionChange = vi.fn();
render(
<Dashboard
activeSection="transactions"
onSectionChange={onSectionChange}
/>,
);
fireEvent.click(screen.getByRole("button", { name: "soroban" }));
expect(onSectionChange).toHaveBeenCalledWith("soroban");
// The parent owns the state, so the view is unchanged until it updates.
expect(screen.getByTestId("screen-transactions")).toBeInTheDocument();
expect(
screen.queryByTestId("screen-wrapper-soroban"),
).not.toBeInTheDocument();
});
it("follows the parent when activeSection changes", async () => {
const { rerender } = render(<Dashboard activeSection="wallet" />);
expect(screen.getByTestId("screen-wallet")).toBeInTheDocument();
rerender(<Dashboard activeSection="network" />);
expect(await screen.findByTestId("screen-network")).toBeInTheDocument();
// Wallet stays mounted (hidden) so its state survives navigation.
expect(
screen.getByTestId("screen-wrapper-wallet"),
).toHaveAttribute("hidden");
});
it("ignores defaultSection when activeSection is set", () => {
render(<Dashboard activeSection="wallet" defaultSection="soroban" />);
expect(screen.getByTestId("screen-wallet")).toBeInTheDocument();
});
it("passes the active section down to the chrome", () => {
render(<Dashboard activeSection="soroban" />);
expect(screen.getByTestId("sidebar-active")).toHaveTextContent("soroban");
expect(screen.getByTestId("topbar-active")).toHaveTextContent("soroban");
});
});
describe("per-screen error boundaries (#564)", () => {
beforeEach(() => {
vi.spyOn(console, "error").mockImplementation(() => {});
});
it("does not bring down the Sidebar or TopBar when a screen crashes", () => {
setTransactionsShouldThrow(true);
render(<Dashboard defaultSection="transactions" />);
expect(screen.getByLabelText("Main navigation")).toBeInTheDocument();
expect(screen.getByTestId("topbar-active")).toBeInTheDocument();
expect(screen.getByText(/Transactions couldn't load/)).toBeInTheDocument();
});
it("shows the screen name and a Retry button in the fallback", () => {
setTransactionsShouldThrow(true);
render(<Dashboard defaultSection="transactions" />);
expect(screen.getByText("Transactions couldn't load")).toBeInTheDocument();
expect(screen.getByRole("button", { name: "Retry" })).toBeInTheDocument();
});
it("recovers and re-mounts only the affected screen when Retry is clicked", async () => {
setTransactionsShouldThrow(true);
render(<Dashboard defaultSection="transactions" />);
expect(screen.getByRole("button", { name: "Retry" })).toBeInTheDocument();
setTransactionsShouldThrow(false);
fireEvent.click(screen.getByRole("button", { name: "Retry" }));
expect(
await screen.findByTestId("screen-transactions"),
).toBeInTheDocument();
// The chrome was never affected by the crash or the retry.
expect(screen.getByLabelText("Main navigation")).toBeInTheDocument();
});
it("leaves other screens working while a different screen is crashed", async () => {
setTransactionsShouldThrow(true);
render(<Dashboard defaultSection="transactions" />);
expect(screen.getByText("Transactions couldn't load")).toBeInTheDocument();
fireEvent.click(screen.getByRole("button", { name: "wallet" }));
expect(await screen.findByTestId("screen-wallet")).toBeInTheDocument();
});
});
});