forked from MergeFi/frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatCard.test.tsx
More file actions
185 lines (166 loc) · 7.94 KB
/
Copy pathStatCard.test.tsx
File metadata and controls
185 lines (166 loc) · 7.94 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
/**
* StatCard.test.tsx
*
* Covers all four explicit states (loading, error, zero, loaded) plus
* large-number scaling and pre-formatted string pass-through.
*
* Seven test cases in total, matching the acceptance criteria in the issue:
* 1. Loading state — skeleton visible, no value text
* 2. Error state — em-dash + "Error loading data", ARIA label
* 3. Zero state (numeric 0) — "0" rendered, zeroLabel shown, trend hidden
* 4. Zero-as-string — must NOT trigger zero-state treatment
* 5. Typical loaded value — renders correctly, trend shown
* 6. Large financial value — shorter font class applied, title tooltip present
* 7. Pre-formatted string — passed through unchanged
*/
import React from "react";
import { render, screen } from "@testing-library/react";
import { StatCard } from "./StatCard";
// ─── 1. Loading state ────────────────────────────────────────────────────────
describe("StatCard — loading state", () => {
it("renders a skeleton and hides the value", () => {
const { container } = render(<StatCard label="Lifetime earnings" status="loading" />);
expect(screen.getByTestId("statcard-skeleton")).toBeInTheDocument();
// No numeric/text value should be visible while loading
expect(screen.queryByTestId("statcard-value")).not.toBeInTheDocument();
expect(screen.queryByTestId("statcard-error")).not.toBeInTheDocument();
expect(screen.queryByTestId("statcard-zero")).not.toBeInTheDocument();
// Skeleton carries the pulse animation class
expect(container.querySelector("[data-testid='statcard-skeleton']")).toHaveClass(
"animate-pulse",
);
});
});
// ─── 2. Error state ──────────────────────────────────────────────────────────
describe("StatCard — error state", () => {
it("renders the error indicator with correct text and ARIA label", () => {
render(<StatCard label="Locked in escrow" status="error" />);
const errorEl = screen.getByTestId("statcard-error");
expect(errorEl).toBeInTheDocument();
// Em-dash must be visible — not a zero or blank
expect(errorEl).toHaveTextContent("—");
// Sub-label wording
expect(errorEl).toHaveTextContent("Error loading data");
// Wrapper region is labelled for screen readers — prevents SR from
// announcing the card as just "Locked in escrow" with no context
const region = screen.getByRole("region", { name: /Error loading data/i });
expect(region).toBeInTheDocument();
});
it("never renders a zero value that could be mistaken for a real figure", () => {
render(<StatCard label="Total paid out" status="error" value={0} format="currency" />);
expect(screen.queryByTestId("statcard-value")).not.toBeInTheDocument();
expect(screen.queryByTestId("statcard-zero")).not.toBeInTheDocument();
expect(screen.getByTestId("statcard-error")).toBeInTheDocument();
});
});
// ─── 3. Zero state — numeric 0 ───────────────────────────────────────────────
describe("StatCard — zero state (numeric 0)", () => {
it("renders a deliberate zero with the default zeroLabel", () => {
render(
<StatCard label="Lifetime earnings" status="loaded" value={0} format="currency" />,
);
const zeroEl = screen.getByTestId("statcard-zero");
expect(zeroEl).toBeInTheDocument();
// The value "0 USDC" must be present and readable
expect(zeroEl).toHaveTextContent("0");
// Default sub-label
expect(zeroEl).toHaveTextContent("No activity yet");
// Trend must NOT appear on a zero value (no misleading "0% vs last period")
expect(screen.queryByText(/vs last period/i)).not.toBeInTheDocument();
});
it("uses a custom zeroLabel when provided", () => {
render(
<StatCard
label="Active bounties"
status="loaded"
value={0}
format="count"
zeroLabel="No bounties yet"
/>,
);
expect(screen.getByTestId("statcard-zero")).toHaveTextContent("No bounties yet");
});
});
// ─── 4. Zero-as-string — must NOT trigger zero-state ─────────────────────────
describe("StatCard — zero-as-string pass-through", () => {
it('renders "0" string as a normal loaded value, not the zero-state', () => {
// "0" as a string means the caller has pre-formatted the value.
// We must not second-guess it and show the zero-state — that would
// incorrectly suppress the value for things like "0%" completion rate
// that the caller explicitly formatted.
render(<StatCard label="Completion rate" status="loaded" value="0%" />);
// Normal value node should appear
expect(screen.getByTestId("statcard-value")).toBeInTheDocument();
expect(screen.getByTestId("statcard-value")).toHaveTextContent("0%");
// Zero-state node must NOT appear
expect(screen.queryByTestId("statcard-zero")).not.toBeInTheDocument();
});
});
// ─── 5. Typical loaded value ──────────────────────────────────────────────────
describe("StatCard — typical loaded value", () => {
it("renders value and trend correctly", () => {
render(
<StatCard
label="Lifetime earnings"
status="loaded"
value={8420}
format="currency"
trend={12}
/>,
);
const valueEl = screen.getByTestId("statcard-value");
expect(valueEl).toBeInTheDocument();
// Locale-formatted + asset suffix
expect(valueEl).toHaveTextContent("8,420 USDC");
// Trend rendered
expect(screen.getByText(/12% vs last period/i)).toBeInTheDocument();
});
it("does not render trend when trend prop is absent", () => {
render(<StatCard label="Repositories" status="loaded" value={4} format="count" />);
expect(screen.queryByText(/vs last period/i)).not.toBeInTheDocument();
});
});
// ─── 6. Large financial value ─────────────────────────────────────────────────
describe("StatCard — large number handling", () => {
it("applies a smaller font class for a long value string", () => {
render(
<StatCard
label="Total paid out"
status="loaded"
value={1_284_999}
format="currency"
/>,
);
const valueEl = screen.getByTestId("statcard-value");
// "1,284,999 USDC" is 14 chars → should get text-xl (not text-2xl)
expect(valueEl.className).toMatch(/text-xl/);
// Must NOT have the default large class
expect(valueEl.className).not.toMatch(/text-2xl/);
});
it("exposes the full exact value via title tooltip", () => {
render(
<StatCard
label="Total paid out"
status="loaded"
value={1_284_999}
format="currency"
/>,
);
const valueEl = screen.getByTestId("statcard-value");
// Title attribute must be present with the unabbreviated figure
expect(valueEl).toHaveAttribute("title");
const title = valueEl.getAttribute("title") ?? "";
// Must contain the full number — no "1.3M" abbreviation
expect(title).toContain("1,284,999");
});
});
// ─── 7. Pre-formatted string pass-through ────────────────────────────────────
describe("StatCard — pre-formatted string value", () => {
it("passes a pre-formatted string through without modification", () => {
render(<StatCard label="Completion rate" status="loaded" value="94%" />);
const valueEl = screen.getByTestId("statcard-value");
expect(valueEl).toHaveTextContent("94%");
// No extra USDC suffix should be appended
expect(valueEl).not.toHaveTextContent("USDC");
});
});