|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { |
| 3 | + type Contribution, |
| 4 | + formatStars, |
| 5 | + pullRequestUrl, |
| 6 | + sortContributions, |
| 7 | +} from "../src/lib/contributions"; |
| 8 | + |
| 9 | +const c = (repo: string, stars: number, pr: number): Contribution => ({ |
| 10 | + repo, |
| 11 | + emoji: "🔧", |
| 12 | + stars, |
| 13 | + pr, |
| 14 | + desc: { en: repo, es: repo }, |
| 15 | + state: "open", |
| 16 | +}); |
| 17 | + |
| 18 | +describe("pullRequestUrl", () => { |
| 19 | + it("builds the pull request url from the repo and number", () => { |
| 20 | + expect(pullRequestUrl(c("vitessio/vitess", 21000, 20700))).toBe( |
| 21 | + "https://github.com/vitessio/vitess/pull/20700", |
| 22 | + ); |
| 23 | + }); |
| 24 | +}); |
| 25 | + |
| 26 | +describe("formatStars", () => { |
| 27 | + it("abbreviates thousands with one decimal", () => { |
| 28 | + expect(formatStars(21000)).toBe("21k"); |
| 29 | + expect(formatStars(2170)).toBe("2.2k"); |
| 30 | + expect(formatStars(1000)).toBe("1k"); |
| 31 | + }); |
| 32 | + |
| 33 | + it("leaves counts below a thousand untouched", () => { |
| 34 | + expect(formatStars(610)).toBe("610"); |
| 35 | + }); |
| 36 | +}); |
| 37 | + |
| 38 | +describe("sortContributions", () => { |
| 39 | + it("puts bigger repos first and breaks ties by pull request number", () => { |
| 40 | + const sorted = sortContributions([ |
| 41 | + c("small/repo", 2170, 144), |
| 42 | + c("big/repo", 21000, 20701), |
| 43 | + c("big/repo", 21000, 20700), |
| 44 | + ]); |
| 45 | + expect(sorted.map((x) => x.pr)).toEqual([20700, 20701, 144]); |
| 46 | + }); |
| 47 | + |
| 48 | + it("does not mutate the input", () => { |
| 49 | + const input = [c("small/repo", 100, 2), c("big/repo", 900, 1)]; |
| 50 | + sortContributions(input); |
| 51 | + expect(input.map((x) => x.repo)).toEqual(["small/repo", "big/repo"]); |
| 52 | + }); |
| 53 | +}); |
0 commit comments