From 0e830c0cc36655e6bdb3089845285aa779eae4e3 Mon Sep 17 00:00:00 2001 From: rjoydip Date: Sun, 2 Aug 2020 15:41:16 +0530 Subject: [PATCH] fix(#287): number of forks now available --- src/fetchStats.js | 10 +++++++++- src/renderStatsCard.js | 7 +++++++ tests/api.test.js | 7 +++++-- tests/fetchStats.test.js | 4 ++++ tests/renderStatsCard.test.js | 7 +++++-- tests/utils.test.js | 2 +- 6 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/fetchStats.js b/src/fetchStats.js index 711d59ec..4293e306 100644 --- a/src/fetchStats.js +++ b/src/fetchStats.js @@ -6,6 +6,9 @@ const githubUsernameRegex = require("github-username-regex"); require("dotenv").config(); +const getRepositoriesArgs = (isFork = false) => + `first: 100, ownerAffiliations: OWNER, isFork: ${isFork}, orderBy: {direction: DESC, field: STARGAZERS}`; + const fetcher = (variables, token) => { return request( { @@ -30,7 +33,10 @@ const fetcher = (variables, token) => { followers { totalCount } - repositories(first: 100, ownerAffiliations: OWNER, isFork: false, orderBy: {direction: DESC, field: STARGAZERS}) { + forkRepositories: repositories(${getRepositoriesArgs(true)}) { + totalCount + } + repositories(${getRepositoriesArgs()}) { totalCount nodes { stargazers { @@ -98,6 +104,7 @@ async function fetchStats( totalStars: 0, contributedTo: 0, rank: { level: "C", score: 0 }, + totalForkRepositories: 0 }; let res = await retryer(fetcher, { login: username }); @@ -117,6 +124,7 @@ async function fetchStats( stats.name = user.name || user.login; stats.totalIssues = user.issues.totalCount; + stats.totalForkRepositories = user.forkRepositories.totalCount; stats.totalCommits = contributionCount.totalCommitContributions + experimental_totalCommits; diff --git a/src/renderStatsCard.js b/src/renderStatsCard.js index 1eb2d412..9fb2e89a 100644 --- a/src/renderStatsCard.js +++ b/src/renderStatsCard.js @@ -51,6 +51,7 @@ const renderStatsCard = (stats = {}, options = { hide: [] }) => { totalPRs, contributedTo, rank, + totalForkRepositories } = stats; const { hide = [], @@ -112,6 +113,12 @@ const renderStatsCard = (stats = {}, options = { hide: [] }) => { value: contributedTo, id: "contribs", }, + forks: { + icon: icons.fork, + label: "Total Forks", + value: totalForkRepositories, + id: "forks", + }, }; // filter out hidden stats defined by user & create the text nodes diff --git a/tests/api.test.js b/tests/api.test.js index 48111287..55ebaccc 100644 --- a/tests/api.test.js +++ b/tests/api.test.js @@ -14,6 +14,7 @@ const stats = { totalPRs: 400, contributedTo: 500, rank: null, + totalForkRepositories: 3 }; stats.rank = calculateRank({ totalCommits: stats.totalCommits, @@ -37,6 +38,7 @@ const data = { pullRequests: { totalCount: stats.totalPRs }, issues: { totalCount: stats.totalIssues }, followers: { totalCount: 0 }, + forkRepositories: { totalCount: stats.totalForkRepositories }, repositories: { totalCount: 1, nodes: [{ stargazers: { totalCount: 100 } }], @@ -106,7 +108,7 @@ describe("Test /api/", () => { const { req, res } = faker( { username: "anuraghazra", - hide: "issues,prs,contribs", + hide: "issues,prs,contribs,forks", show_icons: true, hide_border: true, line_height: 100, @@ -123,7 +125,7 @@ describe("Test /api/", () => { expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml"); expect(res.send).toBeCalledWith( renderStatsCard(stats, { - hide: ["issues", "prs", "contribs"], + hide: ["issues", "prs", "contribs", "forks"], show_icons: true, hide_border: true, line_height: 100, @@ -207,6 +209,7 @@ describe("Test /api/", () => { { ...stats, totalCommits: stats.totalCommits + 100, + totalForkRepositories: 3, rank: calculateRank({ totalCommits: stats.totalCommits + 100, totalRepos: 1, diff --git a/tests/fetchStats.test.js b/tests/fetchStats.test.js index b63312f4..e1c6846a 100644 --- a/tests/fetchStats.test.js +++ b/tests/fetchStats.test.js @@ -16,6 +16,7 @@ const data = { pullRequests: { totalCount: 300 }, issues: { totalCount: 200 }, followers: { totalCount: 100 }, + forkRepositories: { totalCount: 3 }, repositories: { totalCount: 5, nodes: [ @@ -70,6 +71,7 @@ describe("Test fetchStats", () => { totalPRs: 300, totalStars: 400, rank, + totalForkRepositories: 3 }); }); @@ -103,6 +105,7 @@ describe("Test fetchStats", () => { totalPRs: 300, totalStars: 400, rank, + totalForkRepositories: 3 }); }); @@ -131,6 +134,7 @@ describe("Test fetchStats", () => { totalPRs: 300, totalStars: 400, rank, + totalForkRepositories: 3 }); }); }); diff --git a/tests/renderStatsCard.test.js b/tests/renderStatsCard.test.js index 983a4066..9d6ae292 100644 --- a/tests/renderStatsCard.test.js +++ b/tests/renderStatsCard.test.js @@ -18,6 +18,7 @@ describe("Test renderStatsCard", () => { totalPRs: 400, contributedTo: 500, rank: { level: "A+", score: 40 }, + totalForkRepositories: 3 }; it("should render correctly", () => { @@ -29,11 +30,12 @@ describe("Test renderStatsCard", () => { expect( document.body.getElementsByTagName("svg")[0].getAttribute("height") - ).toBe("195"); + ).toBe("220"); expect(getByTestId(document.body, "stars").textContent).toBe("100"); expect(getByTestId(document.body, "commits").textContent).toBe("200"); expect(getByTestId(document.body, "issues").textContent).toBe("300"); expect(getByTestId(document.body, "prs").textContent).toBe("400"); + expect(getByTestId(document.body, "forks").textContent).toBe("3"); expect(getByTestId(document.body, "contribs").textContent).toBe("500"); expect(queryByTestId(document.body, "card-bg")).toBeInTheDocument(); expect(queryByTestId(document.body, "rank-circle")).toBeInTheDocument(); @@ -55,7 +57,7 @@ describe("Test renderStatsCard", () => { it("should hide individual stats", () => { document.body.innerHTML = renderStatsCard(stats, { - hide: ["issues", "prs", "contribs"], + hide: ["issues", "prs", "contribs", "forks"], }); expect( @@ -67,6 +69,7 @@ describe("Test renderStatsCard", () => { expect(queryByTestId(document.body, "issues")).toBeNull(); expect(queryByTestId(document.body, "prs")).toBeNull(); expect(queryByTestId(document.body, "contribs")).toBeNull(); + expect(queryByTestId(document.body, "forks")).toBeNull(); }); it("should hide_rank", () => { diff --git a/tests/utils.test.js b/tests/utils.test.js index ccd1864c..5760fb43 100644 --- a/tests/utils.test.js +++ b/tests/utils.test.js @@ -32,7 +32,7 @@ describe("Test utils.js", () => { expect( queryByTestId(document.body, "message").children[0] ).toHaveTextContent(/Something went wrong/gim); - expect(queryByTestId(document.body, "message").children[1]).toBeEmpty(2); + expect(queryByTestId(document.body, "message").children[1]).toBeEmptyDOMElement(2); // Secondary message document.body.innerHTML = renderError(