forked from Fluxora-Org/Fluxora-Frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamRow.test.tsx
More file actions
316 lines (257 loc) · 10.7 KB
/
Copy pathStreamRow.test.tsx
File metadata and controls
316 lines (257 loc) · 10.7 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import { render, screen } from "@testing-library/react";
import { MemoryRouter } from "react-router-dom";
import { describe, expect, it, vi } from "vitest";
import StreamRow from "../StreamRow";
import type { Stream } from "../Stream";
const stream: Stream = {
id: "STR-900",
name: "Security Review Grant",
recipient: "GABCDEFGHIJKLMNOPQRSTUVWXYZ23456789WXYZ",
rate: "2,500 USDC/mo",
accruedAmount: 1234.56,
status: "Active",
startDate: "2026-01-01",
};
function renderRow(rowStream: Stream = stream) {
const onSelect = vi.fn();
render(
<MemoryRouter>
<table>
<tbody>
<StreamRow stream={rowStream} onSelect={onSelect} />
</tbody>
</table>
</MemoryRouter>
);
return { onSelect };
}
describe("StreamRow", () => {
it("truncates long recipient addresses while preserving the full address for assistive text", () => {
renderRow();
expect(screen.getByText("GABCDE...WXYZ")).toBeInTheDocument();
expect(screen.getByRole("button", { name: `Copy address: ${stream.recipient}` })).toBeInTheDocument();
expect(screen.getByLabelText(`Recipient ${stream.recipient}`)).toHaveAttribute(
"title",
stream.recipient
);
});
it("renders status and accrued amount formatting for a stream", () => {
renderRow();
expect(screen.getByRole("status", { name: "Active status" })).toHaveTextContent("ACTIVE");
expect(screen.getByText("2,500 USDC/mo")).toBeInTheDocument();
expect(screen.getByText("1,234.56 USDC accrued")).toBeInTheDocument();
});
it("renders dynamic cell content as text instead of HTML", () => {
const { container } = render(
<MemoryRouter>
<table>
<tbody>
<StreamRow
stream={{
...stream,
id: "STR-XSS",
name: "<script>alert(1)</script>",
recipient: "<img src=x onerror=alert(1)>",
}}
onSelect={vi.fn()}
/>
</tbody>
</table>
</MemoryRouter>
);
expect(screen.getByText("<script>alert(1)</script>")).toBeInTheDocument();
expect(container.querySelector("script")).toBeNull();
expect(container.querySelector("img")).toBeNull();
});
it("opens the actions menu when the ellipsis button is clicked and closes on Escape", async () => {
renderRow();
// The ellipsis trigger should exist
const trigger = screen.getByRole("button", { name: `Actions for stream ${stream.name}` });
expect(trigger).toBeInTheDocument();
expect(trigger).toHaveAttribute("aria-expanded", "false");
// Click to open
const user = await import("@testing-library/user-event").then((m) => m.default.setup());
await user.click(trigger);
expect(trigger).toHaveAttribute("aria-expanded", "true");
// The menu items should be rendered
const menu = screen.getByRole("menu", { name: `Actions for stream ${stream.name}` });
expect(menu).toBeInTheDocument();
expect(screen.getByRole("menuitem", { name: /view details/i })).toBeInTheDocument();
expect(screen.getByRole("menuitem", { name: /copy address/i })).toBeInTheDocument();
expect(screen.getByRole("menuitem", { name: /view in explorer/i })).toBeInTheDocument();
expect(screen.getByRole("menuitem", { name: /pause\/cancel/i })).toBeInTheDocument();
// Roving focus: First item should get focus
expect(screen.getByRole("menuitem", { name: /view details/i })).toHaveFocus();
// Press Escape to close and check that focus is restored to the trigger
await user.keyboard("{Escape}");
expect(trigger).toHaveAttribute("aria-expanded", "false");
expect(trigger).toHaveFocus();
});
it("navigates through items via Arrow keys in the menu", async () => {
renderRow();
const trigger = screen.getByRole("button", { name: `Actions for stream ${stream.name}` });
const user = await import("@testing-library/user-event").then((m) => m.default.setup());
await user.click(trigger);
const firstItem = screen.getByRole("menuitem", { name: /view details/i });
const secondItem = screen.getByRole("menuitem", { name: /copy address/i });
const thirdItem = screen.getByRole("menuitem", { name: /view in explorer/i });
const fourthItem = screen.getByRole("menuitem", { name: /pause\/cancel/i });
expect(firstItem).toHaveFocus();
// Arrow down
await user.keyboard("{ArrowDown}");
expect(secondItem).toHaveFocus();
// Arrow down again
await user.keyboard("{ArrowDown}");
expect(thirdItem).toHaveFocus();
// Arrow down again
await user.keyboard("{ArrowDown}");
expect(fourthItem).toHaveFocus();
// Arrow down (loop to first)
await user.keyboard("{ArrowDown}");
expect(firstItem).toHaveFocus();
// Arrow up (loop to last)
await user.keyboard("{ArrowUp}");
expect(fourthItem).toHaveFocus();
});
it("triggers the actions menu via right-click (contextmenu event)", () => {
renderRow();
const row = screen.getByRole("row");
// Initially closed
const trigger = screen.getByRole("button", { name: `Actions for stream ${stream.name}` });
expect(trigger).toHaveAttribute("aria-expanded", "false");
// Fire context menu event on the row
const fireEvent = require("@testing-library/react").fireEvent;
fireEvent.contextMenu(row, { clientX: 100, clientY: 100 });
expect(trigger).toHaveAttribute("aria-expanded", "true");
expect(screen.getByRole("menu")).toBeInTheDocument();
});
it("disables the Pause/Cancel action button for completed streams", async () => {
renderRow({
...stream,
status: "Completed",
});
const trigger = screen.getByRole("button", { name: `Actions for stream ${stream.name}` });
const user = await import("@testing-library/user-event").then((m) => m.default.setup());
await user.click(trigger);
const pauseCancelItem = screen.getByRole("menuitem", { name: /pause\/cancel/i });
expect(pauseCancelItem).toBeDisabled();
});
it("closes the menu when clicking outside it", async () => {
renderRow();
const trigger = screen.getByRole("button", { name: `Actions for stream ${stream.name}` });
const user = await import("@testing-library/user-event").then((m) => m.default.setup());
await user.click(trigger);
expect(screen.getByRole("menu")).toBeInTheDocument();
// Click outside the menu and trigger
await user.click(document.body);
expect(screen.queryByRole("menu")).not.toBeInTheDocument();
expect(trigger).toHaveAttribute("aria-expanded", "false");
});
it("toggles the menu closed when the trigger is clicked a second time", async () => {
renderRow();
const trigger = screen.getByRole("button", { name: `Actions for stream ${stream.name}` });
const user = await import("@testing-library/user-event").then((m) => m.default.setup());
await user.click(trigger);
expect(trigger).toHaveAttribute("aria-expanded", "true");
await user.click(trigger);
expect(trigger).toHaveAttribute("aria-expanded", "false");
expect(screen.queryByRole("menu")).not.toBeInTheDocument();
});
it("'View details' menu item navigates to the stream detail route", async () => {
renderRow();
const trigger = screen.getByRole("button", { name: `Actions for stream ${stream.name}` });
const user = await import("@testing-library/user-event").then((m) => m.default.setup());
await user.click(trigger);
await user.click(screen.getByRole("menuitem", { name: /view details/i }));
// Menu should close after selection
expect(screen.queryByRole("menu")).not.toBeInTheDocument();
});
describe("Regression tests for issue #1459", () => {
it("renders accrued amount for streams with zero accrued amount", () => {
renderRow({
...stream,
id: "STR-ZERO",
accruedAmount: 0,
});
expect(screen.getByText("0 USDC accrued")).toBeInTheDocument();
});
it("renders status for completed streams", () => {
renderRow({
...stream,
id: "STR-COMP",
status: "Completed",
});
expect(screen.getByRole("status", { name: "Completed status" })).toHaveTextContent("COMPLETED");
});
it("renders status for paused streams", () => {
renderRow({
...stream,
id: "STR-PAUSED",
status: "Paused",
});
expect(screen.getByRole("status", { name: "Paused status" })).toHaveTextContent("PAUSED");
});
it("renders status for active streams", () => {
renderRow({
...stream,
id: "STR-ACTIVE",
status: "Active",
});
expect(screen.getByRole("status", { name: "Active status" })).toHaveTextContent("ACTIVE");
});
it("renders accrued amount for streams with large values", () => {
renderRow({
...stream,
id: "STR-LARGE",
accruedAmount: 999999999.99,
});
expect(screen.getByText("999,999,999.99 USDC accrued")).toBeInTheDocument();
});
it("renders all required fields without defensive checks", () => {
renderRow({
id: "STR-ALL-FIELDS",
name: "Test Stream",
recipient: "GTEST123456789ABCDEF",
rate: "1,000 USDC/mo",
accruedAmount: 5000,
status: "Active",
startDate: "2026-01-01",
});
expect(screen.getByText("Test Stream")).toBeInTheDocument();
expect(screen.getByText("GTEST1...CDEF")).toBeInTheDocument();
expect(screen.getByText("1,000 USDC/mo")).toBeInTheDocument();
expect(screen.getByText("5,000 USDC accrued")).toBeInTheDocument();
expect(screen.getByRole("status", { name: "Active status" })).toHaveTextContent("ACTIVE");
});
it("consistently renders status matching normalized streamRecords data", () => {
// Test all three valid statuses from streamRecords StreamStatus type
const statuses: Array<"Active" | "Paused" | "Completed"> = ["Active", "Paused", "Completed"];
statuses.forEach((status) => {
const { unmount } = render(
<MemoryRouter>
<table>
<tbody>
<StreamRow
stream={{ ...stream, id: `STR-${status}`, status }}
onSelect={vi.fn()}
/>
</tbody>
</table>
</MemoryRouter>
);
expect(screen.getByRole("status", { name: `${status} status` })).toHaveTextContent(status.toUpperCase());
unmount();
});
});
it("renders accrued amount as number consistently with streamRecords normalization", () => {
// streamRecords.readNumber() returns 0 for invalid values, so we test that
// the UI renders the numeric value without type checking
renderRow({
...stream,
id: "STR-NUMERIC",
accruedAmount: 0,
});
expect(screen.getByText(/0.*USDC accrued/)).toBeInTheDocument();
});
});
});