2022-09-24 16:20:54 +08:00
|
|
|
import { queryAllByTestId, queryByTestId } from "@testing-library/dom";
|
|
|
|
import { cssToObject } from "@uppercod/css-to-object";
|
|
|
|
import {
|
|
|
|
MIN_CARD_WIDTH,
|
|
|
|
renderTopLanguages,
|
2022-09-24 19:20:52 +08:00
|
|
|
} from "../src/cards/top-languages-card.js";
|
2022-09-24 16:20:54 +08:00
|
|
|
// adds special assertions like toHaveTextContent
|
|
|
|
import "@testing-library/jest-dom";
|
2020-07-21 20:34:58 +08:00
|
|
|
|
2022-09-24 19:20:52 +08:00
|
|
|
import { themes } from "../themes/index.js";
|
2020-07-21 20:34:58 +08:00
|
|
|
|
2022-10-09 21:18:11 +08:00
|
|
|
const langs = {
|
|
|
|
HTML: {
|
|
|
|
color: "#0f0",
|
|
|
|
name: "HTML",
|
|
|
|
size: 200,
|
|
|
|
},
|
|
|
|
javascript: {
|
|
|
|
color: "#0ff",
|
|
|
|
name: "javascript",
|
|
|
|
size: 200,
|
|
|
|
},
|
|
|
|
css: {
|
|
|
|
color: "#ff0",
|
|
|
|
name: "css",
|
|
|
|
size: 100,
|
|
|
|
},
|
|
|
|
};
|
2020-07-21 20:34:58 +08:00
|
|
|
|
2022-10-09 21:18:11 +08:00
|
|
|
describe("Test renderTopLanguages", () => {
|
2020-07-21 20:34:58 +08:00
|
|
|
it("should render correctly", () => {
|
|
|
|
document.body.innerHTML = renderTopLanguages(langs);
|
|
|
|
|
|
|
|
expect(queryByTestId(document.body, "header")).toHaveTextContent(
|
2020-09-25 00:08:14 +08:00
|
|
|
"Most Used Languages",
|
2020-07-21 20:34:58 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
expect(queryAllByTestId(document.body, "lang-name")[0]).toHaveTextContent(
|
2020-09-25 00:08:14 +08:00
|
|
|
"HTML",
|
2020-07-21 20:34:58 +08:00
|
|
|
);
|
|
|
|
expect(queryAllByTestId(document.body, "lang-name")[1]).toHaveTextContent(
|
2020-09-25 00:08:14 +08:00
|
|
|
"javascript",
|
2020-07-21 20:34:58 +08:00
|
|
|
);
|
|
|
|
expect(queryAllByTestId(document.body, "lang-name")[2]).toHaveTextContent(
|
2020-09-25 00:08:14 +08:00
|
|
|
"css",
|
2020-07-21 20:34:58 +08:00
|
|
|
);
|
|
|
|
expect(queryAllByTestId(document.body, "lang-progress")[0]).toHaveAttribute(
|
|
|
|
"width",
|
2020-09-25 00:08:14 +08:00
|
|
|
"40%",
|
2020-07-21 20:34:58 +08:00
|
|
|
);
|
|
|
|
expect(queryAllByTestId(document.body, "lang-progress")[1]).toHaveAttribute(
|
|
|
|
"width",
|
2020-09-25 00:08:14 +08:00
|
|
|
"40%",
|
2020-07-21 20:34:58 +08:00
|
|
|
);
|
|
|
|
expect(queryAllByTestId(document.body, "lang-progress")[2]).toHaveAttribute(
|
|
|
|
"width",
|
2020-09-25 00:08:14 +08:00
|
|
|
"20%",
|
2020-07-21 20:34:58 +08:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-07-23 22:17:04 +08:00
|
|
|
it("should hide languages when hide is passed", () => {
|
2020-07-21 21:07:16 +08:00
|
|
|
document.body.innerHTML = renderTopLanguages(langs, {
|
2020-07-23 22:17:04 +08:00
|
|
|
hide: ["HTML"],
|
2020-07-21 21:07:16 +08:00
|
|
|
});
|
|
|
|
expect(queryAllByTestId(document.body, "lang-name")[0]).toBeInTheDocument(
|
2020-09-25 00:08:14 +08:00
|
|
|
"javascript",
|
2020-07-21 21:07:16 +08:00
|
|
|
);
|
|
|
|
expect(queryAllByTestId(document.body, "lang-name")[1]).toBeInTheDocument(
|
2020-09-25 00:08:14 +08:00
|
|
|
"css",
|
2020-07-21 21:07:16 +08:00
|
|
|
);
|
|
|
|
expect(queryAllByTestId(document.body, "lang-name")[2]).not.toBeDefined();
|
2020-07-23 22:17:04 +08:00
|
|
|
|
|
|
|
// multiple languages passed
|
|
|
|
document.body.innerHTML = renderTopLanguages(langs, {
|
2020-08-02 15:37:26 +08:00
|
|
|
hide: ["HTML", "css"],
|
2020-07-23 22:17:04 +08:00
|
|
|
});
|
|
|
|
expect(queryAllByTestId(document.body, "lang-name")[0]).toBeInTheDocument(
|
2020-09-25 00:08:14 +08:00
|
|
|
"javascript",
|
2020-07-23 22:17:04 +08:00
|
|
|
);
|
|
|
|
expect(queryAllByTestId(document.body, "lang-name")[1]).not.toBeDefined();
|
2020-07-21 21:07:16 +08:00
|
|
|
});
|
|
|
|
|
2020-07-21 20:34:58 +08:00
|
|
|
it("should resize the height correctly depending on langs", () => {
|
|
|
|
document.body.innerHTML = renderTopLanguages(langs, {});
|
|
|
|
expect(document.querySelector("svg")).toHaveAttribute("height", "205");
|
|
|
|
|
|
|
|
document.body.innerHTML = renderTopLanguages(
|
|
|
|
{
|
|
|
|
...langs,
|
|
|
|
python: {
|
|
|
|
color: "#ff0",
|
|
|
|
name: "python",
|
|
|
|
size: 100,
|
|
|
|
},
|
|
|
|
},
|
2020-09-25 00:08:14 +08:00
|
|
|
{},
|
2020-07-21 20:34:58 +08:00
|
|
|
);
|
|
|
|
expect(document.querySelector("svg")).toHaveAttribute("height", "245");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should render with custom width set", () => {
|
|
|
|
document.body.innerHTML = renderTopLanguages(langs, {});
|
|
|
|
|
|
|
|
expect(document.querySelector("svg")).toHaveAttribute("width", "300");
|
|
|
|
|
|
|
|
document.body.innerHTML = renderTopLanguages(langs, { card_width: 400 });
|
|
|
|
expect(document.querySelector("svg")).toHaveAttribute("width", "400");
|
|
|
|
});
|
|
|
|
|
2022-08-18 16:32:03 +08:00
|
|
|
it("should render with min width", () => {
|
|
|
|
document.body.innerHTML = renderTopLanguages(langs, { card_width: 190 });
|
|
|
|
|
|
|
|
expect(document.querySelector("svg")).toHaveAttribute(
|
|
|
|
"width",
|
2022-09-24 16:20:54 +08:00
|
|
|
MIN_CARD_WIDTH.toString(),
|
2022-08-18 16:32:03 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
document.body.innerHTML = renderTopLanguages(langs, { card_width: 100 });
|
|
|
|
expect(document.querySelector("svg")).toHaveAttribute(
|
|
|
|
"width",
|
2022-09-24 16:20:54 +08:00
|
|
|
MIN_CARD_WIDTH.toString(),
|
2022-08-18 16:32:03 +08:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-07-21 20:34:58 +08:00
|
|
|
it("should render default colors properly", () => {
|
|
|
|
document.body.innerHTML = renderTopLanguages(langs);
|
|
|
|
|
|
|
|
const styleTag = document.querySelector("style");
|
|
|
|
const stylesObject = cssToObject(styleTag.textContent);
|
|
|
|
|
2021-12-13 22:42:03 +08:00
|
|
|
const headerStyles = stylesObject[":host"][".header "];
|
|
|
|
const langNameStyles = stylesObject[":host"][".lang-name "];
|
2020-07-21 20:34:58 +08:00
|
|
|
|
2021-12-13 22:42:03 +08:00
|
|
|
expect(headerStyles.fill.trim()).toBe("#2f80ed");
|
2022-03-01 03:49:34 +08:00
|
|
|
expect(langNameStyles.fill.trim()).toBe("#434d58");
|
2020-07-21 20:34:58 +08:00
|
|
|
expect(queryByTestId(document.body, "card-bg")).toHaveAttribute(
|
|
|
|
"fill",
|
2020-09-25 00:08:14 +08:00
|
|
|
"#fffefe",
|
2020-07-21 20:34:58 +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 = renderTopLanguages(langs, { ...customColors });
|
|
|
|
|
|
|
|
const styleTag = document.querySelector("style");
|
|
|
|
const stylesObject = cssToObject(styleTag.innerHTML);
|
|
|
|
|
2021-12-13 22:42:03 +08:00
|
|
|
const headerStyles = stylesObject[":host"][".header "];
|
|
|
|
const langNameStyles = stylesObject[":host"][".lang-name "];
|
2020-07-21 20:34:58 +08:00
|
|
|
|
2021-12-13 22:42:03 +08:00
|
|
|
expect(headerStyles.fill.trim()).toBe(`#${customColors.title_color}`);
|
|
|
|
expect(langNameStyles.fill.trim()).toBe(`#${customColors.text_color}`);
|
2020-07-21 20:34:58 +08:00
|
|
|
expect(queryByTestId(document.body, "card-bg")).toHaveAttribute(
|
|
|
|
"fill",
|
2020-09-25 00:08:14 +08:00
|
|
|
"#252525",
|
2020-07-21 20:34:58 +08:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should render custom colors with themes", () => {
|
|
|
|
document.body.innerHTML = renderTopLanguages(langs, {
|
|
|
|
title_color: "5a0",
|
|
|
|
theme: "radical",
|
|
|
|
});
|
|
|
|
|
|
|
|
const styleTag = document.querySelector("style");
|
|
|
|
const stylesObject = cssToObject(styleTag.innerHTML);
|
|
|
|
|
2021-12-13 22:42:03 +08:00
|
|
|
const headerStyles = stylesObject[":host"][".header "];
|
|
|
|
const langNameStyles = stylesObject[":host"][".lang-name "];
|
2020-07-21 20:34:58 +08:00
|
|
|
|
2021-12-13 22:42:03 +08:00
|
|
|
expect(headerStyles.fill.trim()).toBe("#5a0");
|
|
|
|
expect(langNameStyles.fill.trim()).toBe(`#${themes.radical.text_color}`);
|
2020-07-21 20:34:58 +08:00
|
|
|
expect(queryByTestId(document.body, "card-bg")).toHaveAttribute(
|
|
|
|
"fill",
|
2020-09-25 00:08:14 +08:00
|
|
|
`#${themes.radical.bg_color}`,
|
2020-07-21 20:34:58 +08:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should render with all the themes", () => {
|
|
|
|
Object.keys(themes).forEach((name) => {
|
|
|
|
document.body.innerHTML = renderTopLanguages(langs, {
|
|
|
|
theme: name,
|
|
|
|
});
|
|
|
|
|
|
|
|
const styleTag = document.querySelector("style");
|
|
|
|
const stylesObject = cssToObject(styleTag.innerHTML);
|
|
|
|
|
2021-12-13 22:42:03 +08:00
|
|
|
const headerStyles = stylesObject[":host"][".header "];
|
|
|
|
const langNameStyles = stylesObject[":host"][".lang-name "];
|
2020-07-21 20:34:58 +08:00
|
|
|
|
2021-12-13 22:42:03 +08:00
|
|
|
expect(headerStyles.fill.trim()).toBe(`#${themes[name].title_color}`);
|
|
|
|
expect(langNameStyles.fill.trim()).toBe(`#${themes[name].text_color}`);
|
2020-07-21 20:34:58 +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 20:34:58 +08:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2020-07-27 00:15:23 +08:00
|
|
|
|
2020-08-02 15:37:26 +08:00
|
|
|
it("should render with layout compact", () => {
|
|
|
|
document.body.innerHTML = renderTopLanguages(langs, { layout: "compact" });
|
2020-07-27 00:15:23 +08:00
|
|
|
|
2020-08-02 15:37:26 +08:00
|
|
|
expect(queryByTestId(document.body, "header")).toHaveTextContent(
|
2020-09-25 00:08:14 +08:00
|
|
|
"Most Used Languages",
|
2020-08-02 15:37:26 +08:00
|
|
|
);
|
2020-07-27 00:15:23 +08:00
|
|
|
|
2020-08-02 15:37:26 +08:00
|
|
|
expect(queryAllByTestId(document.body, "lang-name")[0]).toHaveTextContent(
|
2020-09-25 00:08:14 +08:00
|
|
|
"HTML 40.00%",
|
2020-08-02 15:37:26 +08:00
|
|
|
);
|
|
|
|
expect(queryAllByTestId(document.body, "lang-progress")[0]).toHaveAttribute(
|
|
|
|
"width",
|
2021-07-11 21:58:06 +08:00
|
|
|
"120",
|
2020-08-02 15:37:26 +08:00
|
|
|
);
|
2020-07-27 00:15:23 +08:00
|
|
|
|
2020-08-02 15:37:26 +08:00
|
|
|
expect(queryAllByTestId(document.body, "lang-name")[1]).toHaveTextContent(
|
2020-09-25 00:08:14 +08:00
|
|
|
"javascript 40.00%",
|
2020-08-02 15:37:26 +08:00
|
|
|
);
|
|
|
|
expect(queryAllByTestId(document.body, "lang-progress")[1]).toHaveAttribute(
|
|
|
|
"width",
|
2021-07-11 21:58:06 +08:00
|
|
|
"120",
|
2020-08-02 15:37:26 +08:00
|
|
|
);
|
2020-07-27 00:15:23 +08:00
|
|
|
|
2020-08-02 15:37:26 +08:00
|
|
|
expect(queryAllByTestId(document.body, "lang-name")[2]).toHaveTextContent(
|
2020-09-25 00:08:14 +08:00
|
|
|
"css 20.00%",
|
2020-08-02 15:37:26 +08:00
|
|
|
);
|
|
|
|
expect(queryAllByTestId(document.body, "lang-progress")[2]).toHaveAttribute(
|
|
|
|
"width",
|
2021-07-11 21:58:06 +08:00
|
|
|
"60",
|
2020-08-02 15:37:26 +08:00
|
|
|
);
|
|
|
|
});
|
2020-10-04 16:05:15 +08:00
|
|
|
|
|
|
|
it("should render a translated title", () => {
|
|
|
|
document.body.innerHTML = renderTopLanguages(langs, { locale: "cn" });
|
|
|
|
expect(document.getElementsByClassName("header")[0].textContent).toBe(
|
|
|
|
"最常用的语言",
|
|
|
|
);
|
|
|
|
});
|
2021-03-08 00:53:32 +08:00
|
|
|
|
|
|
|
it("should render without rounding", () => {
|
|
|
|
document.body.innerHTML = renderTopLanguages(langs, { border_radius: "0" });
|
|
|
|
expect(document.querySelector("rect")).toHaveAttribute("rx", "0");
|
2021-07-11 21:58:06 +08:00
|
|
|
document.body.innerHTML = renderTopLanguages(langs, {});
|
2021-03-08 00:53:32 +08:00
|
|
|
expect(document.querySelector("rect")).toHaveAttribute("rx", "4.5");
|
|
|
|
});
|
2021-05-07 03:01:04 +08:00
|
|
|
|
|
|
|
it("should render langs with specified langs_count", async () => {
|
2022-09-24 16:20:54 +08:00
|
|
|
const options = {
|
2021-07-11 21:58:06 +08:00
|
|
|
langs_count: 1,
|
|
|
|
};
|
2021-05-07 03:01:04 +08:00
|
|
|
document.body.innerHTML = renderTopLanguages(langs, { ...options });
|
2021-07-11 21:58:06 +08:00
|
|
|
expect(queryAllByTestId(document.body, "lang-name").length).toBe(
|
|
|
|
options.langs_count,
|
|
|
|
);
|
2021-05-07 03:01:04 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should render langs with specified langs_count even when hide is set", async () => {
|
2022-09-24 16:20:54 +08:00
|
|
|
const options = {
|
2021-05-07 03:01:04 +08:00
|
|
|
hide: ["HTML"],
|
2021-07-11 21:58:06 +08:00
|
|
|
langs_count: 2,
|
|
|
|
};
|
2021-05-07 03:01:04 +08:00
|
|
|
document.body.innerHTML = renderTopLanguages(langs, { ...options });
|
2021-07-11 21:58:06 +08:00
|
|
|
expect(queryAllByTestId(document.body, "lang-name").length).toBe(
|
|
|
|
options.langs_count,
|
|
|
|
);
|
2021-05-07 03:01:04 +08:00
|
|
|
});
|
2020-07-21 20:34:58 +08:00
|
|
|
});
|