feat: added rankings

This commit is contained in:
anuraghazra
2020-07-13 19:41:47 +05:30
parent 8e8d7dd64d
commit db49ca7b71
8 changed files with 182 additions and 5 deletions

View File

@ -4,6 +4,7 @@ const MockAdapter = require("axios-mock-adapter");
const api = require("../api/index");
const renderStatsCard = require("../src/renderStatsCard");
const { renderError } = require("../src/utils");
const calculateRank = require("../src/calculateRank");
const stats = {
name: "Anurag Hazra",
@ -12,7 +13,17 @@ const stats = {
totalIssues: 300,
totalPRs: 400,
contributedTo: 500,
rank: null,
};
stats.rank = calculateRank({
totalCommits: stats.totalCommits,
totalRepos: 1,
followers: 0,
contributions: stats.contributedTo,
stargazers: stats.totalStars,
prs: stats.totalPRs,
issues: stats.totalIssues,
});
const data = {
data: {
@ -22,7 +33,9 @@ const data = {
contributionsCollection: { totalCommitContributions: stats.totalCommits },
pullRequests: { totalCount: stats.totalPRs },
issues: { totalCount: stats.totalIssues },
followers: { totalCount: 0 },
repositories: {
totalCount: 1,
nodes: [{ stargazers: { totalCount: 100 } }],
},
},

View File

@ -0,0 +1,18 @@
require("@testing-library/jest-dom");
const calculateRank = require("../src/calculateRank");
describe("Test calculateRank", () => {
it("should calculate rank correctly", () => {
expect(
calculateRank({
totalCommits: 100,
totalRepos: 5,
followers: 100,
contributions: 61,
stargazers: 400,
prs: 300,
issues: 200,
})
).toStrictEqual({ level: "S+", score: 192.13 });
});
});

View File

@ -2,6 +2,7 @@ require("@testing-library/jest-dom");
const axios = require("axios");
const MockAdapter = require("axios-mock-adapter");
const fetchStats = require("../src/fetchStats");
const calculateRank = require("../src/calculateRank");
const data = {
data: {
@ -11,7 +12,9 @@ const data = {
contributionsCollection: { totalCommitContributions: 100 },
pullRequests: { totalCount: 300 },
issues: { totalCount: 200 },
followers: { totalCount: 100 },
repositories: {
totalCount: 5,
nodes: [
{ stargazers: { totalCount: 100 } },
{ stargazers: { totalCount: 100 } },
@ -46,6 +49,16 @@ describe("Test fetchStats", () => {
mock.onPost("https://api.github.com/graphql").reply(200, data);
let stats = await fetchStats("anuraghazra");
const rank = calculateRank({
totalCommits: 100,
totalRepos: 5,
followers: 100,
contributions: 61,
stargazers: 400,
prs: 300,
issues: 200,
});
expect(stats).toStrictEqual({
contributedTo: 61,
name: "Anurag Hazra",
@ -53,6 +66,7 @@ describe("Test fetchStats", () => {
totalIssues: 200,
totalPRs: 300,
totalStars: 400,
rank,
});
});
@ -63,4 +77,4 @@ describe("Test fetchStats", () => {
"Could not fetch user"
);
});
});
});

View File

@ -12,6 +12,7 @@ describe("Test renderStatsCard", () => {
totalIssues: 300,
totalPRs: 400,
contributedTo: 500,
rank: { level: "A+", score: 100 },
};
it("should render correctly", () => {
@ -30,6 +31,7 @@ describe("Test renderStatsCard", () => {
expect(getByTestId(document.body, "prs").textContent).toBe("400");
expect(getByTestId(document.body, "contribs").textContent).toBe("500");
expect(queryByTestId(document.body, "card-border")).toBeInTheDocument();
expect(queryByTestId(document.body, "rank-circle")).toBeInTheDocument();
});
it("should hide individual stats", () => {
@ -39,7 +41,8 @@ describe("Test renderStatsCard", () => {
expect(
document.body.getElementsByTagName("svg")[0].getAttribute("height")
).toBe("120");
).toBe("150"); // height should be 150 because we clamped it.
expect(queryByTestId(document.body, "stars")).toBeDefined();
expect(queryByTestId(document.body, "commits")).toBeDefined();
expect(queryByTestId(document.body, "issues")).toBeNull();
@ -53,6 +56,12 @@ describe("Test renderStatsCard", () => {
expect(queryByTestId(document.body, "card-border")).not.toBeInTheDocument();
});
it("should hide_rank", () => {
document.body.innerHTML = renderStatsCard(stats, { hide_rank: true });
expect(queryByTestId(document.body, "rank-circle")).not.toBeInTheDocument();
});
it("should render default colors properly", () => {
document.body.innerHTML = renderStatsCard(stats);