diff --git a/src/data/constants/app.js b/src/data/constants/app.js index 58e11be6..cd6236f7 100644 --- a/src/data/constants/app.js +++ b/src/data/constants/app.js @@ -1,4 +1,4 @@ import { getConfig } from '@edx/frontend-platform'; export const routePath = () => `${getConfig().PUBLIC_PATH}:courseId`; -export const locationId = () => window.location.pathname.replace(getConfig().PUBLIC_PATH, ''); +export const locationId = () => decodeURIComponent(window.location.pathname).replace(getConfig().PUBLIC_PATH, ''); diff --git a/src/data/constants/app.test.js b/src/data/constants/app.test.js index b36a0e47..21908022 100644 --- a/src/data/constants/app.test.js +++ b/src/data/constants/app.test.js @@ -15,10 +15,29 @@ describe('app constants', () => { test('route path draws from public path and adds courseId', () => { expect(constants.routePath()).toEqual(`${platform.PUBLIC_PATH}:courseId`); }); - test('locationId returns trimmed pathname', () => { - const old = window.location; - window.location = { pathName: `${platform.PUBLIC_PATH}somePath.jpg` }; - expect(constants.locationId()).toEqual(window.location.pathname.replace(platform.PUBLIC_PATH, '')); - window.location = old; + describe('locationId', () => { + const domain = 'https://example.com'; + + test('returns trimmed pathname', () => { + const old = window.location; + Object.defineProperty(window, 'location', { + value: new URL(`${domain}${platform.PUBLIC_PATH}somePath.jpg`), + writable: true, + }); + expect(constants.locationId()).toEqual(window.location.pathname.replace(platform.PUBLIC_PATH, '')); + window.location = old; + }); + test('handle none-standard characters pathName', () => { + const old = window.location; + const noneStandardPath = `${platform.PUBLIC_PATH}ORG+%C4%90+2023`; + const expectedPath = `${platform.PUBLIC_PATH}ORG+Đ+2023`; + Object.defineProperty(window, 'location', { + value: new URL(`${domain}${noneStandardPath}`), + writable: true, + }); + expect(noneStandardPath).not.toEqual(expectedPath); + expect(constants.locationId()).toEqual(expectedPath.replace(platform.PUBLIC_PATH, '')); + window.location = old; + }); }); });