forked from Liquifact/Liquifact-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInvoiceCard.test.tsx
More file actions
295 lines (249 loc) · 10.4 KB
/
Copy pathInvoiceCard.test.tsx
File metadata and controls
295 lines (249 loc) · 10.4 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
/**
* @file components/InvoiceCard.test.tsx
* Unit + accessibility tests for the shared InvoiceCard component.
* Target: ≥ 95% branch coverage for InvoiceCard.jsx
*
* Status is now delegated to the shared `StatusPill` component. These
* tests intentionally assert only the *wiring* (the badge is rendered,
* the link aria-label mentions the status, the TitleCase canonical values
* are accepted); per-tone class assertions live in `components/StatusPill.test.tsx`.
*/
import React from "react";
import { render, screen } from "@testing-library/react";
import { axe, toHaveNoViolations } from "jest-axe";
import InvoiceCard from "./InvoiceCard";
expect.extend(toHaveNoViolations);
// Next.js Link renders an <a> in tests; mock it simply.
jest.mock("next/link", () => {
const Link = ({ href, children, ...rest }: any) => (
<a href={href} {...rest}>
{children}
</a>
);
Link.displayName = "Link";
return Link;
});
// ---------------------------------------------------------------------------
// Fixtures
// ---------------------------------------------------------------------------
const BASE_INVOICE = {
id: "INV-001",
issuer: "Acme Corp",
amount: 50000,
currency: "USDC",
dueDate: "2025-09-30",
yield: 8.5,
status: "Open",
};
function renderCard(overrides = {}) {
const invoice = { ...BASE_INVOICE, ...overrides };
return render(<InvoiceCard invoice={invoice} />);
}
// ---------------------------------------------------------------------------
// Basic rendering
// ---------------------------------------------------------------------------
describe("InvoiceCard — basic rendering", () => {
it("renders the issuer name", () => {
renderCard();
expect(screen.getByText("Acme Corp")).toBeInTheDocument();
});
it("renders the invoice id", () => {
renderCard();
expect(screen.getByText("INV-001")).toBeInTheDocument();
});
it("renders the amount via the shared currency formatter", () => {
renderCard({ amount: 50000, currency: "USD" });
expect(screen.getByText("$50,000")).toBeInTheDocument();
});
it("falls back to the shared default currency when the currency code is unsupported", () => {
renderCard();
expect(screen.getByText("$50,000")).toBeInTheDocument();
});
it("renders the yield percentage", () => {
renderCard();
expect(screen.getByText("8.5%")).toBeInTheDocument();
});
it("renders the formatted due date", () => {
renderCard();
// "Sep 30, 2025" or locale-equivalent
expect(screen.getByText(/sep.+30.+2025/i)).toBeInTheDocument();
});
it("renders a status badge with the correct label", () => {
renderCard({ status: "Open" });
expect(screen.getByRole("status", { hidden: true })).toHaveTextContent("Open");
});
it("links to the invoice detail page", () => {
renderCard({ id: "INV-042" });
expect(screen.getByRole("link")).toHaveAttribute("href", "/invest/INV-042");
});
it("renders percent-string yields without a duplicated % suffix", () => {
renderCard({ yield: "8.5%" });
expect(screen.getByText("8.5%")).toBeInTheDocument();
});
it("renders numeric yields with a trailing %", () => {
renderCard({ yield: 8.5 });
expect(screen.getByText("8.5%")).toBeInTheDocument();
});
});
// ---------------------------------------------------------------------------
// Status variants — canonical TitleCase values
// ---------------------------------------------------------------------------
describe("InvoiceCard — status variants", () => {
it.each([
["Open", "Open"],
["Funded", "Funded"],
["Settled", "Settled"],
["Overdue", "Overdue by maturity"],
])("renders status '%s' as '%s'", (status, label) => {
renderCard({ status });
expect(screen.getByRole("status", { hidden: true })).toHaveTextContent(label);
});
it("falls back to a neutral 'Unknown' pill for unrecognised status values", () => {
renderCard({ status: "unknown-value" });
const badge = screen.getByRole("status", { hidden: true });
expect(badge).toBeInTheDocument();
expect(badge).toHaveTextContent("Unknown");
expect(badge).toHaveAttribute("data-status", "Unknown");
});
it("falls back to 'Unknown' when status is null", () => {
renderCard({ status: null as any });
expect(screen.getByRole("status", { hidden: true })).toHaveTextContent("Unknown");
});
it("falls back to 'Unknown' when status is undefined", () => {
const { status, ...rest } = BASE_INVOICE;
render(<InvoiceCard invoice={rest as any} />);
expect(screen.getByRole("status", { hidden: true })).toHaveTextContent("Unknown");
});
});
// ---------------------------------------------------------------------------
// Edge cases — missing optional fields
// ---------------------------------------------------------------------------
describe("InvoiceCard — missing optional fields", () => {
it("renders em-dash when amount is missing", () => {
renderCard({ amount: undefined });
expect(screen.getByText("—")).toBeInTheDocument();
});
it("renders em-dash when amount is null", () => {
renderCard({ amount: null as any });
expect(screen.getByText("—")).toBeInTheDocument();
});
it("renders parsed numeric strings through the shared currency formatter", () => {
renderCard({ amount: "12,500", currency: "USD" });
expect(screen.getByText("$12,500")).toBeInTheDocument();
});
it("renders em-dash when amount is NaN", () => {
renderCard({ amount: Number.NaN });
expect(screen.getByText("—")).toBeInTheDocument();
});
it("renders em-dash when dueDate is missing", () => {
renderCard({ dueDate: undefined });
const dashes = screen.getAllByText("—");
expect(dashes.length).toBeGreaterThanOrEqual(1);
});
it("renders em-dash when yield is missing", () => {
renderCard({ yield: undefined });
const dashes = screen.getAllByText("—");
expect(dashes.length).toBeGreaterThanOrEqual(1);
});
it("renders em-dash when yield is NaN", () => {
renderCard({ yield: Number.NaN });
const dashes = screen.getAllByText("—");
expect(dashes.length).toBeGreaterThanOrEqual(1);
});
it("renders fallback text when issuer is missing", () => {
renderCard({ issuer: undefined });
expect(screen.getByText(/unknown issuer/i)).toBeInTheDocument();
});
it("handles an invalid date string without throwing", () => {
renderCard({ dueDate: "not-a-date" });
expect(screen.getByText("not-a-date")).toBeInTheDocument();
});
it("renders the entire row when invoice is null (uses fallback branches)", () => {
// InvoiceCard tolerates a nullish invoice via `invoice ?? {}` and renders
// a row of em-dashes / "Unknown issuer" / "Unknown" status.
expect(() => render(<InvoiceCard invoice={null as any} />)).not.toThrow();
expect(screen.getByText(/unknown issuer/i)).toBeInTheDocument();
});
});
// ---------------------------------------------------------------------------
// Long issuer names
// ---------------------------------------------------------------------------
describe("InvoiceCard — long issuer names", () => {
it("renders without layout-breaking for a very long issuer name", () => {
const longName = "A".repeat(120);
renderCard({ issuer: longName });
expect(screen.getByText(longName)).toBeInTheDocument();
expect(screen.getByText(longName)).toHaveClass("truncate");
});
});
// ---------------------------------------------------------------------------
// Accessibility
// ---------------------------------------------------------------------------
describe("InvoiceCard — accessibility", () => {
it("has no axe violations for a complete invoice", async () => {
const { container } = renderCard();
const results = await axe(container);
expect(results).toHaveNoViolations();
});
it("has no axe violations when optional fields are missing", async () => {
const { container } = renderCard({
issuer: undefined,
amount: undefined,
dueDate: undefined,
yield: undefined,
});
const results = await axe(container);
expect(results).toHaveNoViolations();
});
it("has an accessible aria-label on the link describing the invoice", () => {
renderCard();
const link = screen.getByRole("link");
expect(link).toHaveAttribute("aria-label");
expect(link.getAttribute("aria-label")).toMatch(/acme corp/i);
expect(link.getAttribute("aria-label")).toMatch(/open/i);
});
it("status badge has role='status' and an aria-label naming the state", () => {
renderCard({ status: "Funded" });
const badge = screen.getByRole("status", { hidden: true });
expect(badge).toHaveAttribute("aria-label", "Status: Funded");
});
it("status badge carries data-status='Funded' (canonical, not label-text)", () => {
renderCard({ status: "Funded" });
const badge = screen.getByRole("status", { hidden: true });
expect(badge).toHaveAttribute("data-status", "Funded");
});
it("link aria-label does NOT include the 'Unknown' trailer when status is nullish", () => {
// Regression guard for the conditional statusSuffix: nullish status
// must result in an aria-label of the form
// "Invoice <id> from <issuer>" — NOT "... \u2014 Unknown".
renderCard({ status: null as any });
const link = screen.getByRole("link");
const aria = link.getAttribute("aria-label") ?? "";
expect(aria).toMatch(/acme corp/i);
expect(aria).not.toMatch(/unknown/i);
expect(aria).not.toMatch(/\u2014/);
});
it("link aria-label does NOT include the 'Unknown' trailer for an unrecognised status", () => {
renderCard({ status: "garbage" as any });
const link = screen.getByRole("link");
const aria = link.getAttribute("aria-label") ?? "";
expect(aria).not.toMatch(/unknown/i);
expect(aria).not.toMatch(/\u2014/);
});
it("link aria-label DOES include the canonical status (with the em-dash trailer)", () => {
renderCard({ status: "Settled" });
const link = screen.getByRole("link");
const aria = link.getAttribute("aria-label") ?? "";
expect(aria).toMatch(/settled/i);
expect(aria).toMatch(/\u2014 settled/i);
});
});
// ---------------------------------------------------------------------------
// Snapshot
// ---------------------------------------------------------------------------
describe("InvoiceCard — snapshot", () => {
it("matches snapshot for a complete invoice", () => {
const { asFragment } = renderCard();
expect(asFragment()).toMatchSnapshot();
});
});