2022-09-24 16:20:54 +08:00
|
|
|
import "@testing-library/jest-dom";
|
|
|
|
import axios from "axios";
|
|
|
|
import MockAdapter from "axios-mock-adapter";
|
2022-09-24 20:10:06 +08:00
|
|
|
import { fetchTopLanguages } from "../src/fetchers/top-languages-fetcher.js";
|
2020-07-21 19:52:22 +08:00
|
|
|
|
|
|
|
const mock = new MockAdapter(axios);
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
mock.reset();
|
|
|
|
});
|
|
|
|
|
|
|
|
const data_langs = {
|
|
|
|
data: {
|
|
|
|
user: {
|
|
|
|
repositories: {
|
|
|
|
nodes: [
|
|
|
|
{
|
2021-12-13 22:57:27 +08:00
|
|
|
name: "test-repo-1",
|
2020-07-21 19:52:22 +08:00
|
|
|
languages: {
|
|
|
|
edges: [{ size: 100, node: { color: "#0f0", name: "HTML" } }],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-12-13 22:57:27 +08:00
|
|
|
name: "test-repo-2",
|
2020-07-21 19:52:22 +08:00
|
|
|
languages: {
|
|
|
|
edges: [{ size: 100, node: { color: "#0f0", name: "HTML" } }],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-12-13 22:57:27 +08:00
|
|
|
name: "test-repo-3",
|
2020-07-21 19:52:22 +08:00
|
|
|
languages: {
|
|
|
|
edges: [
|
|
|
|
{ size: 100, node: { color: "#0ff", name: "javascript" } },
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-12-13 22:57:27 +08:00
|
|
|
name: "test-repo-4",
|
2020-07-21 19:52:22 +08:00
|
|
|
languages: {
|
|
|
|
edges: [
|
|
|
|
{ size: 100, node: { color: "#0ff", name: "javascript" } },
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const error = {
|
|
|
|
errors: [
|
|
|
|
{
|
|
|
|
type: "NOT_FOUND",
|
|
|
|
path: ["user"],
|
|
|
|
locations: [],
|
|
|
|
message: "Could not resolve to a User with the login of 'noname'.",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
describe("FetchTopLanguages", () => {
|
|
|
|
it("should fetch correct language data", async () => {
|
|
|
|
mock.onPost("https://api.github.com/graphql").reply(200, data_langs);
|
|
|
|
|
|
|
|
let repo = await fetchTopLanguages("anuraghazra");
|
|
|
|
expect(repo).toStrictEqual({
|
|
|
|
HTML: {
|
|
|
|
color: "#0f0",
|
|
|
|
name: "HTML",
|
|
|
|
size: 200,
|
|
|
|
},
|
|
|
|
javascript: {
|
|
|
|
color: "#0ff",
|
|
|
|
name: "javascript",
|
|
|
|
size: 200,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-12-13 22:57:27 +08:00
|
|
|
it("should fetch correct language data while excluding the 'test-repo-1' repository", async () => {
|
|
|
|
mock.onPost("https://api.github.com/graphql").reply(200, data_langs);
|
|
|
|
|
2022-09-24 16:20:54 +08:00
|
|
|
let repo = await fetchTopLanguages("anuraghazra", ["test-repo-1"]);
|
2021-12-13 22:57:27 +08:00
|
|
|
expect(repo).toStrictEqual({
|
|
|
|
HTML: {
|
|
|
|
color: "#0f0",
|
|
|
|
name: "HTML",
|
|
|
|
size: 100,
|
|
|
|
},
|
|
|
|
javascript: {
|
|
|
|
color: "#0ff",
|
|
|
|
name: "javascript",
|
|
|
|
size: 200,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-07-21 19:52:22 +08:00
|
|
|
it("should throw error", async () => {
|
|
|
|
mock.onPost("https://api.github.com/graphql").reply(200, error);
|
|
|
|
|
|
|
|
await expect(fetchTopLanguages("anuraghazra")).rejects.toThrow(
|
2020-09-25 00:08:14 +08:00
|
|
|
"Could not resolve to a User with the login of 'noname'.",
|
2020-07-21 19:52:22 +08:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|