Skip to content
Draft
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
1 change: 1 addition & 0 deletions src/course-home/live-tab/LiveTab.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const LiveTab = () => {
return (
<div
id="live_tab"
role="region"
// eslint-disable-next-line react/no-danger
dangerouslySetInnerHTML={{ __html: liveModel[courseId]?.iframe }}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ const SequenceNavigation = ({
} else if (!shouldDisplayNotificationTriggerInSequence) {
buttonText = intl.formatMessage(messages.nextButton);
}

return navigationDisabledNextSequence || (
<NextUnitTopNavTriggerSlot
{...{
Expand All @@ -101,7 +102,13 @@ const SequenceNavigation = ({
};

return sequenceStatus === LOADED ? (
<nav id="courseware-sequence-navigation" data-testid="courseware-sequence-navigation" className={classNames('sequence-navigation', className, { 'mr-2': shouldDisplayNotificationTriggerInSequence })}>
<nav
id="courseware-sequence-navigation"
data-testid="courseware-sequence-navigation"
className={classNames('sequence-navigation', className, { 'mr-2': shouldDisplayNotificationTriggerInSequence })}
style={{ width: shouldDisplayNotificationTriggerInSequence ? '90%' : null }}
aria-label={intl.formatMessage(messages.sequenceNavLabel)}
>
{renderPreviousButton()}
{renderUnitButtons()}
{renderNextButton()}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import React from 'react';
import { Factory } from 'rosie';
import { breakpoints } from '@openedx/paragon';

import {
render, screen, fireEvent, getByText, initializeTestStore,
} from '../../../../setupTest';
import SequenceNavigation from './SequenceNavigation';
import useIndexOfLastVisibleChild from '../../../../generic/tabs/useIndexOfLastVisibleChild';
import messages from './messages';

// Mock the hook to avoid relying on its implementation and mocking `getBoundingClientRect`.
jest.mock('../../../../generic/tabs/useIndexOfLastVisibleChild');
Expand All @@ -29,6 +32,19 @@ describe('Sequence Navigation', () => {
onNavigate: () => {},
nextHandler: () => {},
};
Object.defineProperty(window, 'innerWidth', {
writable: true,
configurable: true,
value: 1024,
});
});

afterEach(() => {
Object.defineProperty(window, 'innerWidth', {
writable: true,
configurable: true,
value: 1024,
});
});

it('is empty while loading', async () => {
Expand Down Expand Up @@ -76,7 +92,7 @@ describe('Sequence Navigation', () => {
const onNavigate = jest.fn();
render(<SequenceNavigation {...mockData} {...{ onNavigate }} />, { wrapWithRouter: true });

const unitButtons = screen.getAllByRole('link', { name: /\d+/ });
const unitButtons = screen.getAllByRole('tab', { name: /\d+/ });
expect(unitButtons).toHaveLength(unitButtons.length);
unitButtons.forEach(button => fireEvent.click(button));
expect(onNavigate).toHaveBeenCalledTimes(unitButtons.length);
Expand Down Expand Up @@ -209,4 +225,30 @@ describe('Sequence Navigation', () => {
);
expect(screen.queryByRole('link', { name: /next/i })).not.toBeInTheDocument();
});

it('shows previous button without label when screen is small', () => {
Object.defineProperty(window, 'innerWidth', {
writable: true,
configurable: true,
value: breakpoints.small.minWidth - 1,
});

const { container } = render(<SequenceNavigation {...mockData} />, { wrapWithRouter: true });

const prevButton = container.querySelector('.previous-btn');
expect(prevButton).toBeInTheDocument();
expect(screen.queryByText(messages.previousButton.defaultMessage)).not.toBeInTheDocument();
});

it('does not set width when screen is large', () => {
Object.defineProperty(window, 'innerWidth', {
writable: true,
configurable: true,
value: breakpoints.small.minWidth,
});
render(<SequenceNavigation {...mockData} />, { wrapWithRouter: true });

const nav = screen.getByRole('navigation', { name: messages.sequenceNavLabel.defaultMessage });
expect(nav).not.toHaveStyle({ width: '90%' });
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import React from 'react';
import { Factory } from 'rosie';
import { getAllByRole } from '@testing-library/dom';
import { act } from '@testing-library/react';
import userEvent from '@testing-library/user-event';

import SequenceNavigationDropdown from './SequenceNavigationDropdown';
import {
render, screen, fireEvent, initializeTestStore,
Expand Down Expand Up @@ -50,7 +52,7 @@ describe('Sequence Navigation Dropdown', () => {
});
const dropdownMenu = container.querySelector('.dropdown-menu');
// Only the current unit should be marked as active.
getAllByRole(dropdownMenu, 'link', { hidden: true }).forEach(button => {
getAllByRole(dropdownMenu, 'tab', { hidden: true }).forEach(button => {
if (button.textContent === unit.display_name) {
expect(button).toHaveClass('active');
} else {
Expand All @@ -60,7 +62,7 @@ describe('Sequence Navigation Dropdown', () => {
});
});

it('handles the clicks', () => {
it('handles the clicks', async () => {
const onNavigate = jest.fn();
const { container } = render(
<SequenceNavigationDropdown {...mockData} onNavigate={onNavigate} />,
Expand All @@ -72,7 +74,13 @@ describe('Sequence Navigation Dropdown', () => {
fireEvent.click(dropdownToggle);
});
const dropdownMenu = container.querySelector('.dropdown-menu');
getAllByRole(dropdownMenu, 'link', { hidden: true }).forEach(button => fireEvent.click(button));
const buttons = getAllByRole(dropdownMenu, 'tab', { hidden: true });

for (const button of buttons) {
// eslint-disable-next-line no-await-in-loop
await userEvent.click(button);
}

expect(onNavigate).toHaveBeenCalledTimes(unitBlocks.length);
unitBlocks.forEach((unit, index) => {
expect(onNavigate).toHaveBeenNthCalledWith(index + 1, unit.id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const SequenceNavigationTabs = ({
<div
className="sequence-navigation-tabs d-flex flex-grow-1"
style={shouldDisplayDropdown ? invisibleStyle : null}
role="tablist"
ref={containerRef}
>
{unitIds.map(buttonUnitId => (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
import React from 'react';
import { Factory } from 'rosie';
import { getAllByRole } from '@testing-library/react';
import userEvent from '@testing-library/user-event';

import { initializeTestStore, render, screen } from '../../../../setupTest';
import SequenceNavigationTabs from './SequenceNavigationTabs';
import useIndexOfLastVisibleChild from '../../../../generic/tabs/useIndexOfLastVisibleChild';
import {
useIsSidebarOpen,
useIsOnXLDesktop,
useIsOnLargeDesktop,
useIsOnMediumDesktop,
} from './hooks';

// Mock the hook to avoid relying on its implementation and mocking `getBoundingClientRect`.
jest.mock('../../../../generic/tabs/useIndexOfLastVisibleChild');

jest.mock('./hooks', () => ({
useIsSidebarOpen: jest.fn(),
useIsOnXLDesktop: jest.fn(),
useIsOnLargeDesktop: jest.fn(),
useIsOnMediumDesktop: jest.fn(),
}));

describe('Sequence Navigation Tabs', () => {
let mockData;

Expand Down Expand Up @@ -44,7 +56,7 @@ describe('Sequence Navigation Tabs', () => {
useIndexOfLastVisibleChild.mockReturnValue([0, null, null]);
render(<SequenceNavigationTabs {...mockData} />, { wrapWithRouter: true });

expect(screen.getAllByRole('link')).toHaveLength(unitBlocks.length);
expect(screen.getAllByRole('tab')).toHaveLength(unitBlocks.length);
});

it('renders unit buttons and dropdown button', async () => {
Expand All @@ -54,17 +66,85 @@ describe('Sequence Navigation Tabs', () => {
const booyah = render(<SequenceNavigationTabs {...mockData} />, { wrapWithRouter: true });

// wait for links to appear so we aren't testing an empty div
await screen.findAllByRole('link');
await screen.findAllByRole('tab');

container = booyah.container;

const dropdownToggle = container.querySelector('.dropdown-toggle');
await userEvent.click(dropdownToggle);

const dropdownMenu = container.querySelector('.dropdown');
const dropdownButtons = getAllByRole(dropdownMenu, 'link');
const dropdownButtons = getAllByRole(dropdownMenu, 'tab');
expect(dropdownButtons).toHaveLength(unitBlocks.length);
expect(screen.getByRole('button', { name: `${activeBlockNumber} of ${unitBlocks.length}` }))
.toHaveClass('dropdown-toggle');
});

it('adds class navigation-tab-width-xl when isOnXLDesktop and sidebar is open', () => {
useIsSidebarOpen.mockReturnValue(true);
useIsOnXLDesktop.mockReturnValue(true);
useIsOnLargeDesktop.mockReturnValue(false);
useIsOnMediumDesktop.mockReturnValue(false);
useIndexOfLastVisibleChild.mockReturnValue([0, null, null]);

const { container } = render(<SequenceNavigationTabs {...mockData} />, { wrapWithRouter: true });

const wrapperDiv = container.querySelector('.navigation-tab-width-xl');
expect(wrapperDiv).toBeInTheDocument();
});

it('adds class navigation-tab-width-large when isOnLargeDesktop and sidebar is open', () => {
useIsSidebarOpen.mockReturnValue(true);
useIsOnXLDesktop.mockReturnValue(false);
useIsOnLargeDesktop.mockReturnValue(true);
useIsOnMediumDesktop.mockReturnValue(false);
useIndexOfLastVisibleChild.mockReturnValue([0, null, null]);

const { container } = render(<SequenceNavigationTabs {...mockData} />, { wrapWithRouter: true });

const wrapperDiv = container.querySelector('.navigation-tab-width-large');
expect(wrapperDiv).toBeInTheDocument();
});

it('adds class navigation-tab-width-medium when isOnMediumDesktop and sidebar is open', () => {
useIsSidebarOpen.mockReturnValue(true);
useIsOnXLDesktop.mockReturnValue(false);
useIsOnLargeDesktop.mockReturnValue(false);
useIsOnMediumDesktop.mockReturnValue(true);
useIndexOfLastVisibleChild.mockReturnValue([0, null, null]);

const { container } = render(<SequenceNavigationTabs {...mockData} />, { wrapWithRouter: true });

const wrapperDiv = container.querySelector('.navigation-tab-width-medium');
expect(wrapperDiv).toBeInTheDocument();
});

it('applies invisibleStyle when shouldDisplayDropdown is true', () => {
useIsSidebarOpen.mockReturnValue(true);
useIsOnXLDesktop.mockReturnValue(false);
useIsOnLargeDesktop.mockReturnValue(false);
useIsOnMediumDesktop.mockReturnValue(false);

useIndexOfLastVisibleChild.mockReturnValue([
0,
null,
{
position: 'absolute',
left: '0px',
pointerEvents: 'none',
visibility: 'hidden',
maxWidth: '100%',
},
]);

render(<SequenceNavigationTabs {...mockData} />, { wrapWithRouter: true });

const tabList = screen.getByRole('tablist');

expect(tabList.style.position).toBe('absolute');
expect(tabList.style.left).toBe('0px');
expect(tabList.style.pointerEvents).toBe('none');
expect(tabList.style.visibility).toBe('hidden');
expect(tabList.style.maxWidth).toBe('100%');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ const UnitButton = ({
variant="link"
onClick={handleClick}
title={title}
role="tab"
aria-selected={isActive}
aria-controls={title}
as={Link}
to={unitPath}
>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
import React from 'react';
import { Factory } from 'rosie';
import userEvent from '@testing-library/user-event';

import {
fireEvent, initializeTestStore, render, screen,
} from '../../../../setupTest';
initializeTestStore, render, screen,
} from '@src/setupTest';
import UnitButton from './UnitButton';

jest.mock('react-router-dom', () => {
const actual = jest.requireActual('react-router-dom');
return {
...actual,
useLocation: () => ({ pathname: '/preview/anything' }),
};
});

describe('Unit Button', () => {
let mockData;
const courseMetadata = Factory.build('courseMetadata');

afterEach(() => {
jest.resetModules();
});

const unitBlocks = [Factory.build(
'block',
{ type: 'problem' },
Expand All @@ -33,12 +48,12 @@ describe('Unit Button', () => {

it('hides title by default', () => {
render(<UnitButton {...mockData} />, { wrapWithRouter: true });
expect(screen.getByRole('link')).not.toHaveTextContent(unit.display_name);
expect(screen.getByRole('tab')).not.toHaveTextContent(unit.display_name);
});

it('shows title', () => {
render(<UnitButton {...mockData} showTitle />, { wrapWithRouter: true });
expect(screen.getByRole('link')).toHaveTextContent(unit.display_name);
expect(screen.getByRole('tab')).toHaveTextContent(unit.display_name);
});

it('does not show completion for non-completed unit', () => {
Expand Down Expand Up @@ -76,10 +91,42 @@ describe('Unit Button', () => {
expect(bookmarkIcon.getAttribute('data-testid')).toBe('bookmark-icon');
});

it('handles the click', () => {
it('handles the click', async () => {
const onClick = jest.fn();
render(<UnitButton {...mockData} onClick={onClick} />, { wrapWithRouter: true });
fireEvent.click(screen.getByRole('link'));
await userEvent.click(screen.getByRole('tab'));
expect(onClick).toHaveBeenCalledTimes(1);
});

it('calls onClick with correct unitId when clicked', async () => {
const onClick = jest.fn();
render(<UnitButton {...mockData} onClick={onClick} />, { wrapWithRouter: true });

await userEvent.click(screen.getByRole('tab'));

expect(onClick).toHaveBeenCalledWith(mockData.unitId);
});

it('renders with no unitId and does not crash', async () => {
render(<UnitButton unitId={undefined} onClick={jest.fn()} contentType="video" title="Unit" />, {
wrapWithRouter: true,
});

expect(screen.getByRole('tab')).toBeInTheDocument();
});

it('prepends /preview to the unit path if in preview mode', () => {
const onClick = jest.fn();

render(
<UnitButton {...mockData} onClick={onClick} />,
{
wrapWithRouter: true,
initialEntries: ['/preview/some/path'],
},
);

const button = screen.getByRole('tab');
expect(button.closest('a')).toHaveAttribute('href', expect.stringContaining('/preview/course/'));
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ const messages = defineMessages({
defaultMessage: 'Previous',
description: 'Button to return to the previous section',
},
sequenceNavLabel: {
id: 'learn.sequence.navigation.aria.label',
defaultMessage: 'Course sequence tabs',
description: 'Accessibility label for the courseware sequence navigation bar',
},
});

export default messages;