Skip to content

Commit 2235737

Browse files
Agrendalathbradenmacdonald
authored andcommitted
feat: close sidebar on mobile after selecting a unit
1 parent fca32ae commit 2235737

File tree

2 files changed

+49
-19
lines changed

2 files changed

+49
-19
lines changed

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

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { sendTrackEvent, sendTrackingLogEvent } from '@edx/frontend-platform/ana
88
import { initializeMockApp, initializeTestStore } from '@src/setupTest';
99
import SidebarContext from '../../../SidebarContext';
1010
import SidebarUnit from './SidebarUnit';
11+
import { ID } from '../constants';
1112

1213
jest.mock('@edx/frontend-platform/analytics', () => ({
1314
sendTrackEvent: jest.fn(),
@@ -20,7 +21,7 @@ describe('<SidebarUnit />', () => {
2021
let store = {};
2122
let unit;
2223
let sequenceId;
23-
let mockData;
24+
let defaultSidebarContext;
2425

2526
const initTestStore = async (options) => {
2627
store = await initializeTestStore(options);
@@ -29,16 +30,17 @@ describe('<SidebarUnit />', () => {
2930
const sequence = state.courseware.courseOutline.sequences[sequenceId];
3031
unit = state.courseware.courseOutline.units[sequence.unitIds[0]];
3132

32-
mockData = {
33+
defaultSidebarContext = {
3334
toggleSidebar: jest.fn(),
35+
currentSidebar: ID,
3436
};
3537
};
3638

37-
function renderWithProvider(props = {}) {
39+
function renderWithProvider(props = {}, sidebarContext = defaultSidebarContext) {
3840
const { container } = render(
3941
<AppProvider store={store} wrapWithRouter={false}>
4042
<IntlProvider locale="en">
41-
<SidebarContext.Provider value={{ ...mockData }}>
43+
<SidebarContext.Provider value={{ ...sidebarContext }}>
4244
<MemoryRouter>
4345
<SidebarUnit
4446
isFirst
@@ -95,22 +97,45 @@ describe('<SidebarUnit />', () => {
9597
expect(screen.getByText(unit.title)).toBeInTheDocument();
9698
});
9799

98-
it('sends log event correctly when unit is clicked', async () => {
99-
const user = userEvent.setup();
100-
await initTestStore();
101-
renderWithProvider({ unit: { ...unit } });
102-
const logData = {
103-
id: unit.id,
104-
current_tab: 1,
105-
tab_count: 1,
106-
target_id: unit.id,
107-
target_tab: 1,
108-
widget_placement: 'left',
109-
};
100+
describe('When a unit is clicked', () => {
101+
it('sends log event correctly', async () => {
102+
const user = userEvent.setup();
103+
await initTestStore();
104+
renderWithProvider({ unit: { ...unit } });
105+
const logData = {
106+
id: unit.id,
107+
current_tab: 1,
108+
tab_count: 1,
109+
target_id: unit.id,
110+
target_tab: 1,
111+
widget_placement: 'left',
112+
};
113+
114+
await user.click(screen.getByText(unit.title));
115+
116+
expect(sendTrackEvent).toHaveBeenCalledWith('edx.ui.lms.sequence.tab_selected', logData);
117+
expect(sendTrackingLogEvent).toHaveBeenCalledWith('edx.ui.lms.sequence.tab_selected', logData);
118+
});
110119

111-
await user.click(screen.getByText(unit.title));
120+
it('leaves sidebar open in desktop mode', async () => {
121+
const user = userEvent.setup();
122+
await initTestStore();
123+
renderWithProvider({ unit: { ...unit } });
124+
await user.click(screen.getByText(unit.title));
112125

113-
expect(sendTrackEvent).toHaveBeenCalledWith('edx.ui.lms.sequence.tab_selected', logData);
114-
expect(sendTrackingLogEvent).toHaveBeenCalledWith('edx.ui.lms.sequence.tab_selected', logData);
126+
expect(defaultSidebarContext.toggleSidebar).not.toHaveBeenCalled();
127+
expect(window.sessionStorage.getItem('hideCourseOutlineSidebar')).toBeNull();
128+
});
129+
130+
it('closes sidebar on mobile devices', async () => {
131+
const user = userEvent.setup();
132+
await initTestStore();
133+
renderWithProvider({ unit: { ...unit } }, { ...defaultSidebarContext, shouldDisplayFullScreen: true });
134+
await user.click(screen.getByText(unit.title));
135+
136+
expect(defaultSidebarContext.toggleSidebar).toHaveBeenCalledTimes(1);
137+
expect(defaultSidebarContext.toggleSidebar).toHaveBeenCalledWith(null);
138+
expect(window.sessionStorage.getItem('hideCourseOutlineSidebar')).toEqual('true');
139+
});
115140
});
116141
});

src/courseware/course/sidebar/sidebars/course-outline/hooks.jsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ export const useCourseOutlineSidebar = () => {
8585

8686
logEvent('edx.ui.lms.sequence.tab_selected', 'left');
8787
dispatch(checkBlockCompletion(courseId, sequenceId, activeUnitId));
88+
89+
// Hide the sidebar after selecting a unit on a mobile device.
90+
if (shouldDisplayFullScreen) {
91+
handleToggleCollapse();
92+
}
8893
};
8994

9095
useEffect(() => {

0 commit comments

Comments
 (0)