Skip to content

Commit

Permalink
fix: fix bugs and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rickstaa committed Oct 16, 2023
1 parent 18db53d commit 49b72b8
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 51 deletions.
4 changes: 2 additions & 2 deletions api/pin.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default async (req, res) => {
locale,
border_radius,
border_color,
card_width, // Add the card-width option
card_width,
} = req.query;

res.setHeader("Content-Type", "image/svg+xml");
Expand Down Expand Up @@ -81,7 +81,7 @@ export default async (req, res) => {
border_color,
show_owner: parseBoolean(show_owner),
locale: locale ? locale.toLowerCase() : null,
card_width: parseInt(card_width) || undefined, // Use the specified card width
card_width: parseInt(card_width, 10),
}),
);
} catch (err) {
Expand Down
57 changes: 15 additions & 42 deletions src/cards/repo-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
} from "../common/utils.js";
import { repoCardLocales } from "../translations.js";

const DEFAULT_CARD_WIDTH = 300;
const MIN_CARD_WIDTH = 400;
const ICON_SIZE = 16;

/**
Expand Down Expand Up @@ -49,8 +51,6 @@ const getBadgeSVG = (label, textColor) => `
*
* @param {RepositoryData} repo Repository data.
* @param {Partial<RepoCardOptions>} options Card options.
* @param {number} card_width The width of the card.
* @param {number} card_height The height of the card.
* @returns {string} Repository card SVG object.
*/
const renderRepoCard = (repo, options = {}) => {
Expand All @@ -76,7 +76,6 @@ const renderRepoCard = (repo, options = {}) => {
border_color,
locale,
card_width,
card_height,
} = options;

const lineHeight = 10;
Expand All @@ -91,13 +90,21 @@ const renderRepoCard = (repo, options = {}) => {
.map((line) => `<tspan dy="1.2em" x="25">${encodeHTML(line)}</tspan>`)
.join("");

const height = card_height || (descriptionLines > 1 ? 120 : 110) + descriptionLines * lineHeight;

const i18n = new I18n({
locale,
translations: repoCardLocales,
});

let width = card_width
? isNaN(card_width)
? DEFAULT_CARD_WIDTH
: card_width < MIN_CARD_WIDTH
? MIN_CARD_WIDTH
: card_width
: DEFAULT_CARD_WIDTH;
const height =
(descriptionLines > 1 ? 120 : 110) + descriptionLines * lineHeight;

// returns theme based colors with proper overrides and defaults
const colors = getCardColors({
title_color,
Expand All @@ -118,13 +125,13 @@ const renderRepoCard = (repo, options = {}) => {
icons.star,
totalStars,
"stargazers",
ICON_SIZE
ICON_SIZE,
);
const svgForks = iconWithLabel(
icons.fork,
totalForks,
"forkcount",
ICON_SIZE
ICON_SIZE,
);

const starAndForkCount = flexLayout({
Expand All @@ -137,40 +144,6 @@ const renderRepoCard = (repo, options = {}) => {
gap: 25,
}).join("");

// Calculate the card width and height based on the provided arguments or defaults;

const width = card_width || 400;
const CARD_MIN_WIDTH = 287;
const CARD_DEFAULT_WIDTH = 287;
const RANK_CARD_MIN_WIDTH = 420;
const RANK_CARD_DEFAULT_WIDTH = 450;
const RANK_ONLY_CARD_MIN_WIDTH = 290;
const RANK_ONLY_CARD_DEFAULT_WIDTH = 290;
const minCardWidth =
(hide_rank
? clampValue(
50 /* padding */ + calculateTextWidth() * 2,
CARD_MIN_WIDTH,
Infinity,
)
: statItems.length
? RANK_CARD_MIN_WIDTH
: RANK_ONLY_CARD_MIN_WIDTH) + iconWidth;
const defaultCardWidth =
(hide_rank
? CARD_DEFAULT_WIDTH
: statItems.length
? RANK_CARD_DEFAULT_WIDTH
: RANK_ONLY_CARD_DEFAULT_WIDTH) + iconWidth;
let width = card_width
? isNaN(card_width)
? defaultCardWidth
: card_width
: defaultCardWidth;
if (width < minCardWidth) {
width = minCardWidth;
}

const card = new Card({
defaultTitle: header.length > 35 ? `${header.slice(0, 35)}...` : header,
titlePrefixIcon: icons.contribs,
Expand Down Expand Up @@ -212,5 +185,5 @@ const renderRepoCard = (repo, options = {}) => {
`);
};

export { renderRepoCard };
export { renderRepoCard, DEFAULT_CARD_WIDTH, MIN_CARD_WIDTH };
export default renderRepoCard;
2 changes: 1 addition & 1 deletion src/cards/stats-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -541,5 +541,5 @@ const renderStatsCard = (stats, options = {}) => {
`);
};

export { renderStatsCard };
export { renderStatsCard, RANK_CARD_DEFAULT_WIDTH, RANK_CARD_MIN_WIDTH };
export default renderStatsCard;
3 changes: 2 additions & 1 deletion src/cards/top-languages-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ const renderDonutVerticalLayout = (langs, totalLanguageSize) => {

circles.push(`
<g class="stagger" style="animation-delay: ${delay}ms">
<circle
<circle
cx="150"
cy="100"
r="${radius}"
Expand Down Expand Up @@ -885,6 +885,7 @@ export {
donutCenterTranslation,
trimTopLanguages,
renderTopLanguages,
DEFAULT_CARD_WIDTH,
MIN_CARD_WIDTH,
getDefaultLanguagesCountByLayout,
};
1 change: 1 addition & 0 deletions src/cards/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export type StatCardOptions = CommonOptions & {

export type RepoCardOptions = CommonOptions & {
show_owner: boolean;
card_width: number;
};

export type TopLangOptions = CommonOptions & {
Expand Down
1 change: 1 addition & 0 deletions tests/pin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ describe("Test /api/pin", () => {
text_color: "fff",
bg_color: "fff",
full_name: "1",
card_width: 100,
},
};
const res = {
Expand Down
58 changes: 57 additions & 1 deletion tests/renderRepoCard.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { queryByTestId } from "@testing-library/dom";
import "@testing-library/jest-dom";
import { cssToObject } from "@uppercod/css-to-object";
import { renderRepoCard } from "../src/cards/repo-card.js";
import {
renderRepoCard,
DEFAULT_CARD_WIDTH,
MIN_CARD_WIDTH,
} from "../src/cards/repo-card.js";
import { expect, it, describe } from "@jest/globals";

import { themes } from "../themes/index.js";
Expand Down Expand Up @@ -338,4 +342,56 @@ describe("Test renderRepoCard", () => {
"No description provided",
);
});

it("should render with custom width set", () => {
document.body.innerHTML = renderRepoCard({
...data_repo.repository,
description: undefined,
isArchived: true,
});

expect(document.querySelector("svg")).toHaveAttribute(
"width",
DEFAULT_CARD_WIDTH.toString(),
);

document.body.innerHTML = renderRepoCard(
{
...data_repo.repository,
description: undefined,
isArchived: true,
},
{ card_width: 400 },
);
expect(document.querySelector("svg")).toHaveAttribute("width", "400");
});

it("should render with min width", () => {
document.body.innerHTML = renderRepoCard(
{
...data_repo.repository,
description: undefined,
isArchived: true,
},
{ card_width: 190 },
);

expect(document.querySelector("svg")).toHaveAttribute(
"width",
MIN_CARD_WIDTH.toString(),
);

document.body.innerHTML = renderRepoCard(
{
...data_repo.repository,
description: undefined,
isArchived: true,
},
{ card_width: 100 },
);
expect(document.querySelector("svg")).toHaveAttribute(
"width",
MIN_CARD_WIDTH.toString(),
);
});
});
16 changes: 13 additions & 3 deletions tests/renderStatsCard.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import {
queryByTestId,
} from "@testing-library/dom";
import { cssToObject } from "@uppercod/css-to-object";
import { renderStatsCard } from "../src/cards/stats-card.js";
import {
renderStatsCard,
RANK_CARD_DEFAULT_WIDTH,
RANK_CARD_MIN_WIDTH,
} from "../src/cards/stats-card.js";
import { expect, it, describe } from "@jest/globals";
import { CustomError } from "../src/common/utils.js";

Expand Down Expand Up @@ -131,15 +135,21 @@ describe("Test renderStatsCard", () => {

it("should render with custom width set", () => {
document.body.innerHTML = renderStatsCard(stats);
expect(document.querySelector("svg")).toHaveAttribute("width", "450");
expect(document.querySelector("svg")).toHaveAttribute(
"width",
RANK_CARD_DEFAULT_WIDTH.toString(),
);

document.body.innerHTML = renderStatsCard(stats, { card_width: 500 });
expect(document.querySelector("svg")).toHaveAttribute("width", "500");
});

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", "420");
expect(document.querySelector("svg")).toHaveAttribute(
"width",
RANK_CARD_MIN_WIDTH.toString(),
);

// Test default minimum card width without rank circle.
document.body.innerHTML = renderStatsCard(stats, {
Expand Down
6 changes: 5 additions & 1 deletion tests/renderTopLanguagesCard.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
donutCenterTranslation,
trimTopLanguages,
renderTopLanguages,
DEFAULT_CARD_WIDTH,
MIN_CARD_WIDTH,
getDefaultLanguagesCountByLayout,
} from "../src/cards/top-languages-card.js";
Expand Down Expand Up @@ -428,7 +429,10 @@ describe("Test renderTopLanguages", () => {
it("should render with custom width set", () => {
document.body.innerHTML = renderTopLanguages(langs, {});

expect(document.querySelector("svg")).toHaveAttribute("width", "300");
expect(document.querySelector("svg")).toHaveAttribute(
"width",
DEFAULT_CARD_WIDTH.toString(),
);

document.body.innerHTML = renderTopLanguages(langs, { card_width: 400 });
expect(document.querySelector("svg")).toHaveAttribute("width", "400");
Expand Down

0 comments on commit 49b72b8

Please sign in to comment.