Top languages card render test: move repeated code into helper function (#2718)

This commit is contained in:
Alexandr Garbuzov 2023-05-13 14:21:06 +03:00 committed by GitHub
parent d59a80599f
commit 6d45f89c9e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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.
*
@ -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 =
@ -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,
@ -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"),
@ -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"),