Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: close sidebar on mobile after selecting a unit #1583

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { sendTrackEvent, sendTrackingLogEvent } from '@edx/frontend-platform/ana
import { initializeMockApp, initializeTestStore } from '@src/setupTest';
import SidebarContext from '../../../SidebarContext';
import SidebarUnit from './SidebarUnit';
import { ID } from '../constants';

jest.mock('@edx/frontend-platform/analytics', () => ({
sendTrackEvent: jest.fn(),
Expand All @@ -20,7 +21,7 @@ describe('<SidebarUnit />', () => {
let store = {};
let unit;
let sequenceId;
let mockData;
let defaultSidebarContext;

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

mockData = {
defaultSidebarContext = {
toggleSidebar: jest.fn(),
currentSidebar: ID,
};
};

function renderWithProvider(props = {}) {
function renderWithProvider(props = {}, sidebarContext = defaultSidebarContext) {
const { container } = render(
<AppProvider store={store} wrapWithRouter={false}>
<IntlProvider locale="en">
<SidebarContext.Provider value={{ ...mockData }}>
<SidebarContext.Provider value={{ ...sidebarContext }}>
<MemoryRouter>
<SidebarUnit
isFirst
Expand Down Expand Up @@ -95,22 +97,45 @@ describe('<SidebarUnit />', () => {
expect(screen.getByText(unit.title)).toBeInTheDocument();
});

it('sends log event correctly when unit is clicked', async () => {
const user = userEvent.setup();
await initTestStore();
renderWithProvider({ unit: { ...unit } });
const logData = {
id: unit.id,
current_tab: 1,
tab_count: 1,
target_id: unit.id,
target_tab: 1,
widget_placement: 'left',
};
describe('When a unit is clicked', () => {
it('sends log event correctly', async () => {
const user = userEvent.setup();
await initTestStore();
renderWithProvider({ unit: { ...unit } });
const logData = {
id: unit.id,
current_tab: 1,
tab_count: 1,
target_id: unit.id,
target_tab: 1,
widget_placement: 'left',
};

await user.click(screen.getByText(unit.title));

expect(sendTrackEvent).toHaveBeenCalledWith('edx.ui.lms.sequence.tab_selected', logData);
expect(sendTrackingLogEvent).toHaveBeenCalledWith('edx.ui.lms.sequence.tab_selected', logData);
});

await user.click(screen.getByText(unit.title));
it('leaves sidebar open in desktop mode', async () => {
const user = userEvent.setup();
await initTestStore();
renderWithProvider({ unit: { ...unit } });
await user.click(screen.getByText(unit.title));

expect(sendTrackEvent).toHaveBeenCalledWith('edx.ui.lms.sequence.tab_selected', logData);
expect(sendTrackingLogEvent).toHaveBeenCalledWith('edx.ui.lms.sequence.tab_selected', logData);
expect(defaultSidebarContext.toggleSidebar).not.toHaveBeenCalled();
expect(window.sessionStorage.getItem('hideCourseOutlineSidebar')).toBeNull();
});

it('closes sidebar on mobile devices', async () => {
const user = userEvent.setup();
await initTestStore();
renderWithProvider({ unit: { ...unit } }, { ...defaultSidebarContext, shouldDisplayFullScreen: true });
await user.click(screen.getByText(unit.title));

expect(defaultSidebarContext.toggleSidebar).toHaveBeenCalledTimes(1);
expect(defaultSidebarContext.toggleSidebar).toHaveBeenCalledWith(null);
expect(window.sessionStorage.getItem('hideCourseOutlineSidebar')).toEqual('true');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ export const useCourseOutlineSidebar = () => {

logEvent('edx.ui.lms.sequence.tab_selected', 'left');
dispatch(checkBlockCompletion(courseId, sequenceId, activeUnitId));

// Hide the sidebar after selecting a unit on a mobile device.
if (shouldDisplayFullScreen) {
handleToggleCollapse();
}
};

useEffect(() => {
Expand Down