From b52dfa0a159a9bf82c82ce951ed1bc67dc58e933 Mon Sep 17 00:00:00 2001 From: draganatrajkovic <65951679+draganatrajkovic@users.noreply.github.com> Date: Tue, 7 Nov 2023 17:06:34 +0100 Subject: [PATCH] MWPW-137448 - Point Stage to Acom Reviews on www.stage.adobe.com (#1393) * MWPW-137448 - Point Stage to Acom Reviews on www.stage.adobe.com Co-authored-by: Dragana Trajkovic --- CODEOWNERS | 1 + libs/blocks/review/review.js | 20 ++++++++++++-------- libs/blocks/review/utils/utils.js | 14 +++++++++++++- test/blocks/review/mocks/body-stage.html | 3 +++ test/blocks/review/mocks/missing-fields.html | 1 - test/blocks/review/review.test.js | 9 --------- test/blocks/review/utils/utils.test.js | 18 ++++++++++++++++++ 7 files changed, 47 insertions(+), 19 deletions(-) create mode 100644 test/blocks/review/mocks/body-stage.html delete mode 100644 test/blocks/review/mocks/missing-fields.html diff --git a/CODEOWNERS b/CODEOWNERS index 920c846853..e5e9de679e 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -38,6 +38,7 @@ /libs/blocks/quiz/ @colloyd @sabyamon @fullcolorcoder @JackySun9 @elaineskpt @echampio-at-adobe /libs/blocks/quiz-results/ @colloyd @sabyamon @fullcolorcoder @JackySun9 @elaineskpt @echampio-at-adobe /libs/blocks/quote/ @ryanmparrish +/libs/blocks/review/ @nkthakur48 @chrischrischris @auniverseaway /libs/blocks/table-of-contents/ @Brandon32 /libs/blocks/tabs/ @ryanmparrish /libs/blocks/tag-selector/ @meganthecoder diff --git a/libs/blocks/review/review.js b/libs/blocks/review/review.js index c52363a631..5879243166 100644 --- a/libs/blocks/review/review.js +++ b/libs/blocks/review/review.js @@ -2,6 +2,7 @@ import { html, render } from '../../deps/htm-preact.js'; import { getMetadata, loadStyle, getConfig } from '../../utils/utils.js'; import HelixReview from './components/helixReview/HelixReview.js'; +import { checkPostUrl } from './utils/utils.js'; const COMMENT_THRESHOLD = 3; @@ -40,7 +41,7 @@ const getProductJson = () => { }; }; -const App = ({ rootEl, strings }) => html` +const App = ({ strings }) => html` <${HelixReview} clickTimeout="5000" commentThreshold=${COMMENT_THRESHOLD} @@ -54,9 +55,6 @@ const App = ({ rootEl, strings }) => html` visitorId=${getVisitorId()} reviewPath=${getReviewPath(strings.postUrl)} initialValue=${strings.initialValue} - onRatingSet=${({ rating, comment }) => {}} - onRatingHover=${({ rating }) => {}} - onReviewLoad=${({ hasRated, rating }) => {}} /> `; @@ -115,15 +113,21 @@ const removeMetaDataElements = (el) => { }; export default async function init(el) { - const { miloLibs, codeRoot } = getConfig(); + const { miloLibs, codeRoot, env } = getConfig(); const base = miloLibs || codeRoot; loadStyle(`${base}/ui/page/page.css`); const metaData = getMetaData(el); const strings = getStrings(metaData); - removeMetaDataElements(el); - const app = html` <${App} rootEl=${el} strings="${strings}" /> `; + if (strings.postUrl) { + strings.postUrl = checkPostUrl(strings.postUrl, env); + removeMetaDataElements(el); - render(app, el); + const app = html` <${App} strings="${strings}" /> `; + + render(app, el); + } else { + throw new Error('Invalid postUrl. Initialization aborted.'); + } } diff --git a/libs/blocks/review/utils/utils.js b/libs/blocks/review/utils/utils.js index e5b1016e48..3f38f8dbd5 100644 --- a/libs/blocks/review/utils/utils.js +++ b/libs/blocks/review/utils/utils.js @@ -9,4 +9,16 @@ const isKeyboardNavigation = (ev) => (ev.clientX === 0 && ev.clientY === 0) && ev.nativeEvent.webkitForce !== undefined && ev.nativeEvent.webkitForce === 0); -export { addToAverage, isKeyboardNavigation }; +const checkPostUrl = (postUrl, env) => { + try { + const url = new URL(postUrl); + const adobeUrlPattern = /adobe\.com$/; + return (env?.name !== 'prod' && url.origin.match(adobeUrlPattern)) + ? url.origin.replace(adobeUrlPattern, 'stage.adobe.com') + url.pathname + url.search + url.hash + : postUrl; + } catch (err) { + throw new Error(`Invalid URL format: ${err.message}`); + } +}; + +export { addToAverage, isKeyboardNavigation, checkPostUrl }; diff --git a/test/blocks/review/mocks/body-stage.html b/test/blocks/review/mocks/body-stage.html new file mode 100644 index 0000000000..de8a15b87a --- /dev/null +++ b/test/blocks/review/mocks/body-stage.html @@ -0,0 +1,3 @@ +
+ https://www.adobe.com/reviews-api/dc/production/rotate-pdf +
diff --git a/test/blocks/review/mocks/missing-fields.html b/test/blocks/review/mocks/missing-fields.html deleted file mode 100644 index c9992e773b..0000000000 --- a/test/blocks/review/mocks/missing-fields.html +++ /dev/null @@ -1 +0,0 @@ -
diff --git a/test/blocks/review/review.test.js b/test/blocks/review/review.test.js index 91125e9610..91af4b419a 100644 --- a/test/blocks/review/review.test.js +++ b/test/blocks/review/review.test.js @@ -16,13 +16,4 @@ describe('Review Comp', () => { const review = await waitForElement('.hlx-ReviewWrapper'); expect(review).to.exist; }); - - it('could be initialized (with all missing fields)', async () => { - document.body.innerHTML = await readFile({ path: './mocks/missing-fields.html' }); - const div = document.querySelector('.review'); - await init(div); - - const review = await waitForElement('.hlx-ReviewWrapper'); - expect(review).to.exist; - }); }); diff --git a/test/blocks/review/utils/utils.test.js b/test/blocks/review/utils/utils.test.js index 251102d159..83884fb826 100644 --- a/test/blocks/review/utils/utils.test.js +++ b/test/blocks/review/utils/utils.test.js @@ -1,7 +1,9 @@ import { expect } from '@esm-bundle/chai'; +import { readFile } from '@web/test-runner-commands'; import { addToAverage, isKeyboardNavigation, + checkPostUrl, } from '../../../../libs/blocks/review/utils/utils.js'; describe('Utils', () => { @@ -16,4 +18,20 @@ describe('Utils', () => { const input = new KeyboardEvent('keypress'); expect(isKeyboardNavigation(input)).to.be.equal(undefined); }); + + describe('CheckReviewPostUrl', () => { + let mockUrl; + beforeEach(async () => { + document.body.innerHTML = await readFile({ path: './../mocks/body-stage.html' }); + mockUrl = document.querySelector('div a').innerText; + }); + it('should not change post url on prod', () => { + const modifiedUrl = checkPostUrl(mockUrl, { name: 'prod' }); + expect(modifiedUrl).to.be.eql(mockUrl); + }); + it('should change post url in a non-prod env', () => { + const modifiedUrl = checkPostUrl(mockUrl, { name: 'stage' }); + expect(modifiedUrl).to.be.eql('https://www.stage.adobe.com/reviews-api/dc/production/rotate-pdf'); + }); + }); });