2020-07-12 00:40:13 +08:00
|
|
|
require("@testing-library/jest-dom");
|
2020-07-12 15:16:08 +08:00
|
|
|
const cssToObject = require("css-to-object");
|
2020-08-02 15:37:26 +08:00
|
|
|
const renderRepoCard = require("../src/cards/repo-card");
|
2020-07-12 00:40:13 +08:00
|
|
|
|
|
|
|
const { queryByTestId } = require("@testing-library/dom");
|
2020-07-19 23:04:41 +08:00
|
|
|
const themes = require("../themes");
|
2020-07-12 00:40:13 +08:00
|
|
|
|
|
|
|
const data_repo = {
|
|
|
|
repository: {
|
2020-07-16 20:33:33 +08:00
|
|
|
nameWithOwner: "anuraghazra/convoychat",
|
2020-07-12 00:40:13 +08:00
|
|
|
name: "convoychat",
|
|
|
|
description: "Help us take over the world! React + TS + GraphQL Chat App",
|
|
|
|
primaryLanguage: {
|
|
|
|
color: "#2b7489",
|
|
|
|
id: "MDg6TGFuZ3VhZ2UyODc=",
|
|
|
|
name: "TypeScript",
|
|
|
|
},
|
2021-09-26 23:32:27 +08:00
|
|
|
starCount: 38000,
|
2020-07-12 00:40:13 +08:00
|
|
|
forkCount: 100,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
describe("Test renderRepoCard", () => {
|
|
|
|
it("should render correctly", () => {
|
|
|
|
document.body.innerHTML = renderRepoCard(data_repo.repository);
|
|
|
|
|
2020-07-16 20:33:33 +08:00
|
|
|
const [header] = document.getElementsByClassName("header");
|
|
|
|
|
|
|
|
expect(header).toHaveTextContent("convoychat");
|
|
|
|
expect(header).not.toHaveTextContent("anuraghazra");
|
2020-07-12 00:40:13 +08:00
|
|
|
expect(document.getElementsByClassName("description")[0]).toHaveTextContent(
|
2020-09-25 00:08:14 +08:00
|
|
|
"Help us take over the world! React + TS + GraphQL Chat App",
|
2020-07-12 00:40:13 +08:00
|
|
|
);
|
|
|
|
expect(queryByTestId(document.body, "stargazers")).toHaveTextContent("38k");
|
|
|
|
expect(queryByTestId(document.body, "forkcount")).toHaveTextContent("100");
|
2020-07-23 16:11:37 +08:00
|
|
|
expect(queryByTestId(document.body, "lang-name")).toHaveTextContent(
|
2020-09-25 00:08:14 +08:00
|
|
|
"TypeScript",
|
2020-07-12 00:40:13 +08:00
|
|
|
);
|
|
|
|
expect(queryByTestId(document.body, "lang-color")).toHaveAttribute(
|
|
|
|
"fill",
|
2020-09-25 00:08:14 +08:00
|
|
|
"#2b7489",
|
2020-07-12 00:40:13 +08:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-07-16 20:33:33 +08:00
|
|
|
it("should display username in title (full repo name)", () => {
|
|
|
|
document.body.innerHTML = renderRepoCard(data_repo.repository, {
|
|
|
|
show_owner: true,
|
|
|
|
});
|
|
|
|
expect(document.getElementsByClassName("header")[0]).toHaveTextContent(
|
2020-09-25 00:08:14 +08:00
|
|
|
"anuraghazra/convoychat",
|
2020-07-16 20:33:33 +08:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-07-12 00:40:13 +08:00
|
|
|
it("should trim description", () => {
|
|
|
|
document.body.innerHTML = renderRepoCard({
|
|
|
|
...data_repo.repository,
|
|
|
|
description:
|
2020-07-27 16:39:35 +08:00
|
|
|
"The quick brown fox jumps over the lazy dog is an English-language pangram—a sentence that contains all of the letters of the English alphabet",
|
2020-07-12 00:40:13 +08:00
|
|
|
});
|
|
|
|
|
2020-07-27 16:39:35 +08:00
|
|
|
expect(
|
2020-09-25 00:08:14 +08:00
|
|
|
document.getElementsByClassName("description")[0].children[0].textContent,
|
2020-07-27 16:39:35 +08:00
|
|
|
).toBe("The quick brown fox jumps over the lazy dog is an");
|
|
|
|
|
|
|
|
expect(
|
2020-09-25 00:08:14 +08:00
|
|
|
document.getElementsByClassName("description")[0].children[1].textContent,
|
2020-07-27 16:39:35 +08:00
|
|
|
).toBe("English-language pangram—a sentence that contains all");
|
2020-07-12 00:40:13 +08:00
|
|
|
|
|
|
|
// Should not trim
|
|
|
|
document.body.innerHTML = renderRepoCard({
|
|
|
|
...data_repo.repository,
|
|
|
|
description: "Small text should not trim",
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(document.getElementsByClassName("description")[0]).toHaveTextContent(
|
2020-09-25 00:08:14 +08:00
|
|
|
"Small text should not trim",
|
2020-07-12 00:40:13 +08:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-07-24 21:26:26 +08:00
|
|
|
it("should render emojis", () => {
|
|
|
|
document.body.innerHTML = renderRepoCard({
|
|
|
|
...data_repo.repository,
|
|
|
|
description: "This is a text with a :poop: poo emoji",
|
|
|
|
});
|
|
|
|
|
|
|
|
// poop emoji may not show in all editors but it's there between "a" and "poo"
|
|
|
|
expect(document.getElementsByClassName("description")[0]).toHaveTextContent(
|
2020-09-25 00:08:14 +08:00
|
|
|
"This is a text with a 💩 poo emoji",
|
2020-07-24 21:26:26 +08:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-07-23 16:11:37 +08:00
|
|
|
it("should hide language if primaryLanguage is null & fallback to correct values", () => {
|
|
|
|
document.body.innerHTML = renderRepoCard({
|
|
|
|
...data_repo.repository,
|
|
|
|
primaryLanguage: null,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(queryByTestId(document.body, "primary-lang")).toBeNull();
|
|
|
|
|
|
|
|
document.body.innerHTML = renderRepoCard({
|
|
|
|
...data_repo.repository,
|
|
|
|
primaryLanguage: { color: null, name: null },
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(queryByTestId(document.body, "primary-lang")).toBeInTheDocument();
|
|
|
|
expect(queryByTestId(document.body, "lang-color")).toHaveAttribute(
|
|
|
|
"fill",
|
2020-09-25 00:08:14 +08:00
|
|
|
"#333",
|
2020-07-23 16:11:37 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
expect(queryByTestId(document.body, "lang-name")).toHaveTextContent(
|
2020-09-25 00:08:14 +08:00
|
|
|
"Unspecified",
|
2020-07-23 16:11:37 +08:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-07-12 15:16:08 +08:00
|
|
|
it("should render default colors properly", () => {
|
|
|
|
document.body.innerHTML = renderRepoCard(data_repo.repository);
|
|
|
|
|
|
|
|
const styleTag = document.querySelector("style");
|
|
|
|
const stylesObject = cssToObject(styleTag.innerHTML);
|
|
|
|
|
|
|
|
const headerClassStyles = stylesObject[".header"];
|
2020-07-19 23:04:41 +08:00
|
|
|
const descClassStyles = stylesObject[".description"];
|
2020-07-12 15:16:08 +08:00
|
|
|
const iconClassStyles = stylesObject[".icon"];
|
|
|
|
|
|
|
|
expect(headerClassStyles.fill).toBe("#2f80ed");
|
2020-07-19 23:04:41 +08:00
|
|
|
expect(descClassStyles.fill).toBe("#333");
|
2020-07-12 15:16:08 +08:00
|
|
|
expect(iconClassStyles.fill).toBe("#586069");
|
2020-07-19 23:04:41 +08:00
|
|
|
expect(queryByTestId(document.body, "card-bg")).toHaveAttribute(
|
2020-07-12 15:16:08 +08:00
|
|
|
"fill",
|
2020-09-25 00:08:14 +08:00
|
|
|
"#fffefe",
|
2020-07-12 15:16:08 +08:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should render custom colors properly", () => {
|
|
|
|
const customColors = {
|
|
|
|
title_color: "5a0",
|
|
|
|
icon_color: "1b998b",
|
|
|
|
text_color: "9991",
|
|
|
|
bg_color: "252525",
|
|
|
|
};
|
|
|
|
|
|
|
|
document.body.innerHTML = renderRepoCard(data_repo.repository, {
|
|
|
|
...customColors,
|
|
|
|
});
|
|
|
|
|
|
|
|
const styleTag = document.querySelector("style");
|
|
|
|
const stylesObject = cssToObject(styleTag.innerHTML);
|
|
|
|
|
|
|
|
const headerClassStyles = stylesObject[".header"];
|
2020-07-19 23:04:41 +08:00
|
|
|
const descClassStyles = stylesObject[".description"];
|
2020-07-12 15:16:08 +08:00
|
|
|
const iconClassStyles = stylesObject[".icon"];
|
|
|
|
|
|
|
|
expect(headerClassStyles.fill).toBe(`#${customColors.title_color}`);
|
2020-07-19 23:04:41 +08:00
|
|
|
expect(descClassStyles.fill).toBe(`#${customColors.text_color}`);
|
2020-07-12 15:16:08 +08:00
|
|
|
expect(iconClassStyles.fill).toBe(`#${customColors.icon_color}`);
|
2020-07-19 23:04:41 +08:00
|
|
|
expect(queryByTestId(document.body, "card-bg")).toHaveAttribute(
|
2020-07-12 15:16:08 +08:00
|
|
|
"fill",
|
2020-09-25 00:08:14 +08:00
|
|
|
"#252525",
|
2020-07-12 15:16:08 +08:00
|
|
|
);
|
|
|
|
});
|
2020-07-18 22:50:36 +08:00
|
|
|
|
2020-07-21 15:45:53 +08:00
|
|
|
it("should render with all the themes", () => {
|
|
|
|
Object.keys(themes).forEach((name) => {
|
|
|
|
document.body.innerHTML = renderRepoCard(data_repo.repository, {
|
|
|
|
theme: name,
|
|
|
|
});
|
|
|
|
|
|
|
|
const styleTag = document.querySelector("style");
|
|
|
|
const stylesObject = cssToObject(styleTag.innerHTML);
|
|
|
|
|
|
|
|
const headerClassStyles = stylesObject[".header"];
|
|
|
|
const descClassStyles = stylesObject[".description"];
|
|
|
|
const iconClassStyles = stylesObject[".icon"];
|
|
|
|
|
|
|
|
expect(headerClassStyles.fill).toBe(`#${themes[name].title_color}`);
|
|
|
|
expect(descClassStyles.fill).toBe(`#${themes[name].text_color}`);
|
|
|
|
expect(iconClassStyles.fill).toBe(`#${themes[name].icon_color}`);
|
|
|
|
expect(queryByTestId(document.body, "card-bg")).toHaveAttribute(
|
|
|
|
"fill",
|
2020-09-25 00:08:14 +08:00
|
|
|
`#${themes[name].bg_color}`,
|
2020-07-21 15:45:53 +08:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-07-19 23:04:41 +08:00
|
|
|
it("should render custom colors with themes", () => {
|
|
|
|
document.body.innerHTML = renderRepoCard(data_repo.repository, {
|
|
|
|
title_color: "5a0",
|
|
|
|
theme: "radical",
|
|
|
|
});
|
|
|
|
|
|
|
|
const styleTag = document.querySelector("style");
|
|
|
|
const stylesObject = cssToObject(styleTag.innerHTML);
|
|
|
|
|
|
|
|
const headerClassStyles = stylesObject[".header"];
|
|
|
|
const descClassStyles = stylesObject[".description"];
|
|
|
|
const iconClassStyles = stylesObject[".icon"];
|
|
|
|
|
|
|
|
expect(headerClassStyles.fill).toBe("#5a0");
|
|
|
|
expect(descClassStyles.fill).toBe(`#${themes.radical.text_color}`);
|
|
|
|
expect(iconClassStyles.fill).toBe(`#${themes.radical.icon_color}`);
|
|
|
|
expect(queryByTestId(document.body, "card-bg")).toHaveAttribute(
|
|
|
|
"fill",
|
2020-09-25 00:08:14 +08:00
|
|
|
`#${themes.radical.bg_color}`,
|
2020-07-19 23:04:41 +08:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should render custom colors with themes and fallback to default colors if invalid", () => {
|
|
|
|
document.body.innerHTML = renderRepoCard(data_repo.repository, {
|
|
|
|
title_color: "invalid color",
|
|
|
|
text_color: "invalid color",
|
|
|
|
theme: "radical",
|
|
|
|
});
|
|
|
|
|
|
|
|
const styleTag = document.querySelector("style");
|
|
|
|
const stylesObject = cssToObject(styleTag.innerHTML);
|
|
|
|
|
|
|
|
const headerClassStyles = stylesObject[".header"];
|
|
|
|
const descClassStyles = stylesObject[".description"];
|
|
|
|
const iconClassStyles = stylesObject[".icon"];
|
|
|
|
|
|
|
|
expect(headerClassStyles.fill).toBe(`#${themes.default.title_color}`);
|
|
|
|
expect(descClassStyles.fill).toBe(`#${themes.default.text_color}`);
|
|
|
|
expect(iconClassStyles.fill).toBe(`#${themes.radical.icon_color}`);
|
|
|
|
expect(queryByTestId(document.body, "card-bg")).toHaveAttribute(
|
|
|
|
"fill",
|
2020-09-25 00:08:14 +08:00
|
|
|
`#${themes.radical.bg_color}`,
|
2020-07-19 23:04:41 +08:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-07-19 01:14:27 +08:00
|
|
|
it("should not render star count or fork count if either of the are zero", () => {
|
|
|
|
document.body.innerHTML = renderRepoCard({
|
|
|
|
...data_repo.repository,
|
2021-09-26 23:32:27 +08:00
|
|
|
starCount: 0,
|
2020-07-19 01:14:27 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(queryByTestId(document.body, "stargazers")).toBeNull();
|
2020-07-23 15:35:50 +08:00
|
|
|
expect(queryByTestId(document.body, "forkcount")).toBeInTheDocument();
|
2020-07-19 01:14:27 +08:00
|
|
|
|
|
|
|
document.body.innerHTML = renderRepoCard({
|
|
|
|
...data_repo.repository,
|
2021-09-26 23:32:27 +08:00
|
|
|
starCount: 1,
|
2020-07-19 01:14:27 +08:00
|
|
|
forkCount: 0,
|
|
|
|
});
|
|
|
|
|
2020-07-23 15:35:50 +08:00
|
|
|
expect(queryByTestId(document.body, "stargazers")).toBeInTheDocument();
|
2020-07-19 01:14:27 +08:00
|
|
|
expect(queryByTestId(document.body, "forkcount")).toBeNull();
|
2020-07-19 23:04:41 +08:00
|
|
|
|
2020-07-19 01:14:27 +08:00
|
|
|
document.body.innerHTML = renderRepoCard({
|
|
|
|
...data_repo.repository,
|
2021-09-26 23:32:27 +08:00
|
|
|
starCount: 0,
|
2020-07-19 01:14:27 +08:00
|
|
|
forkCount: 0,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(queryByTestId(document.body, "stargazers")).toBeNull();
|
|
|
|
expect(queryByTestId(document.body, "forkcount")).toBeNull();
|
|
|
|
});
|
2020-07-23 15:35:50 +08:00
|
|
|
|
|
|
|
it("should render badges", () => {
|
|
|
|
document.body.innerHTML = renderRepoCard({
|
|
|
|
...data_repo.repository,
|
|
|
|
isArchived: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(queryByTestId(document.body, "badge")).toHaveTextContent("Archived");
|
|
|
|
|
|
|
|
document.body.innerHTML = renderRepoCard({
|
|
|
|
...data_repo.repository,
|
|
|
|
isTemplate: true,
|
|
|
|
});
|
|
|
|
expect(queryByTestId(document.body, "badge")).toHaveTextContent("Template");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not render template", () => {
|
|
|
|
document.body.innerHTML = renderRepoCard({
|
|
|
|
...data_repo.repository,
|
|
|
|
});
|
|
|
|
expect(queryByTestId(document.body, "badge")).toBeNull();
|
|
|
|
});
|
2020-10-04 16:05:15 +08:00
|
|
|
|
|
|
|
it("should render translated badges", () => {
|
|
|
|
document.body.innerHTML = renderRepoCard(
|
|
|
|
{
|
|
|
|
...data_repo.repository,
|
|
|
|
isArchived: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
locale: "cn",
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2020-10-04 22:47:13 +08:00
|
|
|
expect(queryByTestId(document.body, "badge")).toHaveTextContent("已归档");
|
2020-10-04 16:05:15 +08:00
|
|
|
|
|
|
|
document.body.innerHTML = renderRepoCard(
|
|
|
|
{
|
|
|
|
...data_repo.repository,
|
|
|
|
isTemplate: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
locale: "cn",
|
|
|
|
},
|
|
|
|
);
|
|
|
|
expect(queryByTestId(document.body, "badge")).toHaveTextContent("模板");
|
|
|
|
});
|
2021-09-19 01:09:28 +08:00
|
|
|
|
2021-03-08 00:53:32 +08:00
|
|
|
it("should render without rounding", () => {
|
2021-09-19 01:09:28 +08:00
|
|
|
document.body.innerHTML = renderRepoCard(data_repo.repository, {
|
|
|
|
border_radius: "0",
|
|
|
|
});
|
2021-03-08 00:53:32 +08:00
|
|
|
expect(document.querySelector("rect")).toHaveAttribute("rx", "0");
|
2021-09-19 01:09:28 +08:00
|
|
|
document.body.innerHTML = renderRepoCard(data_repo.repository, {});
|
2021-03-08 00:53:32 +08:00
|
|
|
expect(document.querySelector("rect")).toHaveAttribute("rx", "4.5");
|
|
|
|
});
|
2021-09-26 23:32:27 +08:00
|
|
|
|
|
|
|
it("should fallback to default description", () => {
|
|
|
|
document.body.innerHTML = renderRepoCard({
|
|
|
|
...data_repo.repository,
|
|
|
|
description: undefined,
|
|
|
|
isArchived: true,
|
|
|
|
});
|
|
|
|
expect(document.getElementsByClassName("description")[0]).toHaveTextContent(
|
|
|
|
"No description provided",
|
|
|
|
);
|
|
|
|
});
|
2020-07-12 00:40:13 +08:00
|
|
|
});
|