Skip to content

Commit

Permalink
Top languages card render test: move repeated code into helper functi…
Browse files Browse the repository at this point in the history
  • Loading branch information
qwerty541 authored May 13, 2023
1 parent d59a805 commit 6d45f89
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions tests/renderTopLanguages.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@ const langs = {
},
};

/**
* Retrieve number array from SVG path definition string.
*
* @param {string} d SVG path definition string.
* @return {number[]} Resulting numbers array.
*/
const getNumbersFromSvgPathDefinitionAttribute = (d) => {
return d
.split(" ")
.filter((x) => !isNaN(x))
.map((x) => parseFloat(x));
};

/**
* Retrieve the language percentage from the donut chart SVG.
*
Expand All @@ -48,10 +61,7 @@ const langs = {
* @returns {number} The percentage of the language.
*/
const langPercentFromDonutLayoutSvg = (d, centerX, centerY) => {
const dTmp = d
.split(" ")
.filter((x) => !isNaN(x))
.map((x) => parseFloat(x));
const dTmp = getNumbersFromSvgPathDefinitionAttribute(d);
const endAngle =
cartesianToPolar(centerX, centerY, dTmp[0], dTmp[1]).angleInDegrees + 90;
let startAngle =
Expand All @@ -69,10 +79,7 @@ const langPercentFromDonutLayoutSvg = (d, centerX, centerY) => {
* @returns {number} The percentage of the language.
*/
const langPercentFromPieLayoutSvg = (d, centerX, centerY) => {
const dTmp = d
.split(" ")
.filter((x) => !isNaN(x))
.map((x) => parseFloat(x));
const dTmp = getNumbersFromSvgPathDefinitionAttribute(d);
const startAngle = cartesianToPolar(
centerX,
centerY,
Expand Down Expand Up @@ -504,11 +511,9 @@ describe("Test renderTopLanguages", () => {
"size",
"40",
);
const d = queryAllByTestId(document.body, "lang-donut")[0]
.getAttribute("d")
.split(" ")
.filter((x) => !isNaN(x))
.map((x) => parseFloat(x));
const d = getNumbersFromSvgPathDefinitionAttribute(
queryAllByTestId(document.body, "lang-donut")[0].getAttribute("d"),
);
const center = { x: d[7], y: d[7] };
const HTMLLangPercent = langPercentFromDonutLayoutSvg(
queryAllByTestId(document.body, "lang-donut")[0].getAttribute("d"),
Expand Down Expand Up @@ -579,11 +584,9 @@ describe("Test renderTopLanguages", () => {
"40",
);

const d = queryAllByTestId(document.body, "lang-pie")[0]
.getAttribute("d")
.split(" ")
.filter((x) => !isNaN(x))
.map((x) => parseFloat(x));
const d = getNumbersFromSvgPathDefinitionAttribute(
queryAllByTestId(document.body, "lang-pie")[0].getAttribute("d"),
);
const center = { x: d[0], y: d[1] };
const HTMLLangPercent = langPercentFromPieLayoutSvg(
queryAllByTestId(document.body, "lang-pie")[0].getAttribute("d"),
Expand Down

0 comments on commit 6d45f89

Please sign in to comment.