Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -101,7 +101,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="course sequence tabs"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we have a translation for this label?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

translations have been added

>
{renderPreviousButton()}
{renderUnitButtons()}
{renderNextButton()}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import React from 'react';
import { Factory } from 'rosie';
import { useWindowSize, breakpoints } from '@openedx/paragon';

import {
render, screen, fireEvent, getByText, initializeTestStore,
} from '../../../../setupTest';
Expand All @@ -10,6 +12,15 @@ import useIndexOfLastVisibleChild from '../../../../generic/tabs/useIndexOfLastV
jest.mock('../../../../generic/tabs/useIndexOfLastVisibleChild');
useIndexOfLastVisibleChild.mockReturnValue([0, null, null]);

jest.mock('@openedx/paragon', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we abandon these mocks and test real functionality?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

const original = jest.requireActual('@openedx/paragon');
return {
...original,
breakpoints: original.breakpoints,
useWindowSize: jest.fn(),
};
});

describe('Sequence Navigation', () => {
let mockData;
const courseMetadata = Factory.build('courseMetadata');
Expand All @@ -29,6 +40,7 @@ describe('Sequence Navigation', () => {
onNavigate: () => {},
nextHandler: () => {},
};
useWindowSize.mockReturnValue({ width: 1024, height: 800 });
});

it('is empty while loading', async () => {
Expand Down Expand Up @@ -76,7 +88,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 +221,22 @@ describe('Sequence Navigation', () => {
);
expect(screen.queryByRole('link', { name: /next/i })).not.toBeInTheDocument();
});

it('shows previous button without label when screen is small', () => {
useWindowSize.mockReturnValue({ width: breakpoints.small.minWidth - 1 });

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

const prevButton = screen.getByRole('button');
expect(prevButton.textContent).toBe('2 of 3');
});

it('does not set width when screen is large', () => {
useWindowSize.mockReturnValue({ width: breakpoints.small.minWidth });

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

const nav = screen.getByRole('navigation', { name: /course sequence tabs/i });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you add a translation for this text, it can be used here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

translations have been added too

expect(nav).not.toHaveStyle({ width: '90%' });
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,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 @@ -72,7 +72,7 @@ describe('Sequence Navigation Dropdown', () => {
fireEvent.click(dropdownToggle);
});
const dropdownMenu = container.querySelector('.dropdown-menu');
getAllByRole(dropdownMenu, 'link', { hidden: true }).forEach(button => fireEvent.click(button));
getAllByRole(dropdownMenu, 'tab', { hidden: true }).forEach(button => fireEvent.click(button));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use userEvent here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

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
Expand Up @@ -5,9 +5,22 @@ import {
} from '../../../../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 +46,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 @@ -79,7 +92,39 @@ describe('Unit Button', () => {
it('handles the click', () => {
const onClick = jest.fn();
render(<UnitButton {...mockData} onClick={onClick} />, { wrapWithRouter: true });
fireEvent.click(screen.getByRole('link'));
fireEvent.click(screen.getByRole('tab'));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use userEvent here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

expect(onClick).toHaveBeenCalledTimes(1);
});

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

fireEvent.click(screen.getByRole('tab'));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use userEvent here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed


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/'));
});
});