forked from openedx/frontend-app-authoring
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLegacyLibMigrationPage.test.tsx
More file actions
443 lines (352 loc) · 17.3 KB
/
LegacyLibMigrationPage.test.tsx
File metadata and controls
443 lines (352 loc) · 17.3 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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
import type MockAdapter from 'axios-mock-adapter';
import userEvent from '@testing-library/user-event';
import {
initializeMocks,
render,
screen,
waitFor,
within,
} from '@src/testUtils';
import studioHomeMock from '@src/studio-home/__mocks__/studioHomeMock';
import { mockGetContentLibraryV2List } from '@src/library-authoring/data/api.mocks';
import { mockGetStudioHomeLibraries } from '@src/studio-home/data/api.mocks';
import { getContentLibraryV2CreateApiUrl } from '@src/library-authoring/create-library/data/api';
import { getStudioHomeApiUrl } from '@src/studio-home/data/api';
import { LegacyLibMigrationPage } from './LegacyLibMigrationPage';
import { bulkMigrateLegacyLibrariesUrl } from './data/api';
const path = '/libraries-v1/migrate/*';
let axiosMock: MockAdapter;
let mockShowToast;
mockGetStudioHomeLibraries.applyMock();
mockGetContentLibraryV2List.applyMock();
const mockNavigate = jest.fn();
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useNavigate: () => mockNavigate,
}));
jest.mock('@src/generic/data/apiHooks', () => ({
...jest.requireActual('@src/generic/data/apiHooks'),
useOrganizationListData: () => ({
data: ['org1', 'org2', 'org3', 'org4', 'org5'],
isLoading: false,
}),
}));
const renderPage = () => (
render(<LegacyLibMigrationPage />, { path })
);
describe('<LegacyLibMigrationPage />', () => {
beforeEach(() => {
const mocks = initializeMocks();
axiosMock = mocks.axiosMock;
mockShowToast = mocks.mockShowToast;
});
it('should render legacy library migration page', async () => {
renderPage();
// Should render the title
expect(await screen.findByText('Migrate Legacy Libraries')).toBeInTheDocument();
// Should render the Migration Steps Viewer
expect(screen.getByText('Select Legacy Libraries')).toBeInTheDocument();
expect(screen.getByText('Select Destination')).toBeInTheDocument();
expect(screen.getByText('Confirm')).toBeInTheDocument();
});
it('should cancel the migration', async () => {
renderPage();
expect(await screen.findByText('Migrate Legacy Libraries')).toBeInTheDocument();
const cancelButton = screen.getByRole('button', { name: /cancel/i });
cancelButton.click();
// Should show exit confirmation modal
expect(await screen.findByText('Exit Migration?')).toBeInTheDocument();
// Close exit confirmation modal
const continueButton = screen.getByRole('button', { name: /continue migrating/i });
continueButton.click();
expect(mockNavigate).not.toHaveBeenCalled();
cancelButton.click();
// Should navigate to legacy libraries tab on studio home
expect(await screen.findByText('Exit Migration?')).toBeInTheDocument();
const exitButton = screen.getByRole('button', { name: /exit/i });
exitButton.click();
await waitFor(() => {
expect(mockNavigate).toHaveBeenCalledWith('/libraries-v1');
});
});
it('should select legacy libraries', async () => {
const user = userEvent.setup();
renderPage();
expect(await screen.findByText('Migrate Legacy Libraries')).toBeInTheDocument();
const nextButton = screen.getByRole('button', { name: /next/i });
// The next button is disabled
expect(nextButton).toBeDisabled();
// The filter is Unmigrated by default
const filterButton = await screen.findByRole('button', { name: /unmigrated/i });
expect(filterButton).toBeInTheDocument();
// Clear filter to show all
await user.click(filterButton);
const clearButton = await screen.findByRole('button', { name: /clear filter/i });
await user.click(clearButton);
expect(await screen.findByText('MBA')).toBeInTheDocument();
expect(await screen.findByText('Legacy library 1')).toBeInTheDocument();
expect(await screen.findByText('MBA 1')).toBeInTheDocument();
const library1 = screen.getByRole('checkbox', { name: 'MBA' });
const library2 = screen.getByRole('checkbox', { name: /legacy library 1 imported library/i });
expect(library1).not.toBeChecked();
expect(library2).not.toBeChecked();
library1.click();
expect(library1).toBeChecked();
expect(library2).not.toBeChecked();
expect(nextButton).not.toBeDisabled();
library2.click();
expect(library1).toBeChecked();
expect(library2).toBeChecked();
expect(nextButton).not.toBeDisabled();
library2.click();
expect(library1).toBeChecked();
expect(library2).not.toBeChecked();
expect(nextButton).not.toBeDisabled();
});
it('should select all legacy libraries', async () => {
const user = userEvent.setup();
renderPage();
expect(await screen.findByText('Migrate Legacy Libraries')).toBeInTheDocument();
// The filter is Unmigrated by default
const filterButton = await screen.findByRole('button', { name: /unmigrated/i });
expect(filterButton).toBeInTheDocument();
// Clear filter to show all
await user.click(filterButton);
const clearButton = await screen.findByRole('button', { name: /clear filter/i });
await user.click(clearButton);
const selectAll = screen.getByRole('checkbox', { name: /select all/i });
await user.click(selectAll);
const library1 = screen.getByRole('checkbox', { name: 'MBA' });
const library2 = screen.getByRole('checkbox', { name: /legacy library 1 imported library/i });
const library3 = screen.getByRole('checkbox', { name: 'MBA 1' });
expect(library1).toBeChecked();
expect(library2).toBeChecked();
expect(library3).toBeChecked();
await user.click(selectAll);
expect(library1).not.toBeChecked();
expect(library2).not.toBeChecked();
expect(library3).not.toBeChecked();
});
it('should back to select legacy libraries', async () => {
const user = userEvent.setup();
renderPage();
// The filter is Unmigrated by default
const filterButton = await screen.findByRole('button', { name: /unmigrated/i });
expect(filterButton).toBeInTheDocument();
// Clear filter to show all
await user.click(filterButton);
const clearButton = await screen.findByRole('button', { name: /clear filter/i });
await user.click(clearButton);
expect(await screen.findByText('MBA')).toBeInTheDocument();
expect(await screen.findByText('Legacy library 1')).toBeInTheDocument();
expect(await screen.findByText('MBA 1')).toBeInTheDocument();
const legacyLibrary = screen.getByRole('checkbox', { name: 'MBA' });
legacyLibrary.click();
const nextButton = screen.getByRole('button', { name: /next/i });
nextButton.click();
// Should show alert of SelectDestinationView
expect(await screen.findByText(/you selected will be migrated to this new library/i)).toBeInTheDocument();
const backButton = screen.getByRole('button', { name: /back/i });
backButton.click();
// The selected legacy library remains checked
expect(legacyLibrary).toBeChecked();
// The filter remains the same
expect(await screen.findByText('MBA')).toBeInTheDocument();
expect(await screen.findByText('Legacy library 1')).toBeInTheDocument();
expect(await screen.findByText('MBA 1')).toBeInTheDocument();
});
it('should select a library destination', async () => {
renderPage();
expect(await screen.findByText('Migrate Legacy Libraries')).toBeInTheDocument();
expect(await screen.findByText('MBA')).toBeInTheDocument();
const legacyLibrary = screen.getByRole('checkbox', { name: 'MBA' });
legacyLibrary.click();
const nextButton = screen.getByRole('button', { name: /next/i });
nextButton.click();
// Should show alert of SelectDestinationView
expect(await screen.findByText(/you selected will be migrated to this new library/i)).toBeInTheDocument();
// The next button is disabled
expect(nextButton).toBeDisabled();
expect(await screen.findByText('Test Library 1')).toBeInTheDocument();
const radioButton = screen.getByRole('radio', { name: /test library 1/i });
radioButton.click();
expect(radioButton).toBeChecked();
expect(nextButton).not.toBeDisabled();
});
it('should back to select library destination', async () => {
const user = userEvent.setup();
renderPage();
expect(await screen.findByText('Migrate Legacy Libraries')).toBeInTheDocument();
expect(await screen.findByText('MBA')).toBeInTheDocument();
const legacyLibrary = screen.getByRole('checkbox', { name: 'MBA' });
await user.click(legacyLibrary);
const nextButton = await screen.findByRole('button', { name: /next/i });
await user.click(nextButton);
// Should show alert of SelectDestinationView
expect(await screen.findByText(/you selected will be migrated to this new library/i)).toBeInTheDocument();
expect(await screen.findByText('Test Library 1')).toBeInTheDocument();
const radioButton = screen.getByRole('radio', { name: /test library 1/i });
await user.click(radioButton);
await user.click(nextButton);
const alert = await screen.findByRole('alert');
expect(await within(alert).findByText(
/All content from the legacy library you selected will be migrated to the Content Library you select/i,
)).toBeInTheDocument();
const backButton = screen.getByRole('button', { name: /back/i });
await user.click(backButton);
expect(await screen.findByText('Test Library 1')).toBeInTheDocument();
// The selected v2 library remains checked
expect(radioButton).toBeChecked();
});
it('should open the create new library modal', async () => {
const user = userEvent.setup();
axiosMock.onGet(getStudioHomeApiUrl()).reply(200, studioHomeMock);
axiosMock.onPost(getContentLibraryV2CreateApiUrl()).reply(200, {
id: 'lib:SampleTaxonomyOrg1:TL1',
});
renderPage();
expect(await screen.findByText('Migrate Legacy Libraries')).toBeInTheDocument();
expect(await screen.findByText('MBA')).toBeInTheDocument();
const legacyLibrary = screen.getByRole('checkbox', { name: 'MBA' });
legacyLibrary.click();
const nextButton = screen.getByRole('button', { name: /next/i });
nextButton.click();
// Should show alert of SelectDestinationView
expect(await screen.findByText(/you selected will be migrated to this new library/i)).toBeInTheDocument();
const createButton = await screen.findByRole('button', { name: /create new library/i });
expect(createButton).toBeInTheDocument();
createButton.click();
// Should open the create library modal
expect(await screen.findByText('Create new library')).toBeInTheDocument();
// Cancel and close the create library modal
const cancelButton = screen.getByRole('button', { name: /cancel/i });
cancelButton.click();
await waitFor(() => {
expect(screen.queryByText('Create new library')).not.toBeInTheDocument();
});
// Open the modal again and create a new library
createButton.click();
const titleInput = await screen.findByRole('textbox', { name: /library name/i });
await user.click(titleInput);
await user.type(titleInput, 'Test Library Name');
const orgInput = await screen.findByRole('combobox', { name: /organization/i });
await user.click(orgInput);
await user.type(orgInput, 'org1');
await user.tab();
const slugInput = await screen.findByRole('textbox', { name: /library id/i });
await user.click(slugInput);
await user.type(slugInput, 'test_library_slug');
const confirmButton = await screen.findByRole('button', { name: 'Create' });
confirmButton.click();
await waitFor(() => {
expect(axiosMock.history.post.length).toBe(1);
});
expect(axiosMock.history.post[0].data).toBe(
'{"description":"","title":"Test Library Name","org":"org1","slug":"test_library_slug"}',
);
// The library should be checked
expect(screen.getByRole('radio', { name: /test library 1/i })).toBeChecked();
});
it('should confirm migration', async () => {
const user = userEvent.setup();
axiosMock.onPost(bulkMigrateLegacyLibrariesUrl()).reply(200);
renderPage();
expect(await screen.findByText('Migrate Legacy Libraries')).toBeInTheDocument();
expect(await screen.findByText('MBA')).toBeInTheDocument();
// The filter is 'unmigrated' by default.
// Clear the filter to select all libraries
const filterButton = screen.getByRole('button', { name: /unmigrated/i });
await user.click(filterButton);
const clearButton = await screen.findByRole('button', { name: /clear filter/i });
await user.click(clearButton);
const legacyLibrary1 = screen.getByRole('checkbox', { name: 'MBA' });
const legacyLibrary2 = screen.getByRole('checkbox', { name: /legacy library 1 imported library/i });
const legacyLibrary3 = screen.getByRole('checkbox', { name: 'MBA 1' });
legacyLibrary1.click();
legacyLibrary2.click();
legacyLibrary3.click();
const nextButton = screen.getByRole('button', { name: /next/i });
await user.click(nextButton);
// Should show alert of SelectDestinationView
expect(await screen.findByText(/you selected will be migrated to this new library/i)).toBeInTheDocument();
expect(await screen.findByText('Test Library 1')).toBeInTheDocument();
const radioButton = screen.getByRole('radio', { name: /test library 1/i });
await user.click(radioButton);
await user.click(nextButton);
// Should show alert of ConfirmationView
const alert = await screen.findByRole('alert');
expect(await within(alert).findByText(
/All content from the 3 legacy libraries you selected will be migrated to the Content Library you select/i,
)).toBeInTheDocument();
expect(screen.getByText('MBA')).toBeInTheDocument();
expect(screen.getByText('Legacy library 1')).toBeInTheDocument();
expect(screen.getByText('MBA 1')).toBeInTheDocument();
expect(screen.getByText(
/Previously migrated library. Any problem bank links were already moved will be migrated to/i,
)).toBeInTheDocument();
const confirmButton = screen.getByRole('button', { name: /confirm/i });
confirmButton.click();
await waitFor(() => {
expect(axiosMock.history.post.length).toBe(1);
});
expect(axiosMock.history.post[0].data).toBe(
'{"sources":["library-v1:MBA+123","library-v1:UNIX+LG1","library-v1:MBA+1234"],"target":"lib:SampleTaxonomyOrg1:TL1","create_collections":true,"repeat_handling_strategy":"fork"}',
);
expect(mockShowToast).toHaveBeenCalledWith('3 legacy libraries are being migrated.');
});
it('should show error when confirm migration', async () => {
const user = userEvent.setup();
axiosMock.onPost(bulkMigrateLegacyLibrariesUrl()).reply(400);
renderPage();
expect(await screen.findByText('Migrate Legacy Libraries')).toBeInTheDocument();
expect(await screen.findByText('MBA')).toBeInTheDocument();
// The filter is 'unmigrated' by default.
// Clear the filter to select all libraries
const filterButton = screen.getByRole('button', { name: /unmigrated/i });
await user.click(filterButton);
const clearButton = await screen.findByRole('button', { name: /clear filter/i });
await user.click(clearButton);
const legacyLibrary1 = screen.getByRole('checkbox', { name: 'MBA' });
const legacyLibrary2 = screen.getByRole('checkbox', { name: /legacy library 1 imported library/i });
const legacyLibrary3 = screen.getByRole('checkbox', { name: 'MBA 1' });
legacyLibrary1.click();
legacyLibrary2.click();
legacyLibrary3.click();
const nextButton = screen.getByRole('button', { name: /next/i });
await user.click(nextButton);
// Should show alert of SelectDestinationView
expect(await screen.findByText(/you selected will be migrated to this new library/i)).toBeInTheDocument();
expect(await screen.findByText('Test Library 1')).toBeInTheDocument();
const radioButton = screen.getByRole('radio', { name: /test library 1/i });
await user.click(radioButton);
await user.click(nextButton);
// Should show alert of ConfirmationView
const alert = await screen.findByRole('alert');
expect(await within(alert).findByText(
/All content from the 3 legacy libraries you selected will be migrated to the Content Library you select/i,
{ exact: false },
)).toBeInTheDocument();
expect(screen.getByText('MBA')).toBeInTheDocument();
expect(screen.getByText('Legacy library 1')).toBeInTheDocument();
expect(screen.getByText('MBA 1')).toBeInTheDocument();
expect(screen.getByText(
/Previously migrated library. Any problem bank links were already moved will be migrated to/i,
)).toBeInTheDocument();
const confirmButton = screen.getByRole('button', { name: /confirm/i });
confirmButton.click();
await waitFor(() => {
expect(axiosMock.history.post.length).toBe(1);
});
expect(axiosMock.history.post[0].data).toBe(
'{"sources":["library-v1:MBA+123","library-v1:UNIX+LG1","library-v1:MBA+1234"],"target":"lib:SampleTaxonomyOrg1:TL1","create_collections":true,"repeat_handling_strategy":"fork"}',
);
expect(mockShowToast).toHaveBeenCalledWith('Legacy libraries migration have failed');
});
it('should show help sidebar', async () => {
renderPage();
expect(await screen.findByText('Help & Support')).toBeInTheDocument();
expect(screen.getByText('What’s different in the new Content Libraries experience?')).toBeInTheDocument();
expect(screen.getByText('What happens when I migrate my Legacy Libraries?')).toBeInTheDocument();
expect(screen.getByText('How do I migrate my Legacy Libraries?')).toBeInTheDocument();
});
});