tests: added test for fetchTopLanguages
This commit is contained in:
parent
411d8f1ffa
commit
b50555ad9a
1 changed files with 84 additions and 0 deletions
84
tests/fetchTopLanguages.test.js
Normal file
84
tests/fetchTopLanguages.test.js
Normal file
|
@ -0,0 +1,84 @@
|
||||||
|
require("@testing-library/jest-dom");
|
||||||
|
const axios = require("axios");
|
||||||
|
const MockAdapter = require("axios-mock-adapter");
|
||||||
|
const fetchTopLanguages = require("../src/fetchTopLanguages");
|
||||||
|
|
||||||
|
const mock = new MockAdapter(axios);
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
mock.reset();
|
||||||
|
});
|
||||||
|
|
||||||
|
const data_langs = {
|
||||||
|
data: {
|
||||||
|
user: {
|
||||||
|
repositories: {
|
||||||
|
nodes: [
|
||||||
|
{
|
||||||
|
languages: {
|
||||||
|
edges: [{ size: 100, node: { color: "#0f0", name: "HTML" } }],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
languages: {
|
||||||
|
edges: [{ size: 100, node: { color: "#0f0", name: "HTML" } }],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
languages: {
|
||||||
|
edges: [
|
||||||
|
{ size: 100, node: { color: "#0ff", name: "javascript" } },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
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,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should throw error", 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'."
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
Loading…
Add table
Reference in a new issue