Skip to content

Commit cf4bea3

Browse files
KristinAokifeanil
authored andcommitted
fix: unit link in preview mode
1 parent 85e6e92 commit cf4bea3

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

src/courseware/course/sidebar/sidebars/course-outline/components/SidebarUnit.test.jsx

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ describe('<SidebarUnit />', () => {
3636
};
3737
};
3838

39-
function renderWithProvider(props = {}, sidebarContext = defaultSidebarContext) {
39+
function renderWithProvider(props = {}, sidebarContext = defaultSidebarContext, pathname = '/course') {
4040
const { container } = render(
4141
<AppProvider store={store} wrapWithRouter={false}>
4242
<IntlProvider locale="en">
4343
<SidebarContext.Provider value={{ ...sidebarContext }}>
44-
<MemoryRouter>
44+
<MemoryRouter initialEntries={[{ pathname }]}>
4545
<SidebarUnit
4646
isFirst
4747
id={unit.id}
@@ -138,4 +138,34 @@ describe('<SidebarUnit />', () => {
138138
expect(window.sessionStorage.getItem('hideCourseOutlineSidebar')).toEqual('true');
139139
});
140140
});
141+
142+
describe('UnitLinkWrapper', () => {
143+
describe('course in preview mode', () => {
144+
beforeEach(async () => {
145+
await initTestStore();
146+
renderWithProvider({ unit: { ...unit } }, { ...defaultSidebarContext, shouldDisplayFullScreen: true }, '/preview/course');
147+
});
148+
149+
it('href includes /preview', async () => {
150+
const unitLink = screen.getByText(unit.title).closest('a');
151+
const linkHref = unitLink.getAttribute('href');
152+
153+
expect(linkHref.includes('/preview/')).toBeTruthy();
154+
});
155+
});
156+
157+
describe('course in live mode', () => {
158+
beforeEach(async () => {
159+
await initTestStore();
160+
renderWithProvider({ unit: { ...unit } }, { ...defaultSidebarContext, shouldDisplayFullScreen: true });
161+
});
162+
163+
it('href does not include /preview/', async () => {
164+
const unitLink = screen.getByText(unit.title).closest('a');
165+
const linkHref = unitLink.getAttribute('href');
166+
167+
expect(linkHref.includes('/preview/')).toBeFalsy();
168+
});
169+
});
170+
});
141171
});

src/courseware/course/sidebar/sidebars/course-outline/components/UnitLinkWrapper.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { Link } from 'react-router-dom';
2+
import { Link, useLocation } from 'react-router-dom';
33

44
import { useCourseOutlineSidebar } from '../hooks';
55

@@ -27,10 +27,14 @@ const UnitLinkWrapper: React.FC<Props> = ({
2727
children,
2828
}) => {
2929
const { handleUnitClick } = useCourseOutlineSidebar();
30+
const { pathname } = useLocation();
31+
const isPreview = pathname.startsWith('/preview');
32+
const baseUrl = `/course/${courseId}/${sequenceId}/${id}`;
33+
const link = isPreview ? `/preview${baseUrl}` : baseUrl;
3034

3135
return (
3236
<Link
33-
to={`/course/${courseId}/${sequenceId}/${id}`}
37+
to={link}
3438
className="row w-100 m-0 d-flex align-items-center text-gray-700"
3539
onClick={() => handleUnitClick({ sequenceId, activeUnitId, id })}
3640
>

0 commit comments

Comments
 (0)