Skip to content

Commit 8dea05b

Browse files
committed
fix(course-outline): handle back from subsection without section
1 parent 0fad221 commit 8dea05b

4 files changed

Lines changed: 57 additions & 9 deletions

File tree

src/course-outline/outline-sidebar/AddSidebar.test.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,23 @@ describe('AddSidebar', () => {
451451
expect(clearSelection).not.toHaveBeenCalled();
452452
});
453453

454+
it('back from subsection without section clears selection', async () => {
455+
const user = userEvent.setup();
456+
const section = outlineChildren[0];
457+
const subsection = section.childInfo.children[0];
458+
selectedContainerState = {
459+
currentId: subsection.id,
460+
subsectionId: subsection.id,
461+
};
462+
renderComponent();
463+
464+
const back = await screen.findByRole('button', { name: 'Back' });
465+
await user.click(back);
466+
467+
expect(clearSelection).toHaveBeenCalled();
468+
expect(openContainerSidebar).not.toHaveBeenCalled();
469+
});
470+
454471
it('back from section clears selection', async () => {
455472
const user = userEvent.setup();
456473
const section = outlineChildren[0];

src/course-outline/outline-sidebar/AddSidebar.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,8 +401,12 @@ export const AddSidebar = () => {
401401
const subsectionIndex = section
402402
?.childInfo.children.findIndex((subsection) => subsection.id === subsectionId) ?? -1;
403403

404-
if (currentId === subsectionId && sectionId) {
405-
openContainerSidebar(sectionId, undefined, sectionId, sectionIndex >= 0 ? sectionIndex : undefined);
404+
if (currentId === subsectionId) {
405+
if (sectionId) {
406+
openContainerSidebar(sectionId, undefined, sectionId, sectionIndex >= 0 ? sectionIndex : undefined);
407+
return;
408+
}
409+
clearSelection();
406410
return;
407411
}
408412

src/course-outline/outline-sidebar/OutlineAlignSidebar.test.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,28 @@ describe('OutlineAlignSidebar', () => {
157157
});
158158
});
159159

160+
it('back button from subsection without section clears selection', async () => {
161+
jest
162+
.spyOn(OutlineSidebarContext, 'useOutlineSidebarContext')
163+
.mockReturnValue({
164+
selectedContainerState: {
165+
currentId: 'subsection-1',
166+
subsectionId: 'subsection-1',
167+
},
168+
clearSelection,
169+
openContainerSidebar,
170+
} as any);
171+
172+
render(<OutlineAlignSidebar />);
173+
174+
const backButton = await screen.findByRole('button', { name: /back/i });
175+
backButton.click();
176+
177+
expect(clearSelection).toHaveBeenCalled();
178+
expect(setCurrentSelection).toHaveBeenCalledWith(undefined);
179+
expect(openContainerSidebar).not.toHaveBeenCalled();
180+
});
181+
160182
it('back button from section clears selection', async () => {
161183
jest
162184
.spyOn(OutlineSidebarContext, 'useOutlineSidebarContext')

src/course-outline/outline-sidebar/OutlineAlignSidebar.tsx

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,18 @@ export const OutlineAlignSidebar = () => {
3030
return;
3131
}
3232

33-
if (currentId === subsectionId && sectionId) {
34-
openContainerSidebar(sectionId, undefined, sectionId, sectionIndex >= 0 ? sectionIndex : undefined);
35-
setCurrentSelection({
36-
currentId: sectionId,
37-
sectionId,
38-
index: sectionIndex >= 0 ? sectionIndex : undefined,
39-
});
33+
if (currentId === subsectionId) {
34+
if (sectionId) {
35+
openContainerSidebar(sectionId, undefined, sectionId, sectionIndex >= 0 ? sectionIndex : undefined);
36+
setCurrentSelection({
37+
currentId: sectionId,
38+
sectionId,
39+
index: sectionIndex >= 0 ? sectionIndex : undefined,
40+
});
41+
return;
42+
}
43+
clearSelection();
44+
setCurrentSelection(undefined);
4045
return;
4146
}
4247

0 commit comments

Comments
 (0)