test(web): add unit tests for pure helper functions (#2454)
* test(web): add unit tests for pure helper functions Add vitest configuration and tests for: - relativeTime: time formatting (just now, minutes, hours, days, months, years) - lastPageFromLink: GitHub Link header pagination parsing These are the first tests for the web frontend. The test framework (vitest) was already in package.json but had no config or test files. * test(web): exercise real GitHub helpers --------- Co-authored-by: Hu Qiantao <huqiantao@HudeMacBook-Air.local> Co-authored-by: Hunter B <hmbown@gmail.com>
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
||||
import { lastPageFromLink, relativeTime } from "./github";
|
||||
|
||||
describe("relativeTime", () => {
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers();
|
||||
vi.setSystemTime(new Date("2026-06-01T12:00:00Z"));
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it("returns 'just now' for less than 30 seconds ago", () => {
|
||||
expect(relativeTime("2026-06-01T11:59:45Z")).toBe("just now");
|
||||
});
|
||||
|
||||
it("returns minutes for < 1 hour", () => {
|
||||
expect(relativeTime("2026-06-01T11:55:00Z")).toBe("5m");
|
||||
expect(relativeTime("2026-06-01T11:30:00Z")).toBe("30m");
|
||||
});
|
||||
|
||||
it("returns hours for < 1 day", () => {
|
||||
expect(relativeTime("2026-06-01T09:00:00Z")).toBe("3h");
|
||||
expect(relativeTime("2026-05-31T18:00:00Z")).toBe("18h");
|
||||
});
|
||||
|
||||
it("returns days before the 30-day month boundary", () => {
|
||||
expect(relativeTime("2026-05-25T12:00:00Z")).toBe("7d");
|
||||
expect(relativeTime("2026-05-03T12:00:00Z")).toBe("29d");
|
||||
});
|
||||
|
||||
it("returns months for < 12 months", () => {
|
||||
expect(relativeTime("2026-05-02T12:00:00Z")).toBe("1mo");
|
||||
expect(relativeTime("2026-03-01T12:00:00Z")).toBe("3mo");
|
||||
expect(relativeTime("2025-08-01T12:00:00Z")).toBe("10mo");
|
||||
});
|
||||
|
||||
it("returns years for >= 12 months", () => {
|
||||
expect(relativeTime("2024-06-01T12:00:00Z")).toBe("2y");
|
||||
expect(relativeTime("2025-01-01T00:00:00Z")).toBe("1y");
|
||||
});
|
||||
});
|
||||
|
||||
describe("lastPageFromLink", () => {
|
||||
it("returns undefined for null input", () => {
|
||||
expect(lastPageFromLink(null)).toBeUndefined();
|
||||
});
|
||||
|
||||
it("returns undefined for empty string", () => {
|
||||
expect(lastPageFromLink("")).toBeUndefined();
|
||||
});
|
||||
|
||||
it("extracts page from Link header with last rel", () => {
|
||||
const link =
|
||||
'<https://api.github.com/repos/Hmbown/CodeWhale/issues?page=5>; rel="last"';
|
||||
expect(lastPageFromLink(link)).toBe(5);
|
||||
});
|
||||
|
||||
it("extracts page from multi-part Link header", () => {
|
||||
const link = [
|
||||
'<https://api.github.com/repos/Hmbown/CodeWhale/issues?page=1>; rel="prev"',
|
||||
'<https://api.github.com/repos/Hmbown/CodeWhale/issues?page=3>; rel="last"',
|
||||
].join(", ");
|
||||
expect(lastPageFromLink(link)).toBe(3);
|
||||
});
|
||||
|
||||
it("returns undefined when no last rel present", () => {
|
||||
const link =
|
||||
'<https://api.github.com/repos/Hmbown/CodeWhale/issues?page=1>; rel="prev"';
|
||||
expect(lastPageFromLink(link)).toBeUndefined();
|
||||
});
|
||||
|
||||
it("returns undefined for invalid URL format", () => {
|
||||
const link = "not-a-valid-link-header; rel=last";
|
||||
expect(lastPageFromLink(link)).toBeUndefined();
|
||||
});
|
||||
});
|
||||
+1
-1
@@ -70,7 +70,7 @@ async function contributorCount(res: Response): Promise<number> {
|
||||
return MIN_KNOWN_CONTRIBUTORS;
|
||||
}
|
||||
|
||||
function lastPageFromLink(link: string | null): number | undefined {
|
||||
export function lastPageFromLink(link: string | null): number | undefined {
|
||||
if (!link) return undefined;
|
||||
|
||||
for (const part of link.split(",")) {
|
||||
|
||||
Generated
+1562
-12
File diff suppressed because it is too large
Load Diff
+3
-1
@@ -8,6 +8,7 @@
|
||||
"prebuild": "node scripts/derive-facts.mjs",
|
||||
"build": "next build",
|
||||
"start": "next start",
|
||||
"test": "vitest run",
|
||||
"lint": "eslint .",
|
||||
"preview": "opennextjs-cloudflare preview",
|
||||
"predeploy": "node scripts/check-kv-id.mjs",
|
||||
@@ -31,9 +32,10 @@
|
||||
"postcss": "^8.5.14",
|
||||
"tailwindcss": "^3.4.17",
|
||||
"typescript": "^5.7.3",
|
||||
"vitest": "^4.1.7",
|
||||
"wrangler": "^4.86.0"
|
||||
},
|
||||
"overrides": {
|
||||
"postcss": "$postcss"
|
||||
"postcss": "^8.5.14"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
import { defineConfig } from "vitest/config";
|
||||
|
||||
export default defineConfig({
|
||||
test: {
|
||||
include: ["lib/**/*.test.ts", "components/**/*.test.tsx"],
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user