Skip to content

Commit 1abdffd

Browse files
authored
test(web): cover round status badge rendering (#314) (#329)
Co-authored-by: emrekayat <237104270+emrekayat@users.noreply.github.com>
1 parent d2058aa commit 1abdffd

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) 2026 Sub Rosa contributors
2+
import assert from "node:assert/strict";
3+
import test from "node:test";
4+
import { renderToStaticMarkup } from "react-dom/server";
5+
import { RoundStatusBadge, type RoundStatusBadgeProps } from "./RoundStatusBadge";
6+
7+
const labels = { loading: "Loading", empty: "Empty", error: "Error", stale: "Stale", found: "Live" } as const;
8+
for (const state of Object.keys(labels) as Array<keyof typeof labels>) {
9+
test(`${state} renders its label and the appropriate optional control`, () => {
10+
const withRetry = renderToStaticMarkup(<RoundStatusBadge state={state} onRetry={() => {}} />);
11+
assert.ok(withRetry.includes(`>${labels[state]}</span>`));
12+
const action = state === "error" ? "Retry" : state === "stale" ? "Refresh" : null;
13+
if (action) assert.match(withRetry, new RegExp(`>${action}</button>`));
14+
else assert.doesNotMatch(withRetry, /<button/);
15+
const withoutRetry = renderToStaticMarkup(<RoundStatusBadge state={state} />);
16+
assert.doesNotMatch(withoutRetry, /<button/);
17+
});
18+
}
19+
20+
test("optional tags render only when supplied", () => {
21+
assert.match(renderToStaticMarkup(<RoundStatusBadge state="found" tag="Open" />), /round-status-tag">Open/);
22+
assert.doesNotMatch(renderToStaticMarkup(<RoundStatusBadge state="found" />), /round-status-tag/);
23+
});
24+
25+
for (const [props, title] of [
26+
[{ error: "RPC offline", message: "Try again" }, "RPC offline"],
27+
[{ message: "Waiting for a round" }, "Waiting for a round"],
28+
[{}, null],
29+
] as Array<[Partial<RoundStatusBadgeProps>, string | null]>) {
30+
test(`badge title resolves to ${title}`, () => {
31+
const html = renderToStaticMarkup(<RoundStatusBadge state="error" {...props} />);
32+
if (title) assert.ok(html.includes(`title="${title}"`));
33+
else assert.doesNotMatch(html, / title=/);
34+
});
35+
}

0 commit comments

Comments
 (0)