Skip to content

Commit c311a75

Browse files
committed
fix: fix bugs and add tests
1 parent 18db53d commit c311a75

File tree

9 files changed

+97
-51
lines changed

9 files changed

+97
-51
lines changed

api/pin.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export default async (req, res) => {
2424
locale,
2525
border_radius,
2626
border_color,
27-
card_width, // Add the card-width option
27+
card_width,
2828
} = req.query;
2929

3030
res.setHeader("Content-Type", "image/svg+xml");
@@ -81,7 +81,7 @@ export default async (req, res) => {
8181
border_color,
8282
show_owner: parseBoolean(show_owner),
8383
locale: locale ? locale.toLowerCase() : null,
84-
card_width: parseInt(card_width) || undefined, // Use the specified card width
84+
card_width: parseInt(card_width, 10),
8585
}),
8686
);
8787
} catch (err) {

src/cards/repo-card.js

Lines changed: 15 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import {
1515
} from "../common/utils.js";
1616
import { repoCardLocales } from "../translations.js";
1717

18+
const DEFAULT_CARD_WIDTH = 300;
19+
const MIN_CARD_WIDTH = 280;
1820
const ICON_SIZE = 16;
1921

2022
/**
@@ -49,8 +51,6 @@ const getBadgeSVG = (label, textColor) => `
4951
*
5052
* @param {RepositoryData} repo Repository data.
5153
* @param {Partial<RepoCardOptions>} options Card options.
52-
* @param {number} card_width The width of the card.
53-
* @param {number} card_height The height of the card.
5454
* @returns {string} Repository card SVG object.
5555
*/
5656
const renderRepoCard = (repo, options = {}) => {
@@ -76,7 +76,6 @@ const renderRepoCard = (repo, options = {}) => {
7676
border_color,
7777
locale,
7878
card_width,
79-
card_height,
8079
} = options;
8180

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

94-
const height = card_height || (descriptionLines > 1 ? 120 : 110) + descriptionLines * lineHeight;
95-
9693
const i18n = new I18n({
9794
locale,
9895
translations: repoCardLocales,
9996
});
10097

98+
let width = card_width
99+
? isNaN(card_width)
100+
? DEFAULT_CARD_WIDTH
101+
: card_width < MIN_CARD_WIDTH
102+
? MIN_CARD_WIDTH
103+
: card_width
104+
: DEFAULT_CARD_WIDTH;
105+
const height =
106+
(descriptionLines > 1 ? 120 : 110) + descriptionLines * lineHeight;
107+
101108
// returns theme based colors with proper overrides and defaults
102109
const colors = getCardColors({
103110
title_color,
@@ -118,13 +125,13 @@ const renderRepoCard = (repo, options = {}) => {
118125
icons.star,
119126
totalStars,
120127
"stargazers",
121-
ICON_SIZE
128+
ICON_SIZE,
122129
);
123130
const svgForks = iconWithLabel(
124131
icons.fork,
125132
totalForks,
126133
"forkcount",
127-
ICON_SIZE
134+
ICON_SIZE,
128135
);
129136

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

140-
// Calculate the card width and height based on the provided arguments or defaults;
141-
142-
const width = card_width || 400;
143-
const CARD_MIN_WIDTH = 287;
144-
const CARD_DEFAULT_WIDTH = 287;
145-
const RANK_CARD_MIN_WIDTH = 420;
146-
const RANK_CARD_DEFAULT_WIDTH = 450;
147-
const RANK_ONLY_CARD_MIN_WIDTH = 290;
148-
const RANK_ONLY_CARD_DEFAULT_WIDTH = 290;
149-
const minCardWidth =
150-
(hide_rank
151-
? clampValue(
152-
50 /* padding */ + calculateTextWidth() * 2,
153-
CARD_MIN_WIDTH,
154-
Infinity,
155-
)
156-
: statItems.length
157-
? RANK_CARD_MIN_WIDTH
158-
: RANK_ONLY_CARD_MIN_WIDTH) + iconWidth;
159-
const defaultCardWidth =
160-
(hide_rank
161-
? CARD_DEFAULT_WIDTH
162-
: statItems.length
163-
? RANK_CARD_DEFAULT_WIDTH
164-
: RANK_ONLY_CARD_DEFAULT_WIDTH) + iconWidth;
165-
let width = card_width
166-
? isNaN(card_width)
167-
? defaultCardWidth
168-
: card_width
169-
: defaultCardWidth;
170-
if (width < minCardWidth) {
171-
width = minCardWidth;
172-
}
173-
174147
const card = new Card({
175148
defaultTitle: header.length > 35 ? `${header.slice(0, 35)}...` : header,
176149
titlePrefixIcon: icons.contribs,
@@ -212,5 +185,5 @@ const renderRepoCard = (repo, options = {}) => {
212185
`);
213186
};
214187

215-
export { renderRepoCard };
188+
export { renderRepoCard, DEFAULT_CARD_WIDTH, MIN_CARD_WIDTH };
216189
export default renderRepoCard;

src/cards/stats-card.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,5 +541,5 @@ const renderStatsCard = (stats, options = {}) => {
541541
`);
542542
};
543543

544-
export { renderStatsCard };
544+
export { renderStatsCard, RANK_CARD_DEFAULT_WIDTH, RANK_CARD_MIN_WIDTH };
545545
export default renderStatsCard;

src/cards/top-languages-card.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ const renderDonutVerticalLayout = (langs, totalLanguageSize) => {
431431

432432
circles.push(`
433433
<g class="stagger" style="animation-delay: ${delay}ms">
434-
<circle
434+
<circle
435435
cx="150"
436436
cy="100"
437437
r="${radius}"
@@ -885,6 +885,7 @@ export {
885885
donutCenterTranslation,
886886
trimTopLanguages,
887887
renderTopLanguages,
888+
DEFAULT_CARD_WIDTH,
888889
MIN_CARD_WIDTH,
889890
getDefaultLanguagesCountByLayout,
890891
};

src/cards/types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export type StatCardOptions = CommonOptions & {
3232

3333
export type RepoCardOptions = CommonOptions & {
3434
show_owner: boolean;
35+
card_width: number;
3536
};
3637

3738
export type TopLangOptions = CommonOptions & {

tests/pin.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ describe("Test /api/pin", () => {
7373
text_color: "fff",
7474
bg_color: "fff",
7575
full_name: "1",
76+
card_width: 100,
7677
},
7778
};
7879
const res = {

tests/renderRepoCard.test.js

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { queryByTestId } from "@testing-library/dom";
22
import "@testing-library/jest-dom";
33
import { cssToObject } from "@uppercod/css-to-object";
4-
import { renderRepoCard } from "../src/cards/repo-card.js";
4+
import {
5+
renderRepoCard,
6+
DEFAULT_CARD_WIDTH,
7+
MIN_CARD_WIDTH,
8+
} from "../src/cards/repo-card.js";
59
import { expect, it, describe } from "@jest/globals";
610

711
import { themes } from "../themes/index.js";
@@ -338,4 +342,56 @@ describe("Test renderRepoCard", () => {
338342
"No description provided",
339343
);
340344
});
345+
346+
it("should render with custom width set", () => {
347+
document.body.innerHTML = renderRepoCard({
348+
...data_repo.repository,
349+
description: undefined,
350+
isArchived: true,
351+
});
352+
353+
expect(document.querySelector("svg")).toHaveAttribute(
354+
"width",
355+
DEFAULT_CARD_WIDTH.toString(),
356+
);
357+
358+
document.body.innerHTML = renderRepoCard(
359+
{
360+
...data_repo.repository,
361+
description: undefined,
362+
isArchived: true,
363+
},
364+
{ card_width: 400 },
365+
);
366+
expect(document.querySelector("svg")).toHaveAttribute("width", "400");
367+
});
368+
369+
it("should render with min width", () => {
370+
document.body.innerHTML = renderRepoCard(
371+
{
372+
...data_repo.repository,
373+
description: undefined,
374+
isArchived: true,
375+
},
376+
{ card_width: 190 },
377+
);
378+
379+
expect(document.querySelector("svg")).toHaveAttribute(
380+
"width",
381+
MIN_CARD_WIDTH.toString(),
382+
);
383+
384+
document.body.innerHTML = renderRepoCard(
385+
{
386+
...data_repo.repository,
387+
description: undefined,
388+
isArchived: true,
389+
},
390+
{ card_width: 100 },
391+
);
392+
expect(document.querySelector("svg")).toHaveAttribute(
393+
"width",
394+
MIN_CARD_WIDTH.toString(),
395+
);
396+
});
341397
});

tests/renderStatsCard.test.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import {
44
queryByTestId,
55
} from "@testing-library/dom";
66
import { cssToObject } from "@uppercod/css-to-object";
7-
import { renderStatsCard } from "../src/cards/stats-card.js";
7+
import {
8+
renderStatsCard,
9+
RANK_CARD_DEFAULT_WIDTH,
10+
RANK_CARD_MIN_WIDTH,
11+
} from "../src/cards/stats-card.js";
812
import { expect, it, describe } from "@jest/globals";
913
import { CustomError } from "../src/common/utils.js";
1014

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

132136
it("should render with custom width set", () => {
133137
document.body.innerHTML = renderStatsCard(stats);
134-
expect(document.querySelector("svg")).toHaveAttribute("width", "450");
138+
expect(document.querySelector("svg")).toHaveAttribute(
139+
"width",
140+
RANK_CARD_DEFAULT_WIDTH.toString(),
141+
);
135142

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

140147
it("should render with custom width set and limit minimum width", () => {
141148
document.body.innerHTML = renderStatsCard(stats, { card_width: 1 });
142-
expect(document.querySelector("svg")).toHaveAttribute("width", "420");
149+
expect(document.querySelector("svg")).toHaveAttribute(
150+
"width",
151+
RANK_CARD_MIN_WIDTH.toString(),
152+
);
143153

144154
// Test default minimum card width without rank circle.
145155
document.body.innerHTML = renderStatsCard(stats, {

tests/renderTopLanguagesCard.test.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
donutCenterTranslation,
1616
trimTopLanguages,
1717
renderTopLanguages,
18+
DEFAULT_CARD_WIDTH,
1819
MIN_CARD_WIDTH,
1920
getDefaultLanguagesCountByLayout,
2021
} from "../src/cards/top-languages-card.js";
@@ -428,7 +429,10 @@ describe("Test renderTopLanguages", () => {
428429
it("should render with custom width set", () => {
429430
document.body.innerHTML = renderTopLanguages(langs, {});
430431

431-
expect(document.querySelector("svg")).toHaveAttribute("width", "300");
432+
expect(document.querySelector("svg")).toHaveAttribute(
433+
"width",
434+
DEFAULT_CARD_WIDTH.toString(),
435+
);
432436

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

0 commit comments

Comments
 (0)