forked from openedx/frontend-app-authoring
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOutlineAddChildButtons.test.tsx
More file actions
186 lines (175 loc) · 6.1 KB
/
OutlineAddChildButtons.test.tsx
File metadata and controls
186 lines (175 loc) · 6.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import userEvent from '@testing-library/user-event';
import { getConfig, setConfig } from '@edx/frontend-platform';
import { ContainerType } from '@src/generic/key-utils';
import {
initializeMocks,
render,
screen,
waitFor,
} from '@src/testUtils';
import { OutlineFlow, OutlineSidebarProvider } from '@src/course-outline/outline-sidebar/OutlineSidebarContext';
import OutlineAddChildButtons from './OutlineAddChildButtons';
jest.mock('@src/studio-home/data/selectors', () => ({
...jest.requireActual('@src/studio-home/data/selectors'),
getStudioHomeData: () => ({
librariesV2Enabled: true,
}),
}));
const handleAddAndOpenUnit = { mutateAsync: jest.fn() };
const handleAddBlock = { mutateAsync: jest.fn() };
const courseUsageKey = 'some/usage/key';
const setCurrentSelection = jest.fn();
jest.mock('@src/CourseAuthoringContext', () => ({
useCourseAuthoringContext: () => ({
courseId: 5,
courseUsageKey,
getUnitUrl: (id: string) => `/some/${id}`,
}),
}));
jest.mock('@src/course-outline/CourseOutlineContext', () => ({
useCourseOutlineContext: () => ({
handleAddAndOpenUnit,
handleAddBlock,
setCurrentSelection,
}),
}));
const startCurrentFlow = jest.fn();
let currentFlow: OutlineFlow | null = null;
jest.mock('@src/course-outline/outline-sidebar/OutlineSidebarContext', () => ({
...jest.requireActual('@src/course-outline/outline-sidebar/OutlineSidebarContext'),
useOutlineSidebarContext: () => ({
...jest.requireActual('@src/course-outline/outline-sidebar/OutlineSidebarContext').useOutlineSidebarContext(),
startCurrentFlow,
currentFlow,
isCurrentFlowOn: !!currentFlow,
}),
}));
[
{ containerType: ContainerType.Section },
{ containerType: ContainerType.Subsection },
{ containerType: ContainerType.Unit },
].forEach(({ containerType }) => {
describe(`<OutlineAddChildButtons> for ${containerType}`, () => {
beforeEach(() => {
initializeMocks();
setConfig({
...getConfig(),
ENABLE_COURSE_OUTLINE_NEW_DESIGN: 'true',
});
});
it('renders and behaves correctly', async () => {
const newClickHandler = jest.fn();
const useFromLibClickHandler = jest.fn();
render(
<OutlineAddChildButtons
handleNewButtonClick={newClickHandler}
handleUseFromLibraryClick={useFromLibClickHandler}
childType={containerType}
parentLocator=""
/>,
{ extraWrapper: OutlineSidebarProvider },
);
const newBtn = await screen.findByRole('button', { name: `New ${containerType}` });
expect(newBtn).toBeInTheDocument();
const useBtn = await screen.findByRole('button', { name: `Use ${containerType} from library` });
expect(useBtn).toBeInTheDocument();
await userEvent.click(newBtn);
await waitFor(() => expect(newClickHandler).toHaveBeenCalled());
await userEvent.click(useBtn);
await waitFor(() => expect(useFromLibClickHandler).toHaveBeenCalled());
});
it('calls appropriate new handlers', async () => {
const parentLocator = `parent-of-${containerType}`;
const grandParentLocator = `grandparent-of-${containerType}`;
render(
<OutlineAddChildButtons
childType={containerType}
parentLocator={parentLocator}
grandParentLocator={grandParentLocator}
/>,
{ extraWrapper: OutlineSidebarProvider },
);
const newBtn = await screen.findByRole('button', { name: `New ${containerType}` });
expect(newBtn).toBeInTheDocument();
await userEvent.click(newBtn);
switch (containerType) {
case ContainerType.Section:
await waitFor(() =>
expect(handleAddBlock.mutateAsync).toHaveBeenCalledWith(
{
type: ContainerType.Chapter,
parentLocator: courseUsageKey,
displayName: 'Section',
},
expect.objectContaining({ onSuccess: expect.any(Function) }),
)
);
break;
case ContainerType.Subsection:
await waitFor(() =>
expect(handleAddBlock.mutateAsync).toHaveBeenCalledWith(
{
type: ContainerType.Sequential,
parentLocator,
displayName: 'Subsection',
sectionId: parentLocator,
},
expect.objectContaining({ onSuccess: expect.any(Function) }),
)
);
break;
case ContainerType.Unit:
await waitFor(() =>
expect(handleAddAndOpenUnit.mutateAsync).toHaveBeenCalledWith({
type: ContainerType.Vertical,
parentLocator,
displayName: 'Unit',
sectionId: grandParentLocator,
})
);
break;
default:
throw new Error(`Unknown container type: ${containerType}`);
}
});
it('calls appropriate use handlers', async () => {
const parentLocator = `parent-of-${containerType}`;
render(
<OutlineAddChildButtons
childType={containerType}
parentLocator={parentLocator}
/>,
{ extraWrapper: OutlineSidebarProvider },
);
const useBtn = await screen.findByRole('button', { name: `Use ${containerType} from library` });
expect(useBtn).toBeInTheDocument();
await userEvent.click(useBtn);
await waitFor(() =>
expect(startCurrentFlow).toHaveBeenCalledWith({
flowType: containerType,
parentLocator,
})
);
});
it('shows appropriate static placeholder', async () => {
const parentLocator = `parent-of-${containerType}`;
currentFlow = {
flowType: containerType,
parentLocator,
};
render(
<OutlineAddChildButtons
childType={containerType}
parentLocator={parentLocator}
/>,
{ extraWrapper: OutlineSidebarProvider },
);
// should show placeholder when use button is clicked
expect(
await screen.findByRole('heading', {
name: new RegExp(`Adding Library ${containerType}`, 'i'),
}),
).toBeInTheDocument();
});
});
});