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 f8f4504
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)

This comment has been minimized.

Copy link
@alexvuong

alexvuong Mar 23, 2023

Collaborator

Very interesting way to mock an original function. How does it differ from the line below? 🤔

removeSiteLocaleFromPath: jest.fn()

This comment has been minimized.

Copy link
@sandragolden

sandragolden Mar 23, 2023

Author Contributor

it doesn't. this test was failing on the navigate back to the account page where removeSiteLocaleFromPath is called.

The chain ultimately goes:
removeSiteLocaleFromPath -> getParamsFromPath -> absoluteUrl -> getAppOrigin

which ultimately breaks here with TypeError: Cannot read properties of null (reading '_location')

const getAppOrigin = () => {
  if (typeof window !== 'undefined') {
    return window.location.origin;
  }
  ...
}

no amount of mocking any of the above functions would work. I'm open to ideas. 🤷‍♀️

this is a minor bug that i'm trying to get in to support hybrid deployments and just trying to clear this failing test.

This comment has been minimized.

Copy link
@sandragolden

sandragolden Mar 23, 2023

Author Contributor

updated

}
})

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 f8f4504

Please sign in to comment.