From f8f4504f9938967b9e4252264dcf5e51ce401a33 Mon Sep 17 00:00:00 2001 From: Sandra Golden Date: Thu, 23 Mar 2023 14:50:37 -0700 Subject: [PATCH] fixing failing test, added tests for removeSiteLocaleFromPath --- .../app/pages/checkout/confirmation.test.js | 8 ++++++ .../app/utils/url.js | 6 ++--- .../app/utils/url.test.js | 25 ++++++++++++++++++- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/packages/template-retail-react-app/app/pages/checkout/confirmation.test.js b/packages/template-retail-react-app/app/pages/checkout/confirmation.test.js index 18659b423b..8fbc664c9a 100644 --- a/packages/template-retail-react-app/app/pages/checkout/confirmation.test.js +++ b/packages/template-retail-react-app/app/pages/checkout/confirmation.test.js @@ -26,6 +26,14 @@ jest.mock('../../commerce-api/auth', () => { } }) +jest.mock('../../utils/url', () => { + const original = jest.requireActual('../../utils/url') + return { + ...original, + removeSiteLocaleFromPath: jest.fn(() => original.removeSiteLocaleFromPath) + } +}) + const mockOrder = keysToCamel({ basket_id: 'testorderbasket', ...ocapiOrderResponse diff --git a/packages/template-retail-react-app/app/utils/url.js b/packages/template-retail-react-app/app/utils/url.js index 179ecadf41..a2db20d9c9 100644 --- a/packages/template-retail-react-app/app/utils/url.js +++ b/packages/template-retail-react-app/app/utils/url.js @@ -272,15 +272,15 @@ export const removeQueryParamsFromPath = (path, keys) => { * // returns '/account/wishlist' */ export const removeSiteLocaleFromPath = (pathName = '') => { - let {siteRef, localeRef} = getParamsFromPath(`${pathName}`) + let {siteRef, localeRef} = getParamsFromPath(pathName) // remove the site alias from the current pathName if (siteRef) { - pathName = pathName.replace(`/${siteRef}`, '') + pathName = pathName.replace(new RegExp(`/${siteRef}`, 'g'), '') } // remove the locale from the current pathName if (localeRef) { - pathName = pathName.replace(`/${localeRef}`, '') + pathName = pathName.replace(new RegExp(`/${localeRef}`, 'g'), '') } return pathName diff --git a/packages/template-retail-react-app/app/utils/url.test.js b/packages/template-retail-react-app/app/utils/url.test.js index 91d1647c3d..73f30b67e0 100644 --- a/packages/template-retail-react-app/app/utils/url.test.js +++ b/packages/template-retail-react-app/app/utils/url.test.js @@ -14,7 +14,8 @@ import { rebuildPathWithParams, removeQueryParamsFromPath, absoluteUrl, - createUrlTemplate + createUrlTemplate, + removeSiteLocaleFromPath } from './url' import {getUrlConfig} from './utils' import mockConfig from '../../config/mocks/default' @@ -389,3 +390,25 @@ describe('absoluteUrl', function () { expect(url).toEqual('https://www.example.com/uk/en/women/dresses') }) }) + +describe('removeSiteLocaleFromPath', function () { + test('return path without site alias and locale', () => { + const pathName = removeSiteLocaleFromPath('/uk/en-GB/account/wishlist') + expect(pathName).toEqual('/account/wishlist') + }) + + test('return path without site alias if they appear multiple times', () => { + const pathName = removeSiteLocaleFromPath('/uk/en-GB/uk/en-GB/account/wishlist') + expect(pathName).toEqual('/account/wishlist') + }) + + test('return expected path name when no locale or site alias appear', () => { + const pathName = removeSiteLocaleFromPath('/account/wishlist') + expect(pathName).toEqual('/account/wishlist') + }) + + test('return empty string when no path name is passed', () => { + const pathName = removeSiteLocaleFromPath() + expect(pathName).toEqual('') + }) +})