generated from openedx/frontend-template-application
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BACKPORT] fix: internal routing ignores public path (#313)
* fix: internal routing ignores public path Add a new util function for constructing the correct internal route URL. * test: add tests for createCorrectInternalRoute
- Loading branch information
Showing
4 changed files
with
71 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { getConfig } from '@edx/frontend-platform'; | ||
|
||
/** | ||
* Create a correct inner path depend on config PUBLIC_PATH. | ||
* @param {string} checkPath - the internal route path that is validated | ||
* @returns {string} - the correct internal route path | ||
*/ | ||
export default function createCorrectInternalRoute(checkPath) { | ||
let basePath = getConfig().PUBLIC_PATH; | ||
|
||
if (basePath.endsWith('/')) { | ||
basePath = basePath.slice(0, -1); | ||
} | ||
|
||
if (!checkPath.startsWith(basePath)) { | ||
return `${basePath}${checkPath}`; | ||
} | ||
|
||
return checkPath; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { getConfig } from '@edx/frontend-platform'; | ||
|
||
import createCorrectInternalRoute from './utils'; | ||
|
||
jest.mock('@edx/frontend-platform', () => ({ | ||
getConfig: jest.fn(), | ||
ensureConfig: jest.fn(), | ||
})); | ||
|
||
describe('LearnerRecord utils', () => { | ||
describe('createCorrectInternalRoute', () => { | ||
beforeEach(() => { | ||
getConfig.mockReset(); | ||
}); | ||
|
||
it('returns the correct internal route when checkPath is not prefixed with basePath', () => { | ||
getConfig.mockReturnValue({ PUBLIC_PATH: '/' }); | ||
|
||
const checkPath = '/some/path'; | ||
const result = createCorrectInternalRoute(checkPath); | ||
|
||
expect(result).toBe('/some/path'); | ||
}); | ||
|
||
it('returns the input checkPath when it is already prefixed with basePath', () => { | ||
getConfig.mockReturnValue({ PUBLIC_PATH: '/learner-record' }); | ||
|
||
const checkPath = '/learner-record/some/path'; | ||
const result = createCorrectInternalRoute(checkPath); | ||
|
||
expect(result).toBe('/learner-record/some/path'); | ||
}); | ||
|
||
it('handles basePath ending with a slash correctly', () => { | ||
getConfig.mockReturnValue({ PUBLIC_PATH: '/learner-record/' }); | ||
|
||
const checkPath = '/some/path'; | ||
const result = createCorrectInternalRoute(checkPath); | ||
|
||
expect(result).toBe('/learner-record/some/path'); | ||
}); | ||
}); | ||
}); |