Skip to content

Commit

Permalink
fixing failing test, added tests for removeSiteLocaleFromPath
Browse files Browse the repository at this point in the history
  • Loading branch information
sandragolden committed Mar 23, 2023
1 parent c8a3ff6 commit 732a5fe
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ jest.mock('../../commerce-api/auth', () => {
}
})

jest.mock('../../utils/url', () => {
return {
...jest.requireActual('../../utils/url'),
removeSiteLocaleFromPath: jest.fn()
}
})

const mockOrder = keysToCamel({
basket_id: 'testorderbasket',
...ocapiOrderResponse
Expand Down
6 changes: 3 additions & 3 deletions packages/template-retail-react-app/app/utils/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 24 additions & 1 deletion packages/template-retail-react-app/app/utils/url.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import {
rebuildPathWithParams,
removeQueryParamsFromPath,
absoluteUrl,
createUrlTemplate
createUrlTemplate,
removeSiteLocaleFromPath
} from './url'
import {getUrlConfig} from './utils'
import mockConfig from '../../config/mocks/default'
Expand Down Expand Up @@ -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('')
})
})

0 comments on commit 732a5fe

Please sign in to comment.