forked from openedx/frontend-app-authoring
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContainerInfo.test.tsx
More file actions
395 lines (346 loc) · 13.6 KB
/
ContainerInfo.test.tsx
File metadata and controls
395 lines (346 loc) · 13.6 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
import userEvent from '@testing-library/user-event';
import type MockAdapter from 'axios-mock-adapter';
import {
initializeMocks,
render as baseRender,
screen,
waitFor,
fireEvent,
within,
} from '@src/testUtils';
import { ContainerType } from '@src/generic/key-utils';
import type { ToastActionData } from '@src/generic/toast-context';
import { mockContentSearchConfig, mockSearchResult, hydrateSearchResult } from '@src/search-manager/data/api.mock';
import { PublishedFilterContextProvider } from '@src/library-authoring/common/context/PublishedFilterContext';
import {
mockContentLibrary,
mockGetContainerChildren,
mockGetContainerMetadata,
mockGetContainerHierarchy,
} from '../data/api.mocks';
import { LibraryProvider } from '../common/context/LibraryContext';
import ContainerInfo from './ContainerInfo';
import {
getLibraryContainerApiUrl,
getLibraryContainerPublishApiUrl,
getLibraryContainerCopyApiUrl,
} from '../data/api';
import { SidebarBodyItemId, SidebarProvider } from '../common/context/SidebarContext';
mockContentLibrary.applyMock();
mockContentSearchConfig.applyMock();
mockGetContainerMetadata.applyMock();
mockGetContainerChildren.applyMock();
mockGetContainerHierarchy.applyMock();
const { libraryId } = mockContentLibrary;
const {
unitId,
subsectionId,
sectionId,
unitIdEmpty,
subsectionIdEmpty,
sectionIdEmpty,
unitIdPublished,
subsectionIdPublished,
sectionIdPublished,
} = mockGetContainerMetadata;
const {
unitIdOneChild,
subsectionIdOneChild,
sectionIdOneChild,
} = mockGetContainerHierarchy;
// Convert a given containerId to its "empty" equivalent
const emptyId = (id: string) => {
switch (id) {
case unitId:
return unitIdEmpty;
case subsectionId:
return subsectionIdEmpty;
case sectionId:
return sectionIdEmpty;
default:
return undefined;
}
};
// Convert a given containerId to its "published" equivalent
const publishedId = (id: string) => {
switch (id) {
case unitId:
return unitIdPublished;
case subsectionId:
return subsectionIdPublished;
case sectionId:
return sectionIdPublished;
default:
return undefined;
}
};
// Convert a given containerId to its "one child" equivalent
const singleChild = (id: string) => {
switch (id) {
case unitId:
return unitIdOneChild;
case subsectionId:
return subsectionIdOneChild;
case sectionId:
return sectionIdOneChild;
default:
return undefined;
}
};
const mockNavigate = jest.fn();
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useNavigate: () => mockNavigate,
}));
const render = (
containerId,
containerType: string = '', // renders container page
showOnlyPublished: boolean = false,
) => {
const params: { libraryId: string; selectedItemId?: string; } = { libraryId, selectedItemId: containerId };
const path = containerType
? `/library/:libraryId/${containerType}/:selectedItemId?`
: '/library/:libraryId/:selectedItemId?';
return baseRender(<ContainerInfo />, {
path,
params,
extraWrapper: ({ children }) => (
<PublishedFilterContextProvider showOnlyPublished={showOnlyPublished}>
<LibraryProvider libraryId={libraryId}>
<SidebarProvider
initialSidebarItemInfo={{
id: containerId,
type: SidebarBodyItemId.ContainerInfo,
}}
>
{children}
</SidebarProvider>
</LibraryProvider>
</PublishedFilterContextProvider>
),
});
};
let axiosMock: MockAdapter;
let mockShowToast: { (message: string, action?: ToastActionData): void; mock?: any; };
[
{
containerType: ContainerType.Unit,
containerId: unitId,
childType: 'component',
willPublishCount: 2,
},
{
containerType: ContainerType.Subsection,
containerId: subsectionId,
childType: 'unit',
willPublishCount: 3,
},
{
containerType: ContainerType.Section,
containerId: sectionId,
childType: 'subsection',
willPublishCount: 4,
},
].forEach(({
containerId,
containerType,
childType,
willPublishCount,
}) => {
describe(`<ContainerInfo /> with containerType: ${containerType}`, () => {
beforeEach(() => {
({ axiosMock, mockShowToast } = initializeMocks());
mockSearchResult(hydrateSearchResult([{
blockType: containerType,
displayName: `Test ${containerType}`,
}]));
});
it(`should delete the ${containerType} using the menu`, async () => {
const user = userEvent.setup();
axiosMock.onDelete(getLibraryContainerApiUrl(containerId)).reply(200);
render(containerId);
// Open menu
expect(await screen.findByTestId('container-info-menu-toggle')).toBeInTheDocument();
await user.click(screen.getByTestId('container-info-menu-toggle'));
// Click on Delete Item
const deleteMenuItem = await screen.findByRole('button', { name: 'Delete' });
expect(deleteMenuItem).toBeInTheDocument();
fireEvent.click(deleteMenuItem);
// Confirm delete Modal is open
const modal = await screen.findByRole('dialog', {
name: `Delete ${containerType[0].toUpperCase()}${containerType.slice(1)}`,
});
expect(modal).toBeInTheDocument();
const deleteButton = screen.getByRole('button', { name: /delete/i });
fireEvent.click(deleteButton);
await waitFor(() => {
expect(axiosMock.history.delete.length).toBe(1);
});
expect(mockShowToast).toHaveBeenCalled();
});
it(`should copy the ${containerType} using the menu`, async () => {
const user = userEvent.setup();
const url = getLibraryContainerCopyApiUrl(containerId);
axiosMock.onPost(url).reply(200);
render(containerId);
// Open menu
expect(await screen.findByTestId('container-info-menu-toggle')).toBeInTheDocument();
await user.click(screen.getByTestId('container-info-menu-toggle'));
// Click on Copy Item
const copyMenuItem = await screen.findByRole('button', { name: 'Copy to clipboard' });
expect(copyMenuItem).toBeInTheDocument();
await user.click(copyMenuItem);
await waitFor(() => {
expect(axiosMock.history.post.length).toBe(1);
});
expect(axiosMock.history.post[0].url).toEqual(url);
});
it(`shows Published if the ${containerType} has no draft changes`, async () => {
render(publishedId(containerId), containerType);
// "Published" status should be displayed
expect(await screen.findByText('Published')).toBeInTheDocument();
});
it(`can publish the ${containerType} from the container page`, async () => {
const user = userEvent.setup();
axiosMock.onPost(getLibraryContainerPublishApiUrl(containerId)).reply(200);
render(containerId, containerType);
// Click on Publish button
let publishButton = await screen.findByRole('button', { name: /publish changes/i });
expect(publishButton).toBeInTheDocument();
await user.click(publishButton);
expect(publishButton).not.toBeInTheDocument();
// Reveals the confirmation box with warning text and publish hierarchy
expect(await screen.findByText('Confirm Publish')).toBeInTheDocument();
expect(screen.getByText(
new RegExp(
`This ${containerType} and the ${childType}s it contains will all be`, // <strong>published</strong>
'i',
),
)).toBeInTheDocument();
expect(screen.queryAllByText('Will Publish').length).toBe(willPublishCount);
expect(screen.queryAllByText('Draft').length).toBe(0);
// Click on the confirm Cancel button
const publishCancel = await screen.findByRole('button', { name: 'Cancel' });
expect(publishCancel).toBeInTheDocument();
await user.click(publishCancel);
expect(axiosMock.history.post.length).toBe(0);
// Click on Publish button again
publishButton = await screen.findByRole('button', { name: /publish changes/i });
expect(publishButton).toBeInTheDocument();
await user.click(publishButton);
expect(publishButton).not.toBeInTheDocument();
// Click on the confirm Publish button
const publishConfirm = await screen.findByRole('button', { name: 'Publish' });
expect(publishConfirm).toBeInTheDocument();
await user.click(publishConfirm);
await waitFor(() => {
expect(axiosMock.history.post.length).toBe(1);
});
expect(mockShowToast).toHaveBeenCalledWith('All changes published');
});
it(`shows an error if publishing the ${containerType} fails`, async () => {
const user = userEvent.setup();
axiosMock.onPost(getLibraryContainerPublishApiUrl(containerId)).reply(500);
render(containerId, containerType);
// Click on Publish button to reveal the confirmation box
const publishButton = await screen.findByRole('button', { name: /publish changes/i });
expect(publishButton).toBeInTheDocument();
await user.click(publishButton);
expect(publishButton).not.toBeInTheDocument();
// Click on the confirm Publish button
const publishConfirm = await screen.findByRole('button', { name: 'Publish' });
expect(publishConfirm).toBeInTheDocument();
await user.click(publishConfirm);
await waitFor(() => {
expect(axiosMock.history.post.length).toBe(1);
});
expect(mockShowToast).toHaveBeenCalledWith('Failed to publish changes');
});
it(`shows single child message before publishing the ${containerType}`, async () => {
const user = userEvent.setup();
render(singleChild(containerId), containerType);
// Click on Publish button
const publishButton = await screen.findByRole('button', { name: /publish changes/i });
expect(publishButton).toBeInTheDocument();
await user.click(publishButton);
expect(publishButton).not.toBeInTheDocument();
// Check warning text in the confirmation box
expect(screen.getByText(
new RegExp(
`This ${containerType} and the ${childType} it contains will all be`, // <strong>published</strong>
'i',
),
)).toBeInTheDocument();
});
it(`omits child count before publishing an empty ${containerType}`, async () => {
const user = userEvent.setup();
render(emptyId(containerId), containerType);
// Click on Publish button
const publishButton = await screen.findByRole('button', { name: /publish changes/i });
expect(publishButton).toBeInTheDocument();
await user.click(publishButton);
expect(publishButton).not.toBeInTheDocument();
// Check warning text in the confirmation box
expect(
await screen.findByText(
new RegExp(
`This ${containerType} will be`, // <strong>published</strong>
'i',
),
),
).toBeInTheDocument();
});
it(`show only published ${containerType} content`, async () => {
render(containerId, containerType, true);
expect(await screen.findByText(/block published 1/i)).toBeInTheDocument();
});
it(`shows the ${containerType} Preview tab by default and the children are readonly`, async () => {
const user = userEvent.setup();
render(containerId);
const previewTab = await screen.findByText('Preview');
expect(previewTab).toBeInTheDocument();
expect(previewTab).toHaveAttribute('aria-selected', 'true');
// Check that there are no edit buttons for components titles
expect(screen.queryAllByRole('button', { name: /edit/i }).length).toBe(0);
// Check that there are no drag handle for components/containers
expect(screen.queryAllByRole('button', { name: 'Drag to reorder' }).length).toBe(0);
// Check that there are no menu buttons for components
expect(screen.queryAllByRole('button', { name: /component actions menu/i }).length).toBe(0);
// If the childType is a component, it should be displayed as a text block
const childTypeDisplayName = childType === 'component' ? 'text' : childType;
const child = await screen.findByText(`${childTypeDisplayName} block 0`);
// Check that there are no menu buttons for containers
expect(
within(
child.parentElement!.parentElement!.parentElement!,
).queryAllByRole('button', { name: /container actions menu/i }).length,
).toBe(0);
// Trigger double click. Find the child card as the parent element
await user.dblClick(child.parentElement!.parentElement!.parentElement!);
// Click should not do anything in preview
expect(mockNavigate).not.toHaveBeenCalled();
});
it(`shows the ${containerType} hierarchy in the Usage tab`, async () => {
const user = userEvent.setup();
render(containerId, containerType);
const usageTab = await screen.findByText('Usage');
await user.click(usageTab);
expect(usageTab).toHaveAttribute('aria-selected', 'true');
// Content hierarchy selects the current containerType and shows its display name
expect(await screen.findByText('Content Hierarchy')).toBeInTheDocument();
const container = await screen.findByText(`${containerType} block 0`);
expect(container.parentElement!.parentElement).toHaveClass('selected');
// Other container types should show counts
if (containerType !== 'section') {
expect(await screen.findByText('2 Sections')).toBeInTheDocument();
}
if (containerType !== 'subsection') {
expect(await screen.findByText('3 Subsections')).toBeInTheDocument();
}
if (containerType !== 'unit') {
expect(await screen.findByText('4 Units')).toBeInTheDocument();
}
expect(await screen.findByText('5 Components')).toBeInTheDocument();
});
});
});