2022-09-24 16:20:54 +08:00
|
|
|
|
import {
|
2020-07-16 18:21:21 +08:00
|
|
|
|
getByTestId,
|
|
|
|
|
queryAllByTestId,
|
2022-09-24 16:20:54 +08:00
|
|
|
|
queryByTestId,
|
|
|
|
|
} from "@testing-library/dom";
|
|
|
|
|
import { cssToObject } from "@uppercod/css-to-object";
|
2022-09-24 20:10:06 +08:00
|
|
|
|
import { renderStatsCard } from "../src/cards/stats-card.js";
|
2022-09-24 16:20:54 +08:00
|
|
|
|
// adds special assertions like toHaveTextContent
|
|
|
|
|
import "@testing-library/jest-dom";
|
|
|
|
|
|
2022-09-24 19:20:52 +08:00
|
|
|
|
import { themes } from "../themes/index.js";
|
2020-07-12 00:40:13 +08:00
|
|
|
|
|
|
|
|
|
describe("Test renderStatsCard", () => {
|
|
|
|
|
const stats = {
|
|
|
|
|
name: "Anurag Hazra",
|
|
|
|
|
totalStars: 100,
|
|
|
|
|
totalCommits: 200,
|
|
|
|
|
totalIssues: 300,
|
|
|
|
|
totalPRs: 400,
|
|
|
|
|
contributedTo: 500,
|
2020-07-14 22:34:36 +08:00
|
|
|
|
rank: { level: "A+", score: 40 },
|
2020-07-12 00:40:13 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
it("should render correctly", () => {
|
|
|
|
|
document.body.innerHTML = renderStatsCard(stats);
|
|
|
|
|
|
|
|
|
|
expect(document.getElementsByClassName("header")[0].textContent).toBe(
|
2020-09-25 00:08:14 +08:00
|
|
|
|
"Anurag Hazra's GitHub Stats",
|
2020-07-12 00:40:13 +08:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
expect(
|
2020-09-25 00:08:14 +08:00
|
|
|
|
document.body.getElementsByTagName("svg")[0].getAttribute("height"),
|
2020-07-12 00:40:13 +08:00
|
|
|
|
).toBe("195");
|
|
|
|
|
expect(getByTestId(document.body, "stars").textContent).toBe("100");
|
|
|
|
|
expect(getByTestId(document.body, "commits").textContent).toBe("200");
|
|
|
|
|
expect(getByTestId(document.body, "issues").textContent).toBe("300");
|
|
|
|
|
expect(getByTestId(document.body, "prs").textContent).toBe("400");
|
|
|
|
|
expect(getByTestId(document.body, "contribs").textContent).toBe("500");
|
2020-07-19 23:04:41 +08:00
|
|
|
|
expect(queryByTestId(document.body, "card-bg")).toBeInTheDocument();
|
2020-07-13 22:11:47 +08:00
|
|
|
|
expect(queryByTestId(document.body, "rank-circle")).toBeInTheDocument();
|
2020-07-12 00:40:13 +08:00
|
|
|
|
});
|
|
|
|
|
|
2020-07-20 02:32:07 +08:00
|
|
|
|
it("should have proper name apostrophe", () => {
|
|
|
|
|
document.body.innerHTML = renderStatsCard({ ...stats, name: "Anil Das" });
|
|
|
|
|
|
|
|
|
|
expect(document.getElementsByClassName("header")[0].textContent).toBe(
|
2020-09-25 00:08:14 +08:00
|
|
|
|
"Anil Das' GitHub Stats",
|
2020-07-20 02:32:07 +08:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
document.body.innerHTML = renderStatsCard({ ...stats, name: "Felix" });
|
|
|
|
|
|
|
|
|
|
expect(document.getElementsByClassName("header")[0].textContent).toBe(
|
2020-09-25 00:08:14 +08:00
|
|
|
|
"Felix' GitHub Stats",
|
2020-07-20 02:32:07 +08:00
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
2020-07-12 00:40:13 +08:00
|
|
|
|
it("should hide individual stats", () => {
|
|
|
|
|
document.body.innerHTML = renderStatsCard(stats, {
|
2020-07-23 23:31:23 +08:00
|
|
|
|
hide: ["issues", "prs", "contribs"],
|
2020-07-12 00:40:13 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(
|
2020-09-25 00:08:14 +08:00
|
|
|
|
document.body.getElementsByTagName("svg")[0].getAttribute("height"),
|
2020-07-13 22:11:47 +08:00
|
|
|
|
).toBe("150"); // height should be 150 because we clamped it.
|
|
|
|
|
|
2020-07-12 00:40:13 +08:00
|
|
|
|
expect(queryByTestId(document.body, "stars")).toBeDefined();
|
|
|
|
|
expect(queryByTestId(document.body, "commits")).toBeDefined();
|
|
|
|
|
expect(queryByTestId(document.body, "issues")).toBeNull();
|
|
|
|
|
expect(queryByTestId(document.body, "prs")).toBeNull();
|
|
|
|
|
expect(queryByTestId(document.body, "contribs")).toBeNull();
|
|
|
|
|
});
|
|
|
|
|
|
2020-07-13 22:11:47 +08:00
|
|
|
|
it("should hide_rank", () => {
|
|
|
|
|
document.body.innerHTML = renderStatsCard(stats, { hide_rank: true });
|
|
|
|
|
|
|
|
|
|
expect(queryByTestId(document.body, "rank-circle")).not.toBeInTheDocument();
|
|
|
|
|
});
|
|
|
|
|
|
2022-09-16 18:50:30 +08:00
|
|
|
|
it("should render with custom width set", () => {
|
|
|
|
|
document.body.innerHTML = renderStatsCard(stats);
|
|
|
|
|
expect(document.querySelector("svg")).toHaveAttribute("width", "495");
|
|
|
|
|
|
|
|
|
|
document.body.innerHTML = renderStatsCard(stats, { card_width: 400 });
|
|
|
|
|
expect(document.querySelector("svg")).toHaveAttribute("width", "400");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should render with custom width set and limit minimum width", () => {
|
|
|
|
|
document.body.innerHTML = renderStatsCard(stats, { card_width: 1 });
|
|
|
|
|
expect(document.querySelector("svg")).toHaveAttribute("width", "340");
|
|
|
|
|
|
|
|
|
|
document.body.innerHTML = renderStatsCard(stats, {
|
|
|
|
|
card_width: 1,
|
|
|
|
|
hide_rank: true,
|
|
|
|
|
});
|
|
|
|
|
expect(document.querySelector("svg")).toHaveAttribute(
|
|
|
|
|
"width",
|
|
|
|
|
"305.81250000000006",
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
document.body.innerHTML = renderStatsCard(stats, {
|
|
|
|
|
card_width: 1,
|
|
|
|
|
hide_rank: true,
|
|
|
|
|
show_icons: true,
|
|
|
|
|
});
|
|
|
|
|
expect(document.querySelector("svg")).toHaveAttribute(
|
|
|
|
|
"width",
|
|
|
|
|
"305.81250000000006",
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
document.body.innerHTML = renderStatsCard(stats, {
|
|
|
|
|
card_width: 1,
|
|
|
|
|
hide_rank: false,
|
|
|
|
|
show_icons: true,
|
|
|
|
|
});
|
|
|
|
|
expect(document.querySelector("svg")).toHaveAttribute("width", "356");
|
|
|
|
|
|
|
|
|
|
document.body.innerHTML = renderStatsCard(stats, {
|
|
|
|
|
card_width: 1,
|
|
|
|
|
hide_rank: false,
|
|
|
|
|
show_icons: false,
|
|
|
|
|
});
|
|
|
|
|
expect(document.querySelector("svg")).toHaveAttribute("width", "340");
|
|
|
|
|
});
|
|
|
|
|
|
2020-07-12 10:28:28 +08:00
|
|
|
|
it("should render default colors properly", () => {
|
|
|
|
|
document.body.innerHTML = renderStatsCard(stats);
|
|
|
|
|
|
|
|
|
|
const styleTag = document.querySelector("style");
|
2020-07-14 22:34:36 +08:00
|
|
|
|
const stylesObject = cssToObject(styleTag.textContent);
|
2020-07-12 10:28:28 +08:00
|
|
|
|
|
2021-12-13 22:42:03 +08:00
|
|
|
|
const headerClassStyles = stylesObject[":host"][".header "];
|
|
|
|
|
const statClassStyles = stylesObject[":host"][".stat "];
|
|
|
|
|
const iconClassStyles = stylesObject[":host"][".icon "];
|
2020-07-12 10:28:28 +08:00
|
|
|
|
|
2021-12-13 22:42:03 +08:00
|
|
|
|
expect(headerClassStyles.fill.trim()).toBe("#2f80ed");
|
2022-03-01 03:49:34 +08:00
|
|
|
|
expect(statClassStyles.fill.trim()).toBe("#434d58");
|
2021-12-13 22:42:03 +08:00
|
|
|
|
expect(iconClassStyles.fill.trim()).toBe("#4c71f2");
|
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
|
|
|
|
);
|
2020-07-12 10:28:28 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should render custom colors properly", () => {
|
|
|
|
|
const customColors = {
|
|
|
|
|
title_color: "5a0",
|
|
|
|
|
icon_color: "1b998b",
|
|
|
|
|
text_color: "9991",
|
2020-07-12 15:16:08 +08:00
|
|
|
|
bg_color: "252525",
|
2020-07-12 10:28:28 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
document.body.innerHTML = renderStatsCard(stats, { ...customColors });
|
|
|
|
|
|
|
|
|
|
const styleTag = document.querySelector("style");
|
|
|
|
|
const stylesObject = cssToObject(styleTag.innerHTML);
|
|
|
|
|
|
2021-12-13 22:42:03 +08:00
|
|
|
|
const headerClassStyles = stylesObject[":host"][".header "];
|
|
|
|
|
const statClassStyles = stylesObject[":host"][".stat "];
|
|
|
|
|
const iconClassStyles = stylesObject[":host"][".icon "];
|
2020-07-12 10:28:28 +08:00
|
|
|
|
|
2021-12-13 22:42:03 +08:00
|
|
|
|
expect(headerClassStyles.fill.trim()).toBe(`#${customColors.title_color}`);
|
|
|
|
|
expect(statClassStyles.fill.trim()).toBe(`#${customColors.text_color}`);
|
|
|
|
|
expect(iconClassStyles.fill.trim()).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-12 10:28:28 +08:00
|
|
|
|
});
|
2020-07-16 18:21:21 +08:00
|
|
|
|
|
2020-07-19 23:04:41 +08:00
|
|
|
|
it("should render custom colors with themes", () => {
|
|
|
|
|
document.body.innerHTML = renderStatsCard(stats, {
|
|
|
|
|
title_color: "5a0",
|
|
|
|
|
theme: "radical",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const styleTag = document.querySelector("style");
|
|
|
|
|
const stylesObject = cssToObject(styleTag.innerHTML);
|
|
|
|
|
|
2021-12-13 22:42:03 +08:00
|
|
|
|
const headerClassStyles = stylesObject[":host"][".header "];
|
|
|
|
|
const statClassStyles = stylesObject[":host"][".stat "];
|
|
|
|
|
const iconClassStyles = stylesObject[":host"][".icon "];
|
2020-07-19 23:04:41 +08:00
|
|
|
|
|
2021-12-13 22:42:03 +08:00
|
|
|
|
expect(headerClassStyles.fill.trim()).toBe("#5a0");
|
|
|
|
|
expect(statClassStyles.fill.trim()).toBe(`#${themes.radical.text_color}`);
|
|
|
|
|
expect(iconClassStyles.fill.trim()).toBe(`#${themes.radical.icon_color}`);
|
2020-07-19 23:04:41 +08:00
|
|
|
|
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-21 15:45:53 +08:00
|
|
|
|
it("should render with all the themes", () => {
|
|
|
|
|
Object.keys(themes).forEach((name) => {
|
|
|
|
|
document.body.innerHTML = renderStatsCard(stats, {
|
|
|
|
|
theme: name,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const styleTag = document.querySelector("style");
|
|
|
|
|
const stylesObject = cssToObject(styleTag.innerHTML);
|
|
|
|
|
|
2021-12-13 22:42:03 +08:00
|
|
|
|
const headerClassStyles = stylesObject[":host"][".header "];
|
|
|
|
|
const statClassStyles = stylesObject[":host"][".stat "];
|
|
|
|
|
const iconClassStyles = stylesObject[":host"][".icon "];
|
2020-07-21 15:45:53 +08:00
|
|
|
|
|
2021-12-13 22:42:03 +08:00
|
|
|
|
expect(headerClassStyles.fill.trim()).toBe(
|
|
|
|
|
`#${themes[name].title_color}`,
|
|
|
|
|
);
|
|
|
|
|
expect(statClassStyles.fill.trim()).toBe(`#${themes[name].text_color}`);
|
|
|
|
|
expect(iconClassStyles.fill.trim()).toBe(`#${themes[name].icon_color}`);
|
2020-07-21 15:45:53 +08:00
|
|
|
|
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 and fallback to default colors if invalid", () => {
|
|
|
|
|
document.body.innerHTML = renderStatsCard(stats, {
|
|
|
|
|
title_color: "invalid color",
|
|
|
|
|
text_color: "invalid color",
|
|
|
|
|
theme: "radical",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const styleTag = document.querySelector("style");
|
|
|
|
|
const stylesObject = cssToObject(styleTag.innerHTML);
|
|
|
|
|
|
2021-12-13 22:42:03 +08:00
|
|
|
|
const headerClassStyles = stylesObject[":host"][".header "];
|
|
|
|
|
const statClassStyles = stylesObject[":host"][".stat "];
|
|
|
|
|
const iconClassStyles = stylesObject[":host"][".icon "];
|
2020-07-19 23:04:41 +08:00
|
|
|
|
|
2021-12-13 22:42:03 +08:00
|
|
|
|
expect(headerClassStyles.fill.trim()).toBe(
|
|
|
|
|
`#${themes.default.title_color}`,
|
|
|
|
|
);
|
|
|
|
|
expect(statClassStyles.fill.trim()).toBe(`#${themes.default.text_color}`);
|
|
|
|
|
expect(iconClassStyles.fill.trim()).toBe(`#${themes.radical.icon_color}`);
|
2020-07-19 23:04:41 +08:00
|
|
|
|
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-16 18:21:21 +08:00
|
|
|
|
it("should render icons correctly", () => {
|
|
|
|
|
document.body.innerHTML = renderStatsCard(stats, {
|
2020-07-18 01:31:16 +08:00
|
|
|
|
show_icons: true,
|
2020-07-16 18:21:21 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(queryAllByTestId(document.body, "icon")[0]).toBeDefined();
|
|
|
|
|
expect(queryByTestId(document.body, "stars")).toBeDefined();
|
|
|
|
|
expect(
|
2020-09-25 00:08:14 +08:00
|
|
|
|
queryByTestId(document.body, "stars").previousElementSibling, // the label
|
2020-07-16 18:21:21 +08:00
|
|
|
|
).toHaveAttribute("x", "25");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should not have icons if show_icons is false", () => {
|
|
|
|
|
document.body.innerHTML = renderStatsCard(stats, { show_icons: false });
|
|
|
|
|
|
|
|
|
|
expect(queryAllByTestId(document.body, "icon")[0]).not.toBeDefined();
|
|
|
|
|
expect(queryByTestId(document.body, "stars")).toBeDefined();
|
|
|
|
|
expect(
|
2020-09-25 00:08:14 +08:00
|
|
|
|
queryByTestId(document.body, "stars").previousElementSibling, // the label
|
2020-07-16 18:21:21 +08:00
|
|
|
|
).not.toHaveAttribute("x");
|
|
|
|
|
});
|
2020-10-04 16:05:15 +08:00
|
|
|
|
|
2020-12-13 22:45:00 +08:00
|
|
|
|
it("should auto resize if hide_rank is true", () => {
|
|
|
|
|
document.body.innerHTML = renderStatsCard(stats, {
|
|
|
|
|
hide_rank: true,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(
|
|
|
|
|
document.body.getElementsByTagName("svg")[0].getAttribute("width"),
|
|
|
|
|
).toBe("305.81250000000006");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("should auto resize if hide_rank is true & custom_title is set", () => {
|
|
|
|
|
document.body.innerHTML = renderStatsCard(stats, {
|
|
|
|
|
hide_rank: true,
|
|
|
|
|
custom_title: "Hello world",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(
|
|
|
|
|
document.body.getElementsByTagName("svg")[0].getAttribute("width"),
|
|
|
|
|
).toBe("270");
|
|
|
|
|
});
|
|
|
|
|
|
2020-10-04 16:05:15 +08:00
|
|
|
|
it("should render translations", () => {
|
|
|
|
|
document.body.innerHTML = renderStatsCard(stats, { locale: "cn" });
|
|
|
|
|
expect(document.getElementsByClassName("header")[0].textContent).toBe(
|
2021-03-08 01:04:25 +08:00
|
|
|
|
"Anurag Hazra 的 GitHub 统计数据",
|
2020-10-04 16:05:15 +08:00
|
|
|
|
);
|
|
|
|
|
expect(
|
|
|
|
|
document.querySelector(
|
|
|
|
|
'g[transform="translate(0, 0)"]>.stagger>.stat.bold',
|
|
|
|
|
).textContent,
|
2021-03-08 01:04:25 +08:00
|
|
|
|
).toMatchInlineSnapshot(`"获标星数(star):"`);
|
2020-10-04 16:05:15 +08:00
|
|
|
|
expect(
|
|
|
|
|
document.querySelector(
|
|
|
|
|
'g[transform="translate(0, 25)"]>.stagger>.stat.bold',
|
|
|
|
|
).textContent,
|
2022-02-11 03:28:28 +08:00
|
|
|
|
).toMatchInlineSnapshot(`"累计提交数(commit) (2022):"`);
|
2020-10-04 16:05:15 +08:00
|
|
|
|
expect(
|
|
|
|
|
document.querySelector(
|
|
|
|
|
'g[transform="translate(0, 50)"]>.stagger>.stat.bold',
|
|
|
|
|
).textContent,
|
2021-03-08 01:04:25 +08:00
|
|
|
|
).toMatchInlineSnapshot(`"拉取请求数(PR):"`);
|
2020-10-04 16:05:15 +08:00
|
|
|
|
expect(
|
|
|
|
|
document.querySelector(
|
|
|
|
|
'g[transform="translate(0, 75)"]>.stagger>.stat.bold',
|
|
|
|
|
).textContent,
|
2021-03-08 01:04:25 +08:00
|
|
|
|
).toMatchInlineSnapshot(`"指出问题数(issue):"`);
|
2020-10-04 16:05:15 +08:00
|
|
|
|
expect(
|
|
|
|
|
document.querySelector(
|
|
|
|
|
'g[transform="translate(0, 100)"]>.stagger>.stat.bold',
|
|
|
|
|
).textContent,
|
2021-03-08 01:04:25 +08:00
|
|
|
|
).toMatchInlineSnapshot(`"参与项目数:"`);
|
2020-10-04 16:05:15 +08:00
|
|
|
|
});
|
2021-03-08 00:53:32 +08:00
|
|
|
|
|
|
|
|
|
it("should render without rounding", () => {
|
|
|
|
|
document.body.innerHTML = renderStatsCard(stats, { border_radius: "0" });
|
|
|
|
|
expect(document.querySelector("rect")).toHaveAttribute("rx", "0");
|
2021-03-08 01:04:25 +08:00
|
|
|
|
document.body.innerHTML = renderStatsCard(stats, {});
|
2021-03-08 00:53:32 +08:00
|
|
|
|
expect(document.querySelector("rect")).toHaveAttribute("rx", "4.5");
|
|
|
|
|
});
|
2020-07-12 00:40:13 +08:00
|
|
|
|
});
|