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 { fetchRepo } from "../src/fetchers/repo-fetcher.js";
|
2023-07-03 14:54:19 +08:00
|
|
|
import { expect, it, describe, afterEach } from "@jest/globals";
|
2020-07-12 00:40:13 +08:00
|
|
|
|
|
|
|
const data_repo = {
|
|
|
|
repository: {
|
|
|
|
name: "convoychat",
|
|
|
|
stargazers: { totalCount: 38000 },
|
|
|
|
description: "Help us take over the world! React + TS + GraphQL Chat App",
|
|
|
|
primaryLanguage: {
|
|
|
|
color: "#2b7489",
|
|
|
|
id: "MDg6TGFuZ3VhZ2UyODc=",
|
|
|
|
name: "TypeScript",
|
|
|
|
},
|
|
|
|
forkCount: 100,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const data_user = {
|
|
|
|
data: {
|
2021-09-26 23:32:27 +08:00
|
|
|
user: { repository: data_repo.repository },
|
2020-07-12 00:40:13 +08:00
|
|
|
organization: null,
|
|
|
|
},
|
|
|
|
};
|
2022-10-09 21:18:11 +08:00
|
|
|
|
2020-07-12 00:40:13 +08:00
|
|
|
const data_org = {
|
|
|
|
data: {
|
|
|
|
user: null,
|
2021-09-26 23:32:27 +08:00
|
|
|
organization: { repository: data_repo.repository },
|
2020-07-12 00:40:13 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const mock = new MockAdapter(axios);
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
mock.reset();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("Test fetchRepo", () => {
|
|
|
|
it("should fetch correct user repo", async () => {
|
|
|
|
mock.onPost("https://api.github.com/graphql").reply(200, data_user);
|
|
|
|
|
|
|
|
let repo = await fetchRepo("anuraghazra", "convoychat");
|
2021-09-26 23:32:27 +08:00
|
|
|
|
|
|
|
expect(repo).toStrictEqual({
|
|
|
|
...data_repo.repository,
|
|
|
|
starCount: data_repo.repository.stargazers.totalCount,
|
|
|
|
});
|
2020-07-12 00:40:13 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should fetch correct org repo", async () => {
|
|
|
|
mock.onPost("https://api.github.com/graphql").reply(200, data_org);
|
|
|
|
|
|
|
|
let repo = await fetchRepo("anuraghazra", "convoychat");
|
2021-09-26 23:32:27 +08:00
|
|
|
expect(repo).toStrictEqual({
|
|
|
|
...data_repo.repository,
|
|
|
|
starCount: data_repo.repository.stargazers.totalCount,
|
|
|
|
});
|
2020-07-12 00:40:13 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should throw error if user is found but repo is null", async () => {
|
|
|
|
mock
|
|
|
|
.onPost("https://api.github.com/graphql")
|
|
|
|
.reply(200, { data: { user: { repository: null }, organization: null } });
|
|
|
|
|
|
|
|
await expect(fetchRepo("anuraghazra", "convoychat")).rejects.toThrow(
|
2020-09-25 00:08:14 +08:00
|
|
|
"User Repository Not found",
|
2020-07-12 00:40:13 +08:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should throw error if org is found but repo is null", async () => {
|
|
|
|
mock
|
|
|
|
.onPost("https://api.github.com/graphql")
|
|
|
|
.reply(200, { data: { user: null, organization: { repository: null } } });
|
|
|
|
|
|
|
|
await expect(fetchRepo("anuraghazra", "convoychat")).rejects.toThrow(
|
2020-09-25 00:08:14 +08:00
|
|
|
"Organization Repository Not found",
|
2020-07-12 00:40:13 +08:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should throw error if both user & org data not found", async () => {
|
|
|
|
mock
|
|
|
|
.onPost("https://api.github.com/graphql")
|
|
|
|
.reply(200, { data: { user: null, organization: null } });
|
|
|
|
|
|
|
|
await expect(fetchRepo("anuraghazra", "convoychat")).rejects.toThrow(
|
2020-09-25 00:08:14 +08:00
|
|
|
"Not found",
|
2020-07-12 00:40:13 +08:00
|
|
|
);
|
|
|
|
});
|
2020-07-17 15:09:08 +08:00
|
|
|
|
|
|
|
it("should throw error if repository is private", async () => {
|
|
|
|
mock.onPost("https://api.github.com/graphql").reply(200, {
|
|
|
|
data: {
|
|
|
|
user: { repository: { ...data_repo, isPrivate: true } },
|
|
|
|
organization: null,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
await expect(fetchRepo("anuraghazra", "convoychat")).rejects.toThrow(
|
2020-09-25 00:08:14 +08:00
|
|
|
"User Repository Not found",
|
2020-07-17 15:09:08 +08:00
|
|
|
);
|
|
|
|
});
|
2020-07-12 00:40:13 +08:00
|
|
|
});
|