Skip to content

Commit

Permalink
Video blocks use util fxn, and tests updated
Browse files Browse the repository at this point in the history
  • Loading branch information
cedric066 committed Aug 11, 2023
1 parent 5169656 commit 0d85cae
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 36 deletions.
21 changes: 3 additions & 18 deletions blocks/article-body-block/chains/article-body/default.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
Conditional,
Divider,
formatCredits,
getAspectRatio,
Heading,
HeadingSection,
Icon,
Expand Down Expand Up @@ -46,19 +47,6 @@ function parseArticleItem(item, index, arcSite, phrases, id, customFields) {
hideVideoCredits = false,
} = customFields;

// Helper function to find the Greatest Common Denominator (GCD) of two numbers. This is used to calculate the aspect ratio
const gcd = (valA, valB) => {
let a = Math.abs(valA);
let b = Math.abs(valB);
while (b) {
const temp = b;
b = a % b;
a = temp;
}

return a;
};

// This is up here to prevent a lexical declaration in a case block (which throws an error). It goes with the "video" case
let videoAspectRatio = "16:9"; // Default to 16:9

Expand Down Expand Up @@ -231,12 +219,9 @@ function parseArticleItem(item, index, arcSite, phrases, id, customFields) {
// Get the width and height of the promo item and calculate the aspect ratio
const width = item?.promo_items.basic.width;
const height = item?.promo_items.basic.height;
const divisor = gcd(width, height);
const aspectWidth = width / divisor;
const aspectHeight = height / divisor;

// Assign the new value to videoAspectRatio
videoAspectRatio = `${aspectWidth}:${aspectHeight}`;
// Assign the calculated value to aspectRatio if it is non-null, otherwise assign default 16:9
videoAspectRatio = getAspectRatio(width, height) || "16:9";
}

return (
Expand Down
27 changes: 27 additions & 0 deletions blocks/article-body-block/chains/article-body/default.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,33 @@ jest.mock("fusion:properties", () =>
jest.mock("@wpmedia/arc-themes-components", () => ({
...jest.requireActual("@wpmedia/arc-themes-components"),
isServerSide: jest.fn(),
getAspectRatio: (width, height) => {
// This arrow function is equivalent to what is in @wpmedia/arc-themes-components/src/utils/get-aspect-ratio/utils.js
// Helper function to find GCD
const gcd = (valA, valB) => {
let a = Math.abs(valA);
let b = Math.abs(valB);
while (b) {
const temp = b;
b = a % b;
a = temp;
}

return a;
};

// Return undefined if height === 0, so there is no division by zero error
if (height === 0) {
return undefined;
}

// Calculate the aspect ratio
const divisor = gcd(width, height);
const aspectWidth = width / divisor;
const aspectHeight = height / divisor;

return `${aspectWidth}:${aspectHeight}`;
},
LazyLoad: ({ children }) => <>{children}</>,
}));

Expand Down
21 changes: 3 additions & 18 deletions blocks/lead-art-block/features/leadart/default.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
Carousel,
formatCredits,
formatPowaVideoEmbed,
getAspectRatio,
Icon,
Image,
MediaItem,
Expand Down Expand Up @@ -96,32 +97,16 @@ export const LeadArtPresentation = (props) => {
playthrough: customFields?.playthrough,
});

// Helper function to find the Greatest Common Denominator (GCD) of two numbers. This is used to calculate the aspect ratio
const gcd = (valA, valB) => {
let a = Math.abs(valA);
let b = Math.abs(valB);
while (b) {
const temp = b;
b = a % b;
a = temp;
}

return a;
};

let aspectRatio = "16:9"; // Default to 16:9

// Make sure that the content source exists and has an existing promo item
if (leadArt && leadArt.promo_items && leadArt.promo_items.basic) {
// Get the width and height of the promo item and calculate the aspect ratio
const width = leadArt?.promo_items.basic.width;
const height = leadArt?.promo_items.basic.height;
const divisor = gcd(width, height);
const aspectWidth = width / divisor;
const aspectHeight = height / divisor;

// Assign the calculated value to aspectRatio
aspectRatio = `${aspectWidth}:${aspectHeight}`;
// Assign the calculated value to aspectRatio if it is non-null, otherwise assign default 16:9
aspectRatio = getAspectRatio(width, height) || "16:9";
}

return (
Expand Down
27 changes: 27 additions & 0 deletions blocks/lead-art-block/features/leadart/default.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,33 @@ const { mount } = require("enzyme");

jest.mock("@wpmedia/arc-themes-components", () => ({
...jest.requireActual("@wpmedia/arc-themes-components"),
getAspectRatio: (width, height) => {
// This arrow function is equivalent to what is in @wpmedia/arc-themes-components/src/utils/get-aspect-ratio/utils.js
// Helper function to find GCD
const gcd = (valA, valB) => {
let a = Math.abs(valA);
let b = Math.abs(valB);
while (b) {
const temp = b;
b = a % b;
a = temp;
}

return a;
};

// Return undefined if height === 0, so there is no division by zero error
if (height === 0) {
return undefined;
}

// Calculate the aspect ratio
const divisor = gcd(width, height);
const aspectWidth = width / divisor;
const aspectHeight = height / divisor;

return `${aspectWidth}:${aspectHeight}`;
},
usePhrases: jest.fn(() => ({
t: jest.fn().mockReturnValue("gallery-expand"),
})),
Expand Down
27 changes: 27 additions & 0 deletions blocks/video-player-block/features/video-player/default.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,33 @@ jest.mock("@wpmedia/arc-themes-components", () => {
...original,
formatCredits: (value) => value,
formatPowaVideoEmbed: (value) => value,
getAspectRatio: (width, height) => {
// This arrow function is equivalent to what is in @wpmedia/arc-themes-components/src/utils/get-aspect-ratio/utils.js
// Helper function to find GCD
const gcd = (valA, valB) => {
let a = Math.abs(valA);
let b = Math.abs(valB);
while (b) {
const temp = b;
b = a % b;
a = temp;
}

return a;
};

// Return undefined if height === 0, so there is no division by zero error
if (height === 0) {
return undefined;
}

// Calculate the aspect ratio
const divisor = gcd(width, height);
const aspectWidth = width / divisor;
const aspectHeight = height / divisor;

return `${aspectWidth}:${aspectHeight}`;
},
Video: ({ embedMarkup }) => <div dangerouslySetInnerHTML={{ __html: embedMarkup }} />,
};
});
Expand Down

0 comments on commit 0d85cae

Please sign in to comment.