2020-07-12 22:38:01 +08:00
|
|
|
require("@testing-library/jest-dom");
|
|
|
|
const axios = require("axios");
|
|
|
|
const MockAdapter = require("axios-mock-adapter");
|
|
|
|
const pin = require("../api/pin");
|
2020-08-02 15:37:26 +08:00
|
|
|
const renderRepoCard = require("../src/cards/repo-card");
|
|
|
|
const { renderError } = require("../src/common/utils");
|
2020-07-12 22:38:01 +08:00
|
|
|
|
|
|
|
const data_repo = {
|
|
|
|
repository: {
|
2020-07-16 20:33:33 +08:00
|
|
|
username: "anuraghazra",
|
2020-07-12 22:38:01 +08:00
|
|
|
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,
|
2020-08-02 15:37:26 +08:00
|
|
|
isTemplate: false,
|
2020-07-12 22:38:01 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const data_user = {
|
|
|
|
data: {
|
|
|
|
user: { repository: data_repo.repository },
|
|
|
|
organization: null,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const mock = new MockAdapter(axios);
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
mock.reset();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("Test /api/pin", () => {
|
|
|
|
it("should test the request", async () => {
|
|
|
|
const req = {
|
|
|
|
query: {
|
|
|
|
username: "anuraghazra",
|
|
|
|
repo: "convoychat",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
const res = {
|
|
|
|
setHeader: jest.fn(),
|
|
|
|
send: jest.fn(),
|
|
|
|
};
|
|
|
|
mock.onPost("https://api.github.com/graphql").reply(200, data_user);
|
|
|
|
|
|
|
|
await pin(req, res);
|
|
|
|
|
|
|
|
expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml");
|
|
|
|
expect(res.send).toBeCalledWith(renderRepoCard(data_repo.repository));
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should get the query options", async () => {
|
|
|
|
const req = {
|
|
|
|
query: {
|
|
|
|
username: "anuraghazra",
|
|
|
|
repo: "convoychat",
|
|
|
|
title_color: "fff",
|
|
|
|
icon_color: "fff",
|
|
|
|
text_color: "fff",
|
|
|
|
bg_color: "fff",
|
2020-07-16 20:33:33 +08:00
|
|
|
full_name: "1",
|
2020-07-12 22:38:01 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
const res = {
|
|
|
|
setHeader: jest.fn(),
|
|
|
|
send: jest.fn(),
|
|
|
|
};
|
|
|
|
mock.onPost("https://api.github.com/graphql").reply(200, data_user);
|
|
|
|
|
|
|
|
await pin(req, res);
|
|
|
|
|
|
|
|
expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml");
|
|
|
|
expect(res.send).toBeCalledWith(
|
2020-09-25 00:08:14 +08:00
|
|
|
renderRepoCard(data_repo.repository, { ...req.query }),
|
2020-07-12 22:38:01 +08:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should render error card if user repo not found", async () => {
|
|
|
|
const req = {
|
|
|
|
query: {
|
|
|
|
username: "anuraghazra",
|
|
|
|
repo: "convoychat",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
const res = {
|
|
|
|
setHeader: jest.fn(),
|
|
|
|
send: jest.fn(),
|
|
|
|
};
|
|
|
|
mock
|
|
|
|
.onPost("https://api.github.com/graphql")
|
|
|
|
.reply(200, { data: { user: { repository: null }, organization: null } });
|
|
|
|
|
|
|
|
await pin(req, res);
|
|
|
|
|
|
|
|
expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml");
|
|
|
|
expect(res.send).toBeCalledWith(renderError("User Repository Not found"));
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should render error card if org repo not found", async () => {
|
|
|
|
const req = {
|
|
|
|
query: {
|
|
|
|
username: "anuraghazra",
|
|
|
|
repo: "convoychat",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
const res = {
|
|
|
|
setHeader: jest.fn(),
|
|
|
|
send: jest.fn(),
|
|
|
|
};
|
|
|
|
mock
|
|
|
|
.onPost("https://api.github.com/graphql")
|
|
|
|
.reply(200, { data: { user: null, organization: { repository: null } } });
|
|
|
|
|
|
|
|
await pin(req, res);
|
|
|
|
|
|
|
|
expect(res.setHeader).toBeCalledWith("Content-Type", "image/svg+xml");
|
|
|
|
expect(res.send).toBeCalledWith(
|
2020-09-25 00:08:14 +08:00
|
|
|
renderError("Organization Repository Not found"),
|
2020-07-12 22:38:01 +08:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|