Skip to content

Commit

Permalink
Merge branch 'main' into sarchibeque/MWPW-137282-center-actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Sartxi authored Nov 7, 2023
2 parents 7fcde9e + b52dfa0 commit ce06030
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 19 deletions.
1 change: 1 addition & 0 deletions CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 12 additions & 8 deletions libs/blocks/review/review.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -40,7 +41,7 @@ const getProductJson = () => {
};
};

const App = ({ rootEl, strings }) => html`
const App = ({ strings }) => html`
<${HelixReview}
clickTimeout="5000"
commentThreshold=${COMMENT_THRESHOLD}
Expand All @@ -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 }) => {}}
/>
`;

Expand Down Expand Up @@ -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.');
}
}
14 changes: 13 additions & 1 deletion libs/blocks/review/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
3 changes: 3 additions & 0 deletions test/blocks/review/mocks/body-stage.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div data-valign="middle">
<a href="/data/review">https://www.adobe.com/reviews-api/dc/production/rotate-pdf</a>
</div>
1 change: 0 additions & 1 deletion test/blocks/review/mocks/missing-fields.html

This file was deleted.

9 changes: 0 additions & 9 deletions test/blocks/review/review.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
});
18 changes: 18 additions & 0 deletions test/blocks/review/utils/utils.test.js
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand All @@ -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');
});
});
});

0 comments on commit ce06030

Please sign in to comment.