2022-09-24 16:20:54 +08:00
|
|
|
import { jest } from "@jest/globals";
|
|
|
|
import "@testing-library/jest-dom";
|
|
|
|
import axios from "axios";
|
|
|
|
import MockAdapter from "axios-mock-adapter";
|
2022-09-24 19:20:52 +08:00
|
|
|
import topLangs from "../api/top-langs.js";
|
|
|
|
import { renderTopLanguages } from "../src/cards/top-languages-card.js";
|
|
|
|
import { renderError } from "../src/common/utils.js";
|
2023-07-03 14:54:19 +08:00
|
|
|
import { expect, it, describe, afterEach } from "@jest/globals";
|
2020-07-21 20:34:58 +08:00
|
|
|
|
|
|
|
const data_langs = {
|
|
|
|
data: {
|
|
|
|
user: {
|
|
|
|
repositories: {
|
|
|
|
nodes: [
|
|
|
|
{
|
|
|
|
languages: {
|
2020-07-29 02:03:43 +08:00
|
|
|
edges: [{ size: 150, node: { color: "#0f0", name: "HTML" } }],
|
2020-07-21 20:34:58 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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 fetch user",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
const langs = {
|
|
|
|
HTML: {
|
|
|
|
color: "#0f0",
|
|
|
|
name: "HTML",
|
2020-07-29 02:03:43 +08:00
|
|
|
size: 250,
|
2020-07-21 20:34:58 +08:00
|
|
|
},
|
|
|
|
javascript: {
|
|
|
|
color: "#0ff",
|
|
|
|
name: "javascript",
|
|
|
|
size: 200,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const mock = new MockAdapter(axios);
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
mock.reset();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("Test /api/top-langs", () => {
|
|
|
|
it("should test the request", async () => {
|
|
|
|
const req = {
|
|
|
|
query: {
|
|
|
|
username: "anuraghazra",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
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(renderTopLanguages(langs));
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should work with the query options", async () => {
|
|
|
|
const req = {
|
|
|
|
query: {
|
|
|
|
username: "anuraghazra",
|
|
|
|
hide_title: true,
|
|
|
|
card_width: 100,
|
|
|
|
title_color: "fff",
|
|
|
|
icon_color: "fff",
|
|
|
|
text_color: "fff",
|
|
|
|
bg_color: "fff",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
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(
|
|
|
|
renderTopLanguages(langs, {
|
|
|
|
hide_title: true,
|
|
|
|
card_width: 100,
|
|
|
|
title_color: "fff",
|
|
|
|
icon_color: "fff",
|
|
|
|
text_color: "fff",
|
|
|
|
bg_color: "fff",
|
2020-09-25 00:08:14 +08:00
|
|
|
}),
|
2020-07-21 20:34:58 +08:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2023-06-06 14:56:23 +08:00
|
|
|
it("should render error card on user data fetch error", async () => {
|
2020-07-21 20:34:58 +08:00
|
|
|
const req = {
|
|
|
|
query: {
|
|
|
|
username: "anuraghazra",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
const res = {
|
|
|
|
setHeader: jest.fn(),
|
|
|
|
send: jest.fn(),
|
|
|
|
};
|
|
|
|
mock.onPost("https://api.github.com/graphql").reply(200, error);
|
|
|
|
|
|
|
|
await topLangs(req, res);
|
|
|
|
|
|
|
|
expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml");
|
2023-08-22 15:00:56 +08:00
|
|
|
expect(res.send).toBeCalledWith(
|
|
|
|
renderError(
|
|
|
|
error.errors[0].message,
|
|
|
|
"Make sure the provided username is not an organization",
|
|
|
|
),
|
|
|
|
);
|
2020-07-21 20:34:58 +08:00
|
|
|
});
|
2023-06-06 14:56:23 +08:00
|
|
|
|
|
|
|
it("should render error card on incorrect layout input", async () => {
|
|
|
|
const req = {
|
|
|
|
query: {
|
|
|
|
username: "anuraghazra",
|
|
|
|
layout: ["pie"],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
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", "Incorrect layout input"),
|
|
|
|
);
|
|
|
|
});
|
2023-08-24 13:39:38 +08:00
|
|
|
|
|
|
|
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"));
|
|
|
|
});
|
2023-08-25 15:20:31 +08:00
|
|
|
|
|
|
|
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"),
|
|
|
|
);
|
|
|
|
});
|
2020-07-21 20:34:58 +08:00
|
|
|
});
|