Skip to content

Commit

Permalink
Add api tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vn7n24fzkq committed Aug 29, 2020
1 parent 545c433 commit fc80cd0
Show file tree
Hide file tree
Showing 7 changed files with 320 additions and 67 deletions.
18 changes: 18 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"number-abbreviate": "^2.0.0"
},
"devDependencies": {
"axios-mock-adapter": "^1.18.2",
"jest": "^26.4.2",
"pre-commit": "^1.2.2"
},
Expand Down
2 changes: 1 addition & 1 deletion src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const core = require("@actions/core");
const { spawn } = require("child_process");
const Themes = require("./const/theme");
const Icons = require("./const/icon");
const getRepoLanguage = require("./github-api/langs");
const getRepoLanguage = require("./github-api/repos-per-language");
const getProfileDetails = require("./github-api/profile-details");
const {writeSVG,outputPath} = require("./utils/svg-writer");
const createDonutChartCard = require("./templates/donut-chart-card");
Expand Down
60 changes: 27 additions & 33 deletions src/github-api/profile-details.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,14 @@ const fetcher = (token, variables) => {
totalPullRequestContributions
totalPullRequestReviewContributions
contributionCalendar {
totalContributions
weeks {
contributionDays {
contributionCount
date
}
}
}
contributionCalendar {
totalContributions
}
}
}
}
Expand All @@ -52,50 +50,46 @@ const fetcher = (token, variables) => {
);
};

//repos per language
async function getProfileDetails(username) {
let result = {
name: "",
email: "",
joinedAt: "",
company: null,
websiteUrl: null,
twitterUsername: null,
location: null,
totalContributions: 0,
totalPublicRepos: 0,
contributions: [],
};

try {
let res = await fetcher(githubToken, {
login: username,
});
let res = await fetcher(githubToken, {
login: username,
});

if (res.data.errors) {
throw Error(res.data.errors[0].message || "Github api fail");
}
if (res.data.errors) {
throw Error(res.data.errors[0].message || "Github api failed");
}

let user = res.data.data.user;
let user = res.data.data.user;

result.name = user.name;
result.email = user.email;
result.joinedAt = user.createdAt;
result.totalContributions =
user.contributionsCollection.contributionCalendar.totalContributions;
result.totalPublicRepos = user.repositories.totalCount;
result.websiteUrl = user.websiteUrl;
result.company = user.company;
result.location = user.location;
result.name = user.name;
result.email = user.email;
result.joinedAt = user.createdAt;
result.totalContributions =
user.contributionsCollection.contributionCalendar.totalContributions;
result.totalPublicRepos = user.repositories.totalCount;
result.websiteUrl = user.websiteUrl;
result.company = user.company;
result.location = user.location;
result.twitterUsername = user.twitterUsername;

//contributions into array
for (let week of user.contributionsCollection.contributionCalendar.weeks) {
for (let day of week.contributionDays) {
day.date = new Date(day.date);
result.contributions.push(day);
}
}
} catch (e) {
if (e.response) {
console.log(e.response.data);
} else {
console.log(e);
//contributions into array
for (let week of user.contributionsCollection.contributionCalendar.weeks) {
for (let day of week.contributionDays) {
day.date = new Date(day.date);
result.contributions.push(day);
}
}

Expand Down
58 changes: 25 additions & 33 deletions src/github-api/langs.js → src/github-api/repos-per-language.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,44 +46,36 @@ async function getRepoLanguage(username) {
let languageMap = new Map();
let nodes = [];

try {
while (hasNextPage) {
let res = await fetcher(githubToken, {
login: username,
endCursor: cursor,
});
while (hasNextPage) {
let res = await fetcher(githubToken, {
login: username,
endCursor: cursor,
});

if (res.data.errors) {
throw Error(res.data.errors[0].message || "Github api fail");
}
cursor = res.data.data.user.repositories.pageInfo.endCursor;
hasNextPage = res.data.data.user.repositories.pageInfo.hasNextPage;
nodes.push(...res.data.data.user.repositories.nodes);
if (res.data.errors) {
throw Error(res.data.errors[0].message || "Github api fail");
}
cursor = res.data.data.user.repositories.pageInfo.endCursor;
hasNextPage = res.data.data.user.repositories.pageInfo.hasNextPage;
nodes.push(...res.data.data.user.repositories.nodes);
}

nodes.forEach((node) => {
if (node.languages.edges.length > 0) {
let edge = node.languages.edges[0];
let langName = edge.node.name;
if (languageMap.has(langName)) {
let lang = languageMap.get(langName);
lang.count += 1;
languageMap.set(langName, lang);
} else {
languageMap.set(langName, {
count: 1,
color: edge.node.color == null ? "#586e75" : edge.node.color,
});
}
nodes.forEach((node) => {
if (node.languages.edges.length > 0) {
let edge = node.languages.edges[0];
let langName = edge.node.name;
if (languageMap.has(langName)) {
let lang = languageMap.get(langName);
lang.count += 1;
languageMap.set(langName, lang);
} else {
languageMap.set(langName, {
count: 1,
color: edge.node.color == null ? "#586e75" : edge.node.color,
});
}
});
} catch (e) {
if (e.response) {
console.log(e.response.data);
} else {
console.log(e);
}
}
});

return languageMap;
}
Expand Down
108 changes: 108 additions & 0 deletions tests/github-api/profile-details.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
const getProfileDetails = require("../../src/github-api/profile-details");
const axios = require("axios");
const MockAdapter = require("axios-mock-adapter");
const mock = new MockAdapter(axios);

const data = {
data: {
user: {
name: "vn7",
email: "[email protected]",
createdAt: "2016-07-01T10:46:25Z",
twitterUsername: null,
company: "vn7",
location: "Taiwan",
websiteUrl: null,
repositories: {
totalCount: 30,
},
contributionsCollection: {
totalIssueContributions: 10,
totalCommitContributions: 20,
totalRepositoryContributions: 30,
totalPullRequestContributions: 40,
totalPullRequestReviewContributions: 50,
contributionCalendar: {
totalContributions: 1030,
weeks: [
{
contributionDays: [
{
date: "2019-09-06T00:00:00.000+00:00",
contributionCount: 20,
},
{
date: "2019-09-07T00:00:00.000+00:00",
contributionCount: 10,
},
],
},
{
contributionDays: [
{
date: "2020-01-12T00:00:00.000+00:00",
contributionCount: 5,
},
],
},
],
},
},
},
},
};

const error = {
errors: [
{
type: "NOT_FOUND",
path: ["user"],
locations: [],
message: "Github api failed",
},
],
};

afterEach(() => {
mock.reset();
});

describe("github api for profile details", () => {
it("should get correct profile data", async () => {
mock.onPost("https://api.github.com/graphql").reply(200, data);
let profileDetails = await getProfileDetails("vn7n24fzkq");
expect(profileDetails).toStrictEqual({
name: "vn7",
email: "[email protected]",
joinedAt: "2016-07-01T10:46:25Z",
company: "vn7",
location: "Taiwan",
websiteUrl: null,
twitterUsername: null,
totalContributions: 1030,
totalPublicRepos: 30,
contributions: [
{
date: new Date("2019-09-06T00:00:00.000+00:00"),
contributionCount: 20,
},
{
date: new Date("2019-09-07T00:00:00.000+00:00"),
contributionCount: 10,
},
{
date: new Date("2020-01-12T00:00:00.000+00:00"),
contributionCount: 5,
},
],
});
});

it("should throw error when api failed", async () => {
mock.onPost("https://api.github.com/graphql").reply(200, error);
await expect(getProfileDetails("vn7n24fzkq")).rejects.toThrow(
"Github api failed"
);
});
});

Loading

0 comments on commit fc80cd0

Please sign in to comment.