Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pull] master from anuraghazra:master #11

Merged
merged 11 commits into from
Aug 26, 2023
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// Possible Errors (overrides from recommended set)

// "no-extra-parens": "error",
// "no-unexpected-multiline": "error",
"no-unexpected-multiline": "error",

// All JSDoc comments must be valid

Expand Down
4 changes: 4 additions & 0 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,21 @@ lang-card:
- src/fetchers/top-languages-fetcher.js
- tests/fetchTopLanguages.test.js
- tests/renderTopLanguagesCard.test.js
- tests/top-langs.test.js
repo-card:
- api/pin.js
- src/cards/repo-card.js
- src/fetchers/repo-fetcher.js
- tests/fetchRepo.test.js
- tests/renderRepoCard.test.js
- tests/pin.test.js
stats-card:
- api/index.js
- src/cards/stats-card.js
- src/fetchers/stats-fetcher.js
- tests/fetchStats.test.js
- tests/renderStatsCard.test.js
- tests/api.test.js
wakatime-card:
- api/wakatime.js
- src/cards/wakatime-card.js
Expand All @@ -41,6 +44,7 @@ gist-card:
- src/fetchers/gist-fetcher.js
- tests/fetchGist.test.js
- tests/renderGistCard.test.js
- tests/gist.test.js
ranks: src/calculateRank.js
ci:
- .github/workflows/*
Expand Down
6 changes: 0 additions & 6 deletions src/fetchers/top-languages-fetcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,6 @@ const fetchTopLanguages = async (

const res = await retryer(fetcher, { login: username });

if (res.data.errors) {
logger.error(res.data.errors);
throw Error(res.data.errors[0].message || "Could not fetch user");
}

// Catch GraphQL errors.
if (res.data.errors) {
logger.error(res.data.errors);
if (res.data.errors[0].type === "NOT_FOUND") {
Expand Down
20 changes: 20 additions & 0 deletions tests/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,4 +276,24 @@ describe("Test /api/", () => {
}),
);
});

it("should render error card if username in blacklist", async () => {
const { req, res } = faker({ username: "renovate-bot" }, data_stats);

await api(req, res);

expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml");
expect(res.send).toBeCalledWith(renderError("Something went wrong"));
});

it("should render error card when wrong locale is provided", async () => {
const { req, res } = faker({ locale: "asdf" }, data_stats);

await api(req, res);

expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml");
expect(res.send).toBeCalledWith(
renderError("Something went wrong", "Language not found"),
);
});
});
22 changes: 21 additions & 1 deletion tests/fetchTopLanguages.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,31 @@ describe("FetchTopLanguages", () => {
});
});

it("should throw error", async () => {
it("should throw specific error when user not found", async () => {
mock.onPost("https://api.github.com/graphql").reply(200, error);

await expect(fetchTopLanguages("anuraghazra")).rejects.toThrow(
"Could not resolve to a User with the login of 'noname'.",
);
});

it("should throw other errors with their message", async () => {
mock.onPost("https://api.github.com/graphql").reply(200, {
errors: [{ message: "Some test GraphQL error" }],
});

await expect(fetchTopLanguages("anuraghazra")).rejects.toThrow(
"Some test GraphQL error",
);
});

it("should throw error with specific message when error does not contain message property", async () => {
mock.onPost("https://api.github.com/graphql").reply(200, {
errors: [{ type: "TEST" }],
});

await expect(fetchTopLanguages("anuraghazra")).rejects.toThrow(
"Something went while trying to retrieve the language data using the GraphQL API.",
);
});
});
92 changes: 1 addition & 91 deletions tests/fetchWakatime.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,97 +112,7 @@ describe("Wakatime fetcher", () => {
.reply(200, wakaTimeData);

const repo = await fetchWakatimeStats({ username });
expect(repo).toMatchInlineSnapshot(`
{
"categories": [
{
"digital": "22:40",
"hours": 22,
"minutes": 40,
"name": "Coding",
"percent": 100,
"text": "22 hrs 40 mins",
"total_seconds": 81643.570077,
},
],
"daily_average": 16095,
"daily_average_including_other_language": 16329,
"days_including_holidays": 7,
"days_minus_holidays": 5,
"editors": [
{
"digital": "22:40",
"hours": 22,
"minutes": 40,
"name": "VS Code",
"percent": 100,
"text": "22 hrs 40 mins",
"total_seconds": 81643.570077,
},
],
"holidays": 2,
"human_readable_daily_average": "4 hrs 28 mins",
"human_readable_daily_average_including_other_language": "4 hrs 32 mins",
"human_readable_total": "22 hrs 21 mins",
"human_readable_total_including_other_language": "22 hrs 40 mins",
"id": "random hash",
"is_already_updating": false,
"is_coding_activity_visible": true,
"is_including_today": false,
"is_other_usage_visible": true,
"is_stuck": false,
"is_up_to_date": true,
"languages": [
{
"digital": "0:19",
"hours": 0,
"minutes": 19,
"name": "Other",
"percent": 1.43,
"text": "19 mins",
"total_seconds": 1170.434361,
},
{
"digital": "0:01",
"hours": 0,
"minutes": 1,
"name": "TypeScript",
"percent": 0.1,
"text": "1 min",
"total_seconds": 83.293809,
},
{
"digital": "0:00",
"hours": 0,
"minutes": 0,
"name": "YAML",
"percent": 0.07,
"text": "0 secs",
"total_seconds": 54.975151,
},
],
"operating_systems": [
{
"digital": "22:40",
"hours": 22,
"minutes": 40,
"name": "Mac",
"percent": 100,
"text": "22 hrs 40 mins",
"total_seconds": 81643.570077,
},
],
"percent_calculated": 100,
"range": "last_7_days",
"status": "ok",
"timeout": 15,
"total_seconds": 80473.135716,
"total_seconds_including_other_language": 81643.570077,
"user_id": "random hash",
"username": "anuraghazra",
"writes_only": false,
}
`);
expect(repo).toStrictEqual(wakaTimeData.data);
});

it("should throw error if username param missing", async () => {
Expand Down
20 changes: 20 additions & 0 deletions tests/gist.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,24 @@ describe("Test /api/gist", () => {
expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml");
expect(res.send).toBeCalledWith(renderError("Gist not found"));
});

it("should render error if wrong locale is provided", async () => {
const req = {
query: {
id: "bbfce31e0217a3689c8d961a356cb10d",
locale: "asdf",
},
};
const res = {
setHeader: jest.fn(),
send: jest.fn(),
};

await gist(req, res);

expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml");
expect(res.send).toBeCalledWith(
renderError("Something went wrong", "Language not found"),
);
});
});
41 changes: 41 additions & 0 deletions tests/pin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,45 @@ describe("Test /api/pin", () => {
renderError("Organization Repository Not found"),
);
});

it("should render error card if username in blacklist", async () => {
const req = {
query: {
username: "renovate-bot",
repo: "convoychat",
},
};
const res = {
setHeader: jest.fn(),
send: jest.fn(),
};
mock.onPost("https://api.github.com/graphql").reply(200, data_user);

await pin(req, res);

expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml");
expect(res.send).toBeCalledWith(renderError("Something went wrong"));
});

it("should render error card if wrong locale provided", async () => {
const req = {
query: {
username: "anuraghazra",
repo: "convoychat",
locale: "asdf",
},
};
const res = {
setHeader: jest.fn(),
send: jest.fn(),
};
mock.onPost("https://api.github.com/graphql").reply(200, data_user);

await pin(req, res);

expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml");
expect(res.send).toBeCalledWith(
renderError("Something went wrong", "Language not found"),
);
});
});
46 changes: 45 additions & 1 deletion tests/top-langs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,12 @@ describe("Test /api/top-langs", () => {
await topLangs(req, res);

expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml");
expect(res.send).toBeCalledWith(renderError(error.errors[0].message));
expect(res.send).toBeCalledWith(
renderError(
error.errors[0].message,
"Make sure the provided username is not an organization",
),
);
});

it("should render error card on incorrect layout input", async () => {
Expand All @@ -162,4 +167,43 @@ describe("Test /api/top-langs", () => {
renderError("Something went wrong", "Incorrect layout input"),
);
});

it("should render error card if username in blacklist", async () => {
const req = {
query: {
username: "renovate-bot",
},
};
const res = {
setHeader: jest.fn(),
send: jest.fn(),
};
mock.onPost("https://api.github.com/graphql").reply(200, data_langs);

await topLangs(req, res);

expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml");
expect(res.send).toBeCalledWith(renderError("Something went wrong"));
});

it("should render error card if wrong locale provided", async () => {
const req = {
query: {
username: "anuraghazra",
locale: "asdf",
},
};
const res = {
setHeader: jest.fn(),
send: jest.fn(),
};
mock.onPost("https://api.github.com/graphql").reply(200, data_langs);

await topLangs(req, res);

expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml");
expect(res.send).toBeCalledWith(
renderError("Something went wrong", "Locale not found"),
);
});
});