Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions react-components/src/utils/__test__/createProductPageUrl.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,23 @@ describe('createProductPageUrl', () => {
expect(url).toBeNull();
});

it('creates a product page url for a product sku', () => {
it('creates a product page url for a product sku from body dataset', () => {
document.body.dataset.storeRootUrl = '/content/venia/us/en.html';

const url = createProductPageUrl('my-product');

let url = createProductPageUrl('my-product');
expect(url).toEqual('http://localhost/content/venia/us/en.cifproductredirect.html/my-product');
url = createProductPageUrl('my-other-product');
expect(url).toEqual('http://localhost/content/venia/us/en.cifproductredirect.html/my-other-product');
});

it('creates a product page url for a product sku from meta element', () => {
const el = { content: '{"storeRootUrl":"/content/venia2/us/en.html"}' };
// eslint-disable-next-line no-unused-vars
jest.spyOn(document, 'querySelector').mockImplementation(_ => el);

let url = createProductPageUrl('my-product');
expect(url).toEqual('http://localhost/content/venia2/us/en.cifproductredirect.html/my-product');
url = createProductPageUrl('my-other-product');
expect(url).toEqual('http://localhost/content/venia2/us/en.cifproductredirect.html/my-other-product');
});
});
22 changes: 7 additions & 15 deletions react-components/src/utils/createProductPageUrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,26 @@
~ limitations under the License.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

let storeRootUrl = null;

export const createProductPageUrl = sku => {
const url = new URL(window.location);

// Provided by StoreConfigExporter
if (storeRootUrl) {
return storeRootUrl;
}

let storeConfigEl = document.querySelector('meta[name="store-config"]');
let pathname;
let storeRootUrl;

if (storeConfigEl) {
pathname = JSON.parse(storeConfigEl.content).storeRootUrl;
storeRootUrl = JSON.parse(storeConfigEl.content).storeRootUrl;
} else {
// TODO: deprecated - the store configuration on the <body> has been deprecated and will be removed
pathname = document.body.dataset.storeRootUrl;
storeRootUrl = document.body.dataset.storeRootUrl;
}

if (!pathname) {
if (!storeRootUrl) {
return null;
}

const extension = '.html';
const path = pathname.substr(0, pathname.lastIndexOf('.'));

url.pathname = `${path}.cifproductredirect${extension}/${sku}`;
const path = storeRootUrl.substr(0, storeRootUrl.lastIndexOf('.'));
url.pathname = `${path}.cifproductredirect.html/${sku}`;

return (storeRootUrl = url.toString());
return url.toString();
};