Skip to content

Commit ee7992b

Browse files
authored
fix: Expand all now expands subsections (#1998)
* fix: Expand all now expands subsections * fix: test cases
1 parent 1dee2bb commit ee7992b

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
import { logError } from '@edx/frontend-platform/logging';
1313
import { useLocation } from 'react-router-dom';
14-
import userEvent from '@testing-library/user-event';
1514
import {
1615
getCourseBestPracticesApiUrl,
1716
getCourseLaunchApiUrl,
@@ -308,13 +307,15 @@ describe('<CourseOutline />', () => {
308307
});
309308

310309
it('check that new section list is saved when dragged', async () => {
311-
const { findAllByRole } = render(<RootWrapper />);
312-
const courseBlockId = courseOutlineIndexMock.courseStructure.id;
310+
const { findAllByRole, findByTestId } = render(<RootWrapper />);
311+
const expandAllButton = await findByTestId('expand-collapse-all-button');
312+
fireEvent.click(expandAllButton);
313+
const [section] = store.getState().courseOutline.sectionsList;
313314
const sectionsDraggers = await findAllByRole('button', { name: 'Drag to reorder' });
314-
const draggableButton = sectionsDraggers[6];
315+
const draggableButton = sectionsDraggers[1];
315316

316317
axiosMock
317-
.onPut(getCourseBlockApiUrl(courseBlockId))
318+
.onPut(getCourseBlockApiUrl(section.id))
318319
.reply(200, { dummy: 'value' });
319320

320321
const section1 = store.getState().courseOutline.sectionsList[0].id;
@@ -333,13 +334,15 @@ describe('<CourseOutline />', () => {
333334
});
334335

335336
it('check section list is restored to original order when API call fails', async () => {
336-
const { findAllByRole } = render(<RootWrapper />);
337-
const courseBlockId = courseOutlineIndexMock.courseStructure.id;
337+
const { findAllByRole, findByTestId } = render(<RootWrapper />);
338+
const expandAllButton = await findByTestId('expand-collapse-all-button');
339+
fireEvent.click(expandAllButton);
340+
const [section] = store.getState().courseOutline.sectionsList;
338341
const sectionsDraggers = await findAllByRole('button', { name: 'Drag to reorder' });
339-
const draggableButton = sectionsDraggers[6];
342+
const draggableButton = sectionsDraggers[1];
340343

341344
axiosMock
342-
.onPut(getCourseBlockApiUrl(courseBlockId))
345+
.onPut(getCourseBlockApiUrl(section.id))
343346
.reply(500);
344347

345348
const section1 = store.getState().courseOutline.sectionsList[0].id;
@@ -414,8 +417,6 @@ describe('<CourseOutline />', () => {
414417
const { findAllByTestId } = render(<RootWrapper />);
415418
const [sectionElement] = await findAllByTestId('section-card');
416419
const [subsectionElement] = await within(sectionElement).findAllByTestId('subsection-card');
417-
const expandBtn = await within(subsectionElement).findByTestId('subsection-card-header__expanded-btn');
418-
fireEvent.click(expandBtn);
419420
const units = await within(subsectionElement).findAllByTestId('unit-card');
420421
expect(units.length).toBe(1);
421422

@@ -440,8 +441,6 @@ describe('<CourseOutline />', () => {
440441
render(<RootWrapper />);
441442
const [sectionElement] = await screen.findAllByTestId('section-card');
442443
const [subsectionElement] = await within(sectionElement).findAllByTestId('subsection-card');
443-
const expandBtn = await within(subsectionElement).findByTestId('subsection-card-header__expanded-btn');
444-
fireEvent.click(expandBtn);
445444
const units = await within(subsectionElement).findAllByTestId('unit-card');
446445
expect(units.length).toBe(1);
447446

@@ -665,8 +664,6 @@ describe('<CourseOutline />', () => {
665664
await checkEditTitle(section, subsectionElement, subsection, 'New subsection name', 'subsection');
666665

667666
// check unit
668-
const expandBtn = await within(subsectionElement).findByTestId('subsection-card-header__expanded-btn');
669-
fireEvent.click(expandBtn);
670667
const [unit] = subsection.childInfo.children;
671668
const [unitElement] = await within(subsectionElement).findAllByTestId('unit-card');
672669
await checkEditTitle(section, unitElement, unit, 'New unit name', 'unit');
@@ -679,8 +676,6 @@ describe('<CourseOutline />', () => {
679676
const [sectionElement] = await screen.findAllByTestId('section-card');
680677
const [subsection] = section.childInfo.children;
681678
const [subsectionElement] = await within(sectionElement).findAllByTestId('subsection-card');
682-
const expandBtn = await within(subsectionElement).findByTestId('subsection-card-header__expanded-btn');
683-
fireEvent.click(expandBtn);
684679
const [unit] = subsection.childInfo.children;
685680
const [unitElement] = await within(subsectionElement).findAllByTestId('unit-card');
686681

@@ -719,8 +714,6 @@ describe('<CourseOutline />', () => {
719714
const [sectionElement] = await findAllByTestId('section-card');
720715
const [subsection] = section.childInfo.children;
721716
const [subsectionElement] = await within(sectionElement).findAllByTestId('subsection-card');
722-
const expandBtn = await within(subsectionElement).findByTestId('subsection-card-header__expanded-btn');
723-
fireEvent.click(expandBtn);
724717
const [unit] = subsection.childInfo.children;
725718
const [unitElement] = await within(subsectionElement).findAllByTestId('unit-card');
726719

@@ -790,8 +783,6 @@ describe('<CourseOutline />', () => {
790783
const [sectionElement] = await findAllByTestId('section-card');
791784
const [subsection] = section.childInfo.children;
792785
const [subsectionElement] = await within(sectionElement).findAllByTestId('subsection-card');
793-
const expandBtn = await within(subsectionElement).findByTestId('subsection-card-header__expanded-btn');
794-
fireEvent.click(expandBtn);
795786
const [unit] = subsection.childInfo.children;
796787
const [unitElement] = await within(subsectionElement).findAllByTestId('unit-card');
797788

@@ -1500,8 +1491,6 @@ describe('<CourseOutline />', () => {
15001491

15011492
const [firstSection] = await findAllByTestId('section-card');
15021493
const [firstSubsection] = await within(firstSection).findAllByTestId('subsection-card');
1503-
const subsectionExpandButton = await within(firstSubsection).getByTestId('subsection-card-header__expanded-btn');
1504-
fireEvent.click(subsectionExpandButton);
15051494
const [firstUnit] = await within(firstSubsection).findAllByTestId('unit-card');
15061495
const unitDropdownButton = await within(firstUnit).findByTestId('unit-card-header__menu-button');
15071496

@@ -1861,8 +1850,6 @@ describe('<CourseOutline />', () => {
18611850
const [, sectionElement] = await findAllByTestId('section-card');
18621851
const [, subsection] = section.childInfo.children;
18631852
const [, subsectionElement] = await within(sectionElement).findAllByTestId('subsection-card');
1864-
const expandBtn = await within(subsectionElement).findByTestId('subsection-card-header__expanded-btn');
1865-
await act(async () => fireEvent.click(expandBtn));
18661853
const [, secondUnit] = subsection.childInfo.children;
18671854
const [, unitElement] = await within(subsectionElement).findAllByTestId('unit-card');
18681855

@@ -1902,8 +1889,6 @@ describe('<CourseOutline />', () => {
19021889
const [, sectionElement] = await findAllByTestId('section-card');
19031890
const [firstSubsection, subsection] = section.childInfo.children;
19041891
const [, subsectionElement] = await within(sectionElement).findAllByTestId('subsection-card');
1905-
const expandBtn = await within(subsectionElement).findByTestId('subsection-card-header__expanded-btn');
1906-
await act(async () => fireEvent.click(expandBtn));
19071892
const [unit] = subsection.childInfo.children;
19081893
const [unitElement] = await within(subsectionElement).findAllByTestId('unit-card');
19091894

@@ -1939,8 +1924,6 @@ describe('<CourseOutline />', () => {
19391924
const [subsection] = secondSection.childInfo.children;
19401925
const firstSectionLastSubsection = firstSection.childInfo.children[firstSection.childInfo.children.length - 1];
19411926
const [subsectionElement] = await within(sectionElement).findAllByTestId('subsection-card');
1942-
const expandBtn = await within(subsectionElement).findByTestId('subsection-card-header__expanded-btn');
1943-
await act(async () => fireEvent.click(expandBtn));
19441927
const [unit] = subsection.childInfo.children;
19451928
const [unitElement] = await within(subsectionElement).findAllByTestId('unit-card');
19461929

@@ -1985,8 +1968,6 @@ describe('<CourseOutline />', () => {
19851968
const [, sectionElement] = await findAllByTestId('section-card');
19861969
const [firstSubsection, subsection] = section.childInfo.children;
19871970
const [subsectionElement] = await within(sectionElement).findAllByTestId('subsection-card');
1988-
const expandBtn = await within(subsectionElement).findByTestId('subsection-card-header__expanded-btn');
1989-
await act(async () => fireEvent.click(expandBtn));
19901971
const lastUnitIdx = firstSubsection.childInfo.children.length - 1;
19911972
const unit = firstSubsection.childInfo.children[lastUnitIdx];
19921973
const unitElement = (await within(subsectionElement).findAllByTestId('unit-card'))[lastUnitIdx];
@@ -2024,8 +2005,6 @@ describe('<CourseOutline />', () => {
20242005
const secondSectionLastSubsection = secondSection.childInfo.children[lastSubIndex];
20252006
const thirdSectionFirstSubsection = thirdSection.childInfo.children[0];
20262007
const subsectionElement = (await within(sectionElement).findAllByTestId('subsection-card'))[lastSubIndex];
2027-
const expandBtn = await within(subsectionElement).findByTestId('subsection-card-header__expanded-btn');
2028-
await act(async () => fireEvent.click(expandBtn));
20292008
const lastUnitIdx = secondSectionLastSubsection.childInfo.children.length - 1;
20302009
const unit = secondSectionLastSubsection.childInfo.children[lastUnitIdx];
20312010
const unitElement = (await within(subsectionElement).findAllByTestId('unit-card'))[lastUnitIdx];
@@ -2070,8 +2049,6 @@ describe('<CourseOutline />', () => {
20702049
const sections = await findAllByTestId('section-card');
20712050
const [sectionElement] = sections;
20722051
const [subsectionElement] = await within(sectionElement).findAllByTestId('subsection-card');
2073-
const expandBtn = await within(subsectionElement).findByTestId('subsection-card-header__expanded-btn');
2074-
await act(async () => fireEvent.click(expandBtn));
20752052
// get first and only unit in the subsection
20762053
const [firstUnit] = await within(subsectionElement).findAllByTestId('unit-card');
20772054

@@ -2091,8 +2068,6 @@ describe('<CourseOutline />', () => {
20912068
const lastSection = sections[sections.length - 1];
20922069
// it has only one subsection
20932070
const [lastSubsectionElement] = await within(lastSection).findAllByTestId('subsection-card');
2094-
const lastExpandBtn = await within(lastSubsectionElement).findByTestId('subsection-card-header__expanded-btn');
2095-
await act(async () => fireEvent.click(lastExpandBtn));
20962071
// get last and the only unit in the subsection
20972072
const [lastUnit] = await within(lastSubsectionElement).findAllByTestId('unit-card');
20982073

@@ -2113,6 +2088,9 @@ describe('<CourseOutline />', () => {
21132088
const { findAllByTestId } = render(<RootWrapper />);
21142089

21152090
const [sectionElement] = await findAllByTestId('section-card');
2091+
const [subsectionElement] = await within(sectionElement).findAllByTestId('subsection-card');
2092+
const expandBtn = await within(subsectionElement).findByTestId('subsection-card-header__expanded-btn');
2093+
fireEvent.click(expandBtn);
21162094
const [section] = store.getState().courseOutline.sectionsList;
21172095
const subsectionsDraggers = within(sectionElement).getAllByRole('button', { name: 'Drag to reorder' });
21182096
const draggableButton = subsectionsDraggers[1];
@@ -2144,6 +2122,9 @@ describe('<CourseOutline />', () => {
21442122
const { findAllByTestId } = render(<RootWrapper />);
21452123

21462124
const [sectionElement] = await findAllByTestId('section-card');
2125+
const [subsectionElement] = await within(sectionElement).findAllByTestId('subsection-card');
2126+
const expandBtn = await within(subsectionElement).findByTestId('subsection-card-header__expanded-btn');
2127+
fireEvent.click(expandBtn);
21472128
const [section] = store.getState().courseOutline.sectionsList;
21482129
const subsectionsDraggers = within(sectionElement).getAllByRole('button', { name: 'Drag to reorder' });
21492130
const draggableButton = subsectionsDraggers[1];
@@ -2173,8 +2154,6 @@ describe('<CourseOutline />', () => {
21732154
const [subsectionElement] = await within(sectionElement).findAllByTestId('subsection-card');
21742155
const section = store.getState().courseOutline.sectionsList[2];
21752156
const [subsection] = section.childInfo.children;
2176-
const expandBtn = within(subsectionElement).getByTestId('subsection-card-header__expanded-btn');
2177-
fireEvent.click(expandBtn);
21782157
const unitDraggers = await within(subsectionElement).findAllByRole('button', { name: 'Drag to reorder' });
21792158
const draggableButton = unitDraggers[1];
21802159
const sections = courseOutlineIndexMock.courseStructure.childInfo.children;
@@ -2209,8 +2188,6 @@ describe('<CourseOutline />', () => {
22092188
const [subsectionElement] = await within(sectionElement).findAllByTestId('subsection-card');
22102189
const section = store.getState().courseOutline.sectionsList[2];
22112190
const [subsection] = section.childInfo.children;
2212-
const expandBtn = within(subsectionElement).getByTestId('subsection-card-header__expanded-btn');
2213-
fireEvent.click(expandBtn);
22142191
const unitDraggers = await within(subsectionElement).findAllByRole('button', { name: 'Drag to reorder' });
22152192
const draggableButton = unitDraggers[1];
22162193
const sections = courseOutlineIndexMock.courseStructure.childInfo.children;
@@ -2248,8 +2225,6 @@ describe('<CourseOutline />', () => {
22482225
.onGet(getXBlockApiUrl(section.id))
22492226
.reply(200, courseSectionMock);
22502227
let [subsectionElement] = await within(sectionElement).findAllByTestId('subsection-card');
2251-
const expandBtn = await within(subsectionElement).findByTestId('subsection-card-header__expanded-btn');
2252-
userEvent.click(expandBtn);
22532228
const [unit] = subsection.childInfo.children;
22542229
const [unitElement] = await within(subsectionElement).findAllByTestId('unit-card');
22552230

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)