2020-07-12 22:38:01 +08:00
|
|
|
require("@testing-library/jest-dom");
|
|
|
|
const axios = require("axios");
|
|
|
|
const MockAdapter = require("axios-mock-adapter");
|
|
|
|
const api = require("../api/index");
|
2020-08-02 15:37:26 +08:00
|
|
|
const renderStatsCard = require("../src/cards/stats-card");
|
|
|
|
const { renderError, CONSTANTS } = require("../src/common/utils");
|
2020-07-13 22:11:47 +08:00
|
|
|
const calculateRank = require("../src/calculateRank");
|
2020-07-12 22:38:01 +08:00
|
|
|
|
|
|
|
const stats = {
|
|
|
|
name: "Anurag Hazra",
|
|
|
|
totalStars: 100,
|
|
|
|
totalCommits: 200,
|
|
|
|
totalIssues: 300,
|
|
|
|
totalPRs: 400,
|
|
|
|
contributedTo: 500,
|
2020-07-13 22:11:47 +08:00
|
|
|
rank: null,
|
2020-07-12 22:38:01 +08:00
|
|
|
};
|
2020-07-13 22:11:47 +08:00
|
|
|
stats.rank = calculateRank({
|
|
|
|
totalCommits: stats.totalCommits,
|
|
|
|
totalRepos: 1,
|
|
|
|
followers: 0,
|
|
|
|
contributions: stats.contributedTo,
|
|
|
|
stargazers: stats.totalStars,
|
|
|
|
prs: stats.totalPRs,
|
|
|
|
issues: stats.totalIssues,
|
|
|
|
});
|
2020-07-12 22:38:01 +08:00
|
|
|
|
|
|
|
const data = {
|
|
|
|
data: {
|
|
|
|
user: {
|
|
|
|
name: stats.name,
|
|
|
|
repositoriesContributedTo: { totalCount: stats.contributedTo },
|
2020-07-24 22:04:38 +08:00
|
|
|
contributionsCollection: {
|
|
|
|
totalCommitContributions: stats.totalCommits,
|
|
|
|
restrictedContributionsCount: 100,
|
|
|
|
},
|
2020-07-12 22:38:01 +08:00
|
|
|
pullRequests: { totalCount: stats.totalPRs },
|
|
|
|
issues: { totalCount: stats.totalIssues },
|
2020-07-13 22:11:47 +08:00
|
|
|
followers: { totalCount: 0 },
|
2020-07-12 22:38:01 +08:00
|
|
|
repositories: {
|
2020-07-13 22:11:47 +08:00
|
|
|
totalCount: 1,
|
2020-07-12 22:38:01 +08:00
|
|
|
nodes: [{ stargazers: { totalCount: 100 } }],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const error = {
|
|
|
|
errors: [
|
|
|
|
{
|
|
|
|
type: "NOT_FOUND",
|
|
|
|
path: ["user"],
|
|
|
|
locations: [],
|
|
|
|
message: "Could not fetch user",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
const mock = new MockAdapter(axios);
|
|
|
|
|
2020-07-21 00:13:51 +08:00
|
|
|
const faker = (query, data) => {
|
|
|
|
const req = {
|
|
|
|
query: {
|
|
|
|
username: "anuraghazra",
|
|
|
|
...query,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
const res = {
|
|
|
|
setHeader: jest.fn(),
|
|
|
|
send: jest.fn(),
|
|
|
|
};
|
|
|
|
mock.onPost("https://api.github.com/graphql").reply(200, data);
|
|
|
|
|
|
|
|
return { req, res };
|
|
|
|
};
|
|
|
|
|
2020-07-12 22:38:01 +08:00
|
|
|
afterEach(() => {
|
|
|
|
mock.reset();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("Test /api/", () => {
|
|
|
|
it("should test the request", async () => {
|
2020-07-21 00:13:51 +08:00
|
|
|
const { req, res } = faker({}, data);
|
2020-07-12 22:38:01 +08:00
|
|
|
|
|
|
|
await api(req, res);
|
|
|
|
|
|
|
|
expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml");
|
|
|
|
expect(res.send).toBeCalledWith(renderStatsCard(stats, { ...req.query }));
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should render error card on error", async () => {
|
2020-07-21 00:13:51 +08:00
|
|
|
const { req, res } = faker({}, error);
|
2020-07-12 22:38:01 +08:00
|
|
|
|
|
|
|
await api(req, res);
|
|
|
|
|
|
|
|
expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml");
|
2020-07-25 15:59:32 +08:00
|
|
|
expect(res.send).toBeCalledWith(
|
|
|
|
renderError(
|
|
|
|
error.errors[0].message,
|
|
|
|
"Make sure the provided username is not an organization"
|
|
|
|
)
|
|
|
|
);
|
2020-07-12 22:38:01 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should get the query options", async () => {
|
2020-07-21 00:13:51 +08:00
|
|
|
const { req, res } = faker(
|
|
|
|
{
|
2020-07-12 22:38:01 +08:00
|
|
|
username: "anuraghazra",
|
2020-07-23 23:31:23 +08:00
|
|
|
hide: "issues,prs,contribs",
|
2020-07-12 22:38:01 +08:00
|
|
|
show_icons: true,
|
|
|
|
hide_border: true,
|
|
|
|
line_height: 100,
|
|
|
|
title_color: "fff",
|
|
|
|
icon_color: "fff",
|
|
|
|
text_color: "fff",
|
|
|
|
bg_color: "fff",
|
|
|
|
},
|
2020-07-21 00:13:51 +08:00
|
|
|
data
|
|
|
|
);
|
2020-07-12 22:38:01 +08:00
|
|
|
|
|
|
|
await api(req, res);
|
|
|
|
|
|
|
|
expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml");
|
|
|
|
expect(res.send).toBeCalledWith(
|
|
|
|
renderStatsCard(stats, {
|
|
|
|
hide: ["issues", "prs", "contribs"],
|
|
|
|
show_icons: true,
|
|
|
|
hide_border: true,
|
|
|
|
line_height: 100,
|
|
|
|
title_color: "fff",
|
|
|
|
icon_color: "fff",
|
|
|
|
text_color: "fff",
|
|
|
|
bg_color: "fff",
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
2020-07-21 00:13:51 +08:00
|
|
|
|
|
|
|
it("should have proper cache", async () => {
|
|
|
|
const { req, res } = faker({}, data);
|
|
|
|
mock.onPost("https://api.github.com/graphql").reply(200, data);
|
|
|
|
|
|
|
|
await api(req, res);
|
|
|
|
|
|
|
|
expect(res.setHeader.mock.calls).toEqual([
|
|
|
|
["Content-Type", "image/svg+xml"],
|
2020-08-13 21:56:42 +08:00
|
|
|
["Cache-Control", `public, max-age=${CONSTANTS.TWO_HOURS}`],
|
2020-07-21 00:13:51 +08:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should set proper cache", async () => {
|
2020-08-13 21:56:42 +08:00
|
|
|
const { req, res } = faker({ cache_seconds: 8000 }, data);
|
2020-07-21 00:13:51 +08:00
|
|
|
await api(req, res);
|
|
|
|
|
|
|
|
expect(res.setHeader.mock.calls).toEqual([
|
|
|
|
["Content-Type", "image/svg+xml"],
|
2020-08-13 21:56:42 +08:00
|
|
|
["Cache-Control", `public, max-age=${8000}`],
|
2020-07-21 00:13:51 +08:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should set proper cache with clamped values", async () => {
|
|
|
|
{
|
|
|
|
let { req, res } = faker({ cache_seconds: 200000 }, data);
|
|
|
|
await api(req, res);
|
|
|
|
|
|
|
|
expect(res.setHeader.mock.calls).toEqual([
|
|
|
|
["Content-Type", "image/svg+xml"],
|
|
|
|
["Cache-Control", `public, max-age=${CONSTANTS.ONE_DAY}`],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// note i'm using block scoped vars
|
|
|
|
{
|
|
|
|
let { req, res } = faker({ cache_seconds: 0 }, data);
|
|
|
|
await api(req, res);
|
|
|
|
|
|
|
|
expect(res.setHeader.mock.calls).toEqual([
|
|
|
|
["Content-Type", "image/svg+xml"],
|
2020-08-13 21:56:42 +08:00
|
|
|
["Cache-Control", `public, max-age=${CONSTANTS.TWO_HOURS}`],
|
2020-07-21 00:13:51 +08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
let { req, res } = faker({ cache_seconds: -10000 }, data);
|
|
|
|
await api(req, res);
|
|
|
|
|
|
|
|
expect(res.setHeader.mock.calls).toEqual([
|
|
|
|
["Content-Type", "image/svg+xml"],
|
2020-08-13 21:56:42 +08:00
|
|
|
["Cache-Control", `public, max-age=${CONSTANTS.TWO_HOURS}`],
|
2020-07-21 00:13:51 +08:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
});
|
2020-07-24 22:04:38 +08:00
|
|
|
|
|
|
|
it("should add private contributions", async () => {
|
|
|
|
const { req, res } = faker(
|
|
|
|
{
|
|
|
|
username: "anuraghazra",
|
|
|
|
count_private: true,
|
|
|
|
},
|
|
|
|
data
|
|
|
|
);
|
|
|
|
|
|
|
|
await api(req, res);
|
|
|
|
|
|
|
|
expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml");
|
|
|
|
expect(res.send).toBeCalledWith(
|
|
|
|
renderStatsCard(
|
|
|
|
{
|
|
|
|
...stats,
|
|
|
|
totalCommits: stats.totalCommits + 100,
|
|
|
|
rank: calculateRank({
|
|
|
|
totalCommits: stats.totalCommits + 100,
|
|
|
|
totalRepos: 1,
|
|
|
|
followers: 0,
|
|
|
|
contributions: stats.contributedTo,
|
|
|
|
stargazers: stats.totalStars,
|
|
|
|
prs: stats.totalPRs,
|
|
|
|
issues: stats.totalIssues,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
{}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
});
|
2020-07-12 22:38:01 +08:00
|
|
|
});
|