Skip to content

Commit efb1a28

Browse files
authored
fix: Expand all now expands subsections (#2085)
1 parent 1ff5e5b commit efb1a28

File tree

4 files changed

+29
-45
lines changed

4 files changed

+29
-45
lines changed

src/course-outline/CourseOutline.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@ const CourseOutline = ({ courseId }) => {
375375
section,
376376
section.childInfo.children,
377377
)}
378+
isSectionsExpanded={isSectionsExpanded}
378379
isSelfPaced={statusBarData.isSelfPaced}
379380
isCustomRelativeDatesActive={isCustomRelativeDatesActive}
380381
savingStatus={savingStatus}

src/course-outline/CourseOutline.test.jsx

Lines changed: 18 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import { cloneDeep } from 'lodash';
1111
import { closestCorners } from '@dnd-kit/core';
1212

1313
import { useLocation } from 'react-router-dom';
14-
import userEvent from '@testing-library/user-event';
1514
import {
1615
getCourseBestPracticesApiUrl,
1716
getCourseLaunchApiUrl,
@@ -289,13 +288,15 @@ describe('<CourseOutline />', () => {
289288
});
290289

291290
it('check that new section list is saved when dragged', async () => {
292-
const { findAllByRole } = render(<RootWrapper />);
293-
const courseBlockId = courseOutlineIndexMock.courseStructure.id;
291+
const { findAllByRole, findByTestId } = render(<RootWrapper />);
292+
const expandAllButton = await findByTestId('expand-collapse-all-button');
293+
fireEvent.click(expandAllButton);
294+
const [section] = store.getState().courseOutline.sectionsList;
294295
const sectionsDraggers = await findAllByRole('button', { name: 'Drag to reorder' });
295-
const draggableButton = sectionsDraggers[6];
296+
const draggableButton = sectionsDraggers[1];
296297

297298
axiosMock
298-
.onPut(getCourseBlockApiUrl(courseBlockId))
299+
.onPut(getCourseBlockApiUrl(section.id))
299300
.reply(200, { dummy: 'value' });
300301

301302
const section1 = store.getState().courseOutline.sectionsList[0].id;
@@ -314,13 +315,15 @@ describe('<CourseOutline />', () => {
314315
});
315316

316317
it('check section list is restored to original order when API call fails', async () => {
317-
const { findAllByRole } = render(<RootWrapper />);
318-
const courseBlockId = courseOutlineIndexMock.courseStructure.id;
318+
const { findAllByRole, findByTestId } = render(<RootWrapper />);
319+
const expandAllButton = await findByTestId('expand-collapse-all-button');
320+
fireEvent.click(expandAllButton);
321+
const [section] = store.getState().courseOutline.sectionsList;
319322
const sectionsDraggers = await findAllByRole('button', { name: 'Drag to reorder' });
320-
const draggableButton = sectionsDraggers[6];
323+
const draggableButton = sectionsDraggers[1];
321324

322325
axiosMock
323-
.onPut(getCourseBlockApiUrl(courseBlockId))
326+
.onPut(getCourseBlockApiUrl(section.id))
324327
.reply(500);
325328

326329
const section1 = store.getState().courseOutline.sectionsList[0].id;
@@ -395,8 +398,6 @@ describe('<CourseOutline />', () => {
395398
const { findAllByTestId } = render(<RootWrapper />);
396399
const [sectionElement] = await findAllByTestId('section-card');
397400
const [subsectionElement] = await within(sectionElement).findAllByTestId('subsection-card');
398-
const expandBtn = await within(subsectionElement).findByTestId('subsection-card-header__expanded-btn');
399-
fireEvent.click(expandBtn);
400401
const units = await within(subsectionElement).findAllByTestId('unit-card');
401402
expect(units.length).toBe(1);
402403

@@ -421,8 +422,6 @@ describe('<CourseOutline />', () => {
421422
render(<RootWrapper />);
422423
const [sectionElement] = await screen.findAllByTestId('section-card');
423424
const [subsectionElement] = await within(sectionElement).findAllByTestId('subsection-card');
424-
const expandBtn = await within(subsectionElement).findByTestId('subsection-card-header__expanded-btn');
425-
fireEvent.click(expandBtn);
426425
const units = await within(subsectionElement).findAllByTestId('unit-card');
427426
expect(units.length).toBe(1);
428427

@@ -646,8 +645,6 @@ describe('<CourseOutline />', () => {
646645
await checkEditTitle(section, subsectionElement, subsection, 'New subsection name', 'subsection');
647646

648647
// check unit
649-
const expandBtn = await within(subsectionElement).findByTestId('subsection-card-header__expanded-btn');
650-
fireEvent.click(expandBtn);
651648
const [unit] = subsection.childInfo.children;
652649
const [unitElement] = await within(subsectionElement).findAllByTestId('unit-card');
653650
await checkEditTitle(section, unitElement, unit, 'New unit name', 'unit');
@@ -660,8 +657,6 @@ describe('<CourseOutline />', () => {
660657
const [sectionElement] = await screen.findAllByTestId('section-card');
661658
const [subsection] = section.childInfo.children;
662659
const [subsectionElement] = await within(sectionElement).findAllByTestId('subsection-card');
663-
const expandBtn = await within(subsectionElement).findByTestId('subsection-card-header__expanded-btn');
664-
fireEvent.click(expandBtn);
665660
const [unit] = subsection.childInfo.children;
666661
const [unitElement] = await within(subsectionElement).findAllByTestId('unit-card');
667662

@@ -700,8 +695,6 @@ describe('<CourseOutline />', () => {
700695
const [sectionElement] = await findAllByTestId('section-card');
701696
const [subsection] = section.childInfo.children;
702697
const [subsectionElement] = await within(sectionElement).findAllByTestId('subsection-card');
703-
const expandBtn = await within(subsectionElement).findByTestId('subsection-card-header__expanded-btn');
704-
fireEvent.click(expandBtn);
705698
const [unit] = subsection.childInfo.children;
706699
const [unitElement] = await within(subsectionElement).findAllByTestId('unit-card');
707700

@@ -771,8 +764,6 @@ describe('<CourseOutline />', () => {
771764
const [sectionElement] = await findAllByTestId('section-card');
772765
const [subsection] = section.childInfo.children;
773766
const [subsectionElement] = await within(sectionElement).findAllByTestId('subsection-card');
774-
const expandBtn = await within(subsectionElement).findByTestId('subsection-card-header__expanded-btn');
775-
fireEvent.click(expandBtn);
776767
const [unit] = subsection.childInfo.children;
777768
const [unitElement] = await within(subsectionElement).findAllByTestId('unit-card');
778769

@@ -1481,8 +1472,6 @@ describe('<CourseOutline />', () => {
14811472

14821473
const [firstSection] = await findAllByTestId('section-card');
14831474
const [firstSubsection] = await within(firstSection).findAllByTestId('subsection-card');
1484-
const subsectionExpandButton = await within(firstSubsection).getByTestId('subsection-card-header__expanded-btn');
1485-
fireEvent.click(subsectionExpandButton);
14861475
const [firstUnit] = await within(firstSubsection).findAllByTestId('unit-card');
14871476
const unitDropdownButton = await within(firstUnit).findByTestId('unit-card-header__menu-button');
14881477

@@ -1842,8 +1831,6 @@ describe('<CourseOutline />', () => {
18421831
const [, sectionElement] = await findAllByTestId('section-card');
18431832
const [, subsection] = section.childInfo.children;
18441833
const [, subsectionElement] = await within(sectionElement).findAllByTestId('subsection-card');
1845-
const expandBtn = await within(subsectionElement).findByTestId('subsection-card-header__expanded-btn');
1846-
await act(async () => fireEvent.click(expandBtn));
18471834
const [, secondUnit] = subsection.childInfo.children;
18481835
const [, unitElement] = await within(subsectionElement).findAllByTestId('unit-card');
18491836

@@ -1883,8 +1870,6 @@ describe('<CourseOutline />', () => {
18831870
const [, sectionElement] = await findAllByTestId('section-card');
18841871
const [firstSubsection, subsection] = section.childInfo.children;
18851872
const [, subsectionElement] = await within(sectionElement).findAllByTestId('subsection-card');
1886-
const expandBtn = await within(subsectionElement).findByTestId('subsection-card-header__expanded-btn');
1887-
await act(async () => fireEvent.click(expandBtn));
18881873
const [unit] = subsection.childInfo.children;
18891874
const [unitElement] = await within(subsectionElement).findAllByTestId('unit-card');
18901875

@@ -1920,8 +1905,6 @@ describe('<CourseOutline />', () => {
19201905
const [subsection] = secondSection.childInfo.children;
19211906
const firstSectionLastSubsection = firstSection.childInfo.children[firstSection.childInfo.children.length - 1];
19221907
const [subsectionElement] = await within(sectionElement).findAllByTestId('subsection-card');
1923-
const expandBtn = await within(subsectionElement).findByTestId('subsection-card-header__expanded-btn');
1924-
await act(async () => fireEvent.click(expandBtn));
19251908
const [unit] = subsection.childInfo.children;
19261909
const [unitElement] = await within(subsectionElement).findAllByTestId('unit-card');
19271910

@@ -1966,8 +1949,6 @@ describe('<CourseOutline />', () => {
19661949
const [, sectionElement] = await findAllByTestId('section-card');
19671950
const [firstSubsection, subsection] = section.childInfo.children;
19681951
const [subsectionElement] = await within(sectionElement).findAllByTestId('subsection-card');
1969-
const expandBtn = await within(subsectionElement).findByTestId('subsection-card-header__expanded-btn');
1970-
await act(async () => fireEvent.click(expandBtn));
19711952
const lastUnitIdx = firstSubsection.childInfo.children.length - 1;
19721953
const unit = firstSubsection.childInfo.children[lastUnitIdx];
19731954
const unitElement = (await within(subsectionElement).findAllByTestId('unit-card'))[lastUnitIdx];
@@ -2005,8 +1986,6 @@ describe('<CourseOutline />', () => {
20051986
const secondSectionLastSubsection = secondSection.childInfo.children[lastSubIndex];
20061987
const thirdSectionFirstSubsection = thirdSection.childInfo.children[0];
20071988
const subsectionElement = (await within(sectionElement).findAllByTestId('subsection-card'))[lastSubIndex];
2008-
const expandBtn = await within(subsectionElement).findByTestId('subsection-card-header__expanded-btn');
2009-
await act(async () => fireEvent.click(expandBtn));
20101989
const lastUnitIdx = secondSectionLastSubsection.childInfo.children.length - 1;
20111990
const unit = secondSectionLastSubsection.childInfo.children[lastUnitIdx];
20121991
const unitElement = (await within(subsectionElement).findAllByTestId('unit-card'))[lastUnitIdx];
@@ -2051,8 +2030,6 @@ describe('<CourseOutline />', () => {
20512030
const sections = await findAllByTestId('section-card');
20522031
const [sectionElement] = sections;
20532032
const [subsectionElement] = await within(sectionElement).findAllByTestId('subsection-card');
2054-
const expandBtn = await within(subsectionElement).findByTestId('subsection-card-header__expanded-btn');
2055-
await act(async () => fireEvent.click(expandBtn));
20562033
// get first and only unit in the subsection
20572034
const [firstUnit] = await within(subsectionElement).findAllByTestId('unit-card');
20582035

@@ -2072,8 +2049,6 @@ describe('<CourseOutline />', () => {
20722049
const lastSection = sections[sections.length - 1];
20732050
// it has only one subsection
20742051
const [lastSubsectionElement] = await within(lastSection).findAllByTestId('subsection-card');
2075-
const lastExpandBtn = await within(lastSubsectionElement).findByTestId('subsection-card-header__expanded-btn');
2076-
await act(async () => fireEvent.click(lastExpandBtn));
20772052
// get last and the only unit in the subsection
20782053
const [lastUnit] = await within(lastSubsectionElement).findAllByTestId('unit-card');
20792054

@@ -2094,6 +2069,9 @@ describe('<CourseOutline />', () => {
20942069
const { findAllByTestId } = render(<RootWrapper />);
20952070

20962071
const [sectionElement] = await findAllByTestId('section-card');
2072+
const [subsectionElement] = await within(sectionElement).findAllByTestId('subsection-card');
2073+
const expandBtn = await within(subsectionElement).findByTestId('subsection-card-header__expanded-btn');
2074+
fireEvent.click(expandBtn);
20972075
const [section] = store.getState().courseOutline.sectionsList;
20982076
const subsectionsDraggers = within(sectionElement).getAllByRole('button', { name: 'Drag to reorder' });
20992077
const draggableButton = subsectionsDraggers[1];
@@ -2125,6 +2103,9 @@ describe('<CourseOutline />', () => {
21252103
const { findAllByTestId } = render(<RootWrapper />);
21262104

21272105
const [sectionElement] = await findAllByTestId('section-card');
2106+
const [subsectionElement] = await within(sectionElement).findAllByTestId('subsection-card');
2107+
const expandBtn = await within(subsectionElement).findByTestId('subsection-card-header__expanded-btn');
2108+
fireEvent.click(expandBtn);
21282109
const [section] = store.getState().courseOutline.sectionsList;
21292110
const subsectionsDraggers = within(sectionElement).getAllByRole('button', { name: 'Drag to reorder' });
21302111
const draggableButton = subsectionsDraggers[1];
@@ -2154,8 +2135,6 @@ describe('<CourseOutline />', () => {
21542135
const [subsectionElement] = await within(sectionElement).findAllByTestId('subsection-card');
21552136
const section = store.getState().courseOutline.sectionsList[2];
21562137
const [subsection] = section.childInfo.children;
2157-
const expandBtn = within(subsectionElement).getByTestId('subsection-card-header__expanded-btn');
2158-
fireEvent.click(expandBtn);
21592138
const unitDraggers = await within(subsectionElement).findAllByRole('button', { name: 'Drag to reorder' });
21602139
const draggableButton = unitDraggers[1];
21612140
const sections = courseOutlineIndexMock.courseStructure.childInfo.children;
@@ -2190,8 +2169,6 @@ describe('<CourseOutline />', () => {
21902169
const [subsectionElement] = await within(sectionElement).findAllByTestId('subsection-card');
21912170
const section = store.getState().courseOutline.sectionsList[2];
21922171
const [subsection] = section.childInfo.children;
2193-
const expandBtn = within(subsectionElement).getByTestId('subsection-card-header__expanded-btn');
2194-
fireEvent.click(expandBtn);
21952172
const unitDraggers = await within(subsectionElement).findAllByRole('button', { name: 'Drag to reorder' });
21962173
const draggableButton = unitDraggers[1];
21972174
const sections = courseOutlineIndexMock.courseStructure.childInfo.children;
@@ -2229,8 +2206,6 @@ describe('<CourseOutline />', () => {
22292206
.onGet(getXBlockApiUrl(section.id))
22302207
.reply(200, courseSectionMock);
22312208
let [subsectionElement] = await within(sectionElement).findAllByTestId('subsection-card');
2232-
const expandBtn = await within(subsectionElement).findByTestId('subsection-card-header__expanded-btn');
2233-
userEvent.click(expandBtn);
22342209
const [unit] = subsection.childInfo.children;
22352210
const [unitElement] = await within(subsectionElement).findAllByTestId('unit-card');
22362211

src/course-outline/header-navigations/HeaderNavigations.jsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ const HeaderNavigations = ({
6666
{hasSections && (
6767
<Button
6868
variant="outline-primary"
69+
id="expand-collapse-all-button"
70+
data-testid="expand-collapse-all-button"
6971
iconBefore={isSectionsExpanded ? ArrowUpIcon : ArrowDownIcon}
7072
onClick={handleExpandAll}
7173
>

src/course-outline/subsection-card/SubsectionCard.jsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { getStudioHomeData } from '../../studio-home/data/selectors';
3131
const SubsectionCard = ({
3232
section,
3333
subsection,
34+
isSectionsExpanded,
3435
isSelfPaced,
3536
isCustomRelativeDatesActive,
3637
children,
@@ -98,14 +99,18 @@ const SubsectionCard = ({
9899

99100
return false;
100101
};
101-
const [isExpanded, setIsExpanded] = useState(containsSearchResult() || !isHeaderVisible);
102+
const [isExpanded, setIsExpanded] = useState(containsSearchResult() || !isHeaderVisible || isSectionsExpanded);
102103
const subsectionStatus = getItemStatus({
103104
published,
104105
visibilityState,
105106
hasChanges,
106107
});
107108
const borderStyle = getItemStatusBorder(subsectionStatus);
108109

110+
useEffect(() => {
111+
setIsExpanded(isSectionsExpanded);
112+
}, [isSectionsExpanded]);
113+
109114
const handleExpandContent = () => {
110115
setIsExpanded((prevState) => !prevState);
111116
};
@@ -252,7 +257,7 @@ const SubsectionCard = ({
252257
</div>
253258
</>
254259
)}
255-
{isExpanded && (
260+
{(isExpanded) && (
256261
<div
257262
data-testid="subsection-card__units"
258263
className={classNames('subsection-card__units', { 'item-children': isDraggable })}
@@ -354,6 +359,7 @@ SubsectionCard.propTypes = {
354359
}).isRequired,
355360
}).isRequired,
356361
children: PropTypes.node,
362+
isSectionsExpanded: PropTypes.bool.isRequired,
357363
isSelfPaced: PropTypes.bool.isRequired,
358364
isCustomRelativeDatesActive: PropTypes.bool.isRequired,
359365
onOpenPublishModal: PropTypes.func.isRequired,

0 commit comments

Comments
 (0)