Files
multica/apps/web/features/landing/utils/use-github-stars.test.ts
Bohan Jiang 09f90abb70 MUL-3568 feat(landing): show live GitHub star count on the header GitHub button (#4451)
* feat(landing): show live GitHub star count on the header GitHub button

Add a small client hook (useGithubStars) that fetches stargazers_count
from the GitHub API and a formatStarCount helper that renders it in
GitHub's compact repo-header style (e.g. "37.6k"). The landing header's
GitHub button now appends a star badge (faint divider + filled star +
count) on both the desktop and mobile menu entries.

Fetched client-side on purpose: LandingHeader is shared across every
marketing page, so one client fetch covers them all without threading a
server value through each render site, and each visitor calls the API
from their own IP, sidestepping the shared-outbound-IP rate limit the
server-side github-release fetcher works around with a PAT. The result
is memoized at module scope (plus in-flight dedupe); a failed fetch
caches null and the button degrades to the plain "GitHub" label.

* fix(landing): drop the star glyph from the GitHub star badge

In the GitHub button context the number already reads as the star count,
so the icon is redundant. Keep the divider + count only.
2026-06-23 15:51:14 +08:00

32 lines
1.0 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { formatStarCount } from "./use-github-stars";
describe("formatStarCount", () => {
it("renders counts below 1,000 exactly", () => {
expect(formatStarCount(0)).toBe("0");
expect(formatStarCount(7)).toBe("7");
expect(formatStarCount(999)).toBe("999");
});
it("formats thousands with one decimal, GitHub-style", () => {
expect(formatStarCount(37_600)).toBe("37.6k");
expect(formatStarCount(1_234)).toBe("1.2k");
expect(formatStarCount(12_300)).toBe("12.3k");
});
it("trims a trailing .0 ('1k', not '1.0k')", () => {
expect(formatStarCount(1_000)).toBe("1k");
expect(formatStarCount(2_000)).toBe("2k");
});
it("rounds to one decimal like the repo header", () => {
expect(formatStarCount(1_949)).toBe("1.9k");
expect(formatStarCount(1_990)).toBe("2k");
});
it("formats millions with an 'm' suffix", () => {
expect(formatStarCount(1_200_000)).toBe("1.2m");
expect(formatStarCount(2_000_000)).toBe("2m");
});
});