Skip to content

Commit

Permalink
Top langs card: Remove unreachable code from fetcher and increase tes…
Browse files Browse the repository at this point in the history
…ts coverage (#3126)
  • Loading branch information
qwerty541 authored Aug 22, 2023
1 parent 51fcae8 commit 0ac5280
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
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
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.",
);
});
});
7 changes: 6 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 Down

0 comments on commit 0ac5280

Please sign in to comment.