Skip to content

Commit

Permalink
[BACKPORT] fix: internal routing ignores public path (#313)
Browse files Browse the repository at this point in the history
* 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
dyudyunov authored Mar 29, 2024
1 parent c3e2599 commit 4795361
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/components/ProgramRecord/ProgramRecord.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import ProgramRecordTable from './ProgramRecordTable';
import RecordsHelp from './RecordsHelp';
import ProgramRecordAlert from '../ProgramRecordAlert';
import SendLearnerRecordModal from '../ProgramRecordSendModal';
import createCorrectInternalRoute from '../../utils';

import getProgramDetails from './data/service';

Expand Down Expand Up @@ -81,7 +82,7 @@ function ProgramRecord({ isPublic }) {
className="back-to-records"
>
<Hyperlink
destination="/"
destination={createCorrectInternalRoute('/')}
variant="muted"
>
<FormattedMessage
Expand Down
7 changes: 6 additions & 1 deletion src/components/ProgramRecordsList/ProgramRecordsList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import _ from 'lodash';

import NavigationBar from '../NavigationBar/NavigationBar';

import createCorrectInternalRoute from '../../utils';
import getProgramRecords from './data/service';

function ProgramRecordsList() {
Expand Down Expand Up @@ -115,7 +116,11 @@ function ProgramRecordsList() {
<div className="d-flex align-items-center pt-3 pt-lg-0">
<Hyperlink
variant="muted"
destination={getConfig().USE_LR_MFE ? `/${record.uuid}` : `${getConfig().CREDENTIALS_BASE_URL}/records/programs/${record.uuid}/`}
destination={
getConfig().USE_LR_MFE
? createCorrectInternalRoute(`/${record.uuid}`)
: `${getConfig().CREDENTIALS_BASE_URL}/records/programs/${record.uuid}/`
}
>
<Button variant="outline-primary">
<FormattedMessage
Expand Down
20 changes: 20 additions & 0 deletions src/utils.js
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;
}
43 changes: 43 additions & 0 deletions src/utils.test.js
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');
});
});
});

0 comments on commit 4795361

Please sign in to comment.