forked from openedx/frontend-app-authoring
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImportStepperPage.test.tsx
More file actions
332 lines (279 loc) · 12.4 KB
/
ImportStepperPage.test.tsx
File metadata and controls
332 lines (279 loc) · 12.4 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
import userEvent from '@testing-library/user-event';
import {
initializeMocks,
render,
screen,
waitFor,
} from '@src/testUtils';
import { initialState } from '@src/studio-home/factories/mockApiResponses';
import { RequestStatus } from '@src/data/constants';
import { type DeprecatedReduxState } from '@src/store';
import studioHomeMock from '@src/studio-home/__mocks__/studioHomeMock';
import { getCourseDetailsApiUrl } from '@src/course-outline/data/api';
import { LibraryProvider } from '@src/library-authoring/common/context/LibraryContext';
import { mockContentLibrary, mockGetMigrationInfo } from '@src/library-authoring/data/api.mocks';
import { bulkModulestoreMigrateUrl } from '@src/data/api';
import { mockGetPreviewModulestoreMigration } from '@src/data/api.mocks';
import { ImportStepperPage } from './ImportStepperPage';
let axiosMock;
mockGetMigrationInfo.applyMock();
mockContentLibrary.applyMock();
mockGetPreviewModulestoreMigration.applyMock();
type StudioHomeState = DeprecatedReduxState['studioHome'];
const libraryKey = mockContentLibrary.libraryId;
const numPages = 1;
const coursesCount = studioHomeMock.courses.length;
const courses = [
{
courseKey: mockGetPreviewModulestoreMigration.sourceKeyGood,
displayName: 'Managing Risk in the Information Age',
lmsLink: '//localhost:18000/courses/course-v1:HarvardX+123+2023/jump_to/block-v1:HarvardX+123+2023+type@course+block@course',
number: '123',
org: 'HarvardX',
rerunLink: '/course_rerun/course-v1:HarvardX+123+2023',
run: '2023',
url: '/course/course-v1:HarvardX+123+2023',
},
{
courseKey: mockGetPreviewModulestoreMigration.sourceKeyBlockLimit,
displayName: 'Course with a lot of components',
lmsLink: '//localhost:18000/courses/course-v1:HarvardX+123+2023/jump_to/block-v1:HarvardX+123+2023+type@course+block@course',
number: '3',
org: 'HarvardX',
rerunLink: '/course_rerun/course-v1:HarvardX+123+2023',
run: '2023',
url: '/course/course-v1:HarvardX+123+2023',
},
{
courseKey: mockGetPreviewModulestoreMigration.sourceKeyBlockLoading,
displayName: 'Course with a loading',
lmsLink: '//localhost:18000/courses/course-v1:HarvardX+123+2023/jump_to/block-v1:HarvardX+123+2023+type@course+block@course',
number: '4',
org: 'HarvardX',
rerunLink: '/course_rerun/course-v1:HarvardX+123+2023',
run: '2023',
url: '/course/course-v1:HarvardX+123+2023',
},
];
const mockNavigate = jest.fn();
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useNavigate: () => mockNavigate,
}));
// Mock the useGetBlockTypes hook
jest.mock('@src/search-manager', () => ({
useGetBlockTypes: jest.fn().mockReturnValue({ isPending: true, data: null }),
useGetContentHits: jest.fn().mockReturnValue({ isPending: true, data: null }),
}));
const renderComponent = (studioHomeState: Partial<StudioHomeState> = {}) => {
// Generate a custom initial state based on studioHomeCoursesRequestParams
const customInitialState: Partial<DeprecatedReduxState> = {
...initialState,
studioHome: {
...initialState.studioHome,
studioHomeData: {
courses,
numPages,
coursesCount,
},
loadingStatuses: {
...initialState.studioHome.loadingStatuses,
courseLoadingStatus: RequestStatus.SUCCESSFUL,
},
...studioHomeState,
},
};
// Initialize the store with the custom initial state
const newMocks = initializeMocks({ initialState: customInitialState });
const store = newMocks.reduxStore;
axiosMock = newMocks.axiosMock;
return {
...render(
<ImportStepperPage />,
{
extraWrapper: ({ children }: { children: React.ReactNode }) => (
<LibraryProvider libraryId={libraryKey}>
{children}
</LibraryProvider>
),
path: '/libraries/:libraryId/import/course',
params: { libraryId: libraryKey },
},
),
store,
};
};
describe('<ImportStepperModal />', () => {
it('should render correctly', async () => {
const user = userEvent.setup();
renderComponent();
// Renders the stepper header
expect(await screen.findByText('Select Course')).toBeInTheDocument();
expect(await screen.findByText('Review Import Details')).toBeInTheDocument();
// Hides previously imported courses.
expect(screen.queryByText(/managing risk in the information age/i)).not.toBeInTheDocument();
expect(screen.queryByText('Previously Imported')).not.toBeInTheDocument();
// use filter modal to show previously imported courses.
await user.click(await screen.findByRole('button', { name: 'Filter settings' }));
await user.click(await screen.findByRole('checkbox', { name: 'Show Previously Imported Courses' }));
await user.click(await screen.findByRole('button', { name: 'Save' }));
// Renders previously imported courses and badge
expect(await screen.findByText(/managing risk in the information age/i)).toBeInTheDocument();
expect(await screen.findByText('Previously Imported')).toBeInTheDocument();
// Renders cancel and next step buttons
expect(screen.getByRole('button', { name: /cancel/i })).toBeInTheDocument();
expect(screen.getByRole('button', { name: /next step/i })).toBeInTheDocument();
});
it('should cancel the import', async () => {
const user = userEvent.setup();
renderComponent();
const cancelButon = await screen.findByRole('button', { name: /cancel/i });
await user.click(cancelButon);
expect(mockNavigate).toHaveBeenCalled();
});
it('should go to review import details step', async () => {
const user = userEvent.setup();
renderComponent();
const courseId = mockGetPreviewModulestoreMigration.sourceKeyBlockLoading;
axiosMock.onGet(getCourseDetailsApiUrl(courseId)).reply(200, {
courseId,
title: 'Managing Risk in the Information Age',
subtitle: '',
org: 'HarvardX',
description: 'This is a test course',
});
// use filter modal to show previously imported courses.
await user.click(await screen.findByRole('button', { name: 'Filter settings' }));
await user.click(await screen.findByRole('checkbox', { name: 'Show Previously Imported Courses' }));
await user.click(await screen.findByRole('button', { name: 'Save' }));
const nextButton = await screen.findByRole('button', { name: /next step/i });
expect(nextButton).toBeDisabled();
// Select a course
const courseCard = screen.getAllByRole('radio')[2];
await user.click(courseCard);
expect(courseCard).toBeChecked();
// Click next
expect(nextButton).toBeEnabled();
await user.click(nextButton);
await waitFor(async () => expect(await screen.findByText(
/managing risk in the information age is being analyzed for review prior to import/i,
)).toBeInTheDocument());
expect(await screen.findByText('Analysis Summary')).toBeInTheDocument();
// The import details is loading
expect(await screen.findByText('Import Analysis in Progress')).toBeInTheDocument();
});
it('should block import when content limit is reached', async () => {
const user = userEvent.setup();
renderComponent();
const courseId = mockGetPreviewModulestoreMigration.sourceKeyBlockLimit;
axiosMock.onGet(getCourseDetailsApiUrl(courseId)).reply(200, {
courseId,
title: 'Managing Risk in the Information Age',
subtitle: '',
org: 'HarvardX',
description: 'This is a test course',
});
const nextButton = await screen.findByRole('button', { name: /next step/i });
expect(nextButton).toBeDisabled();
// Select a course
const courseCard = screen.getAllByRole('radio')[0];
await user.click(courseCard);
expect(courseCard).toBeChecked();
// Click next
expect(nextButton).toBeEnabled();
await user.click(nextButton);
expect(await screen.findByText(/Import Blocked/i)).toBeInTheDocument();
expect(await screen.findByText(
/This import would exceed the Content Library limit of 1000 items/i,
)).toBeInTheDocument();
expect(screen.getByRole('button', { name: /import course/i })).toBeDisabled();
});
it('the course should remain selected on back only for non-imported courses', async () => {
const user = userEvent.setup();
renderComponent();
const nextButton = await screen.findByRole('button', { name: /next step/i });
expect(nextButton).toBeDisabled();
// Select a course
const courseCard = (await screen.findAllByRole('radio'))[0];
await user.click(courseCard);
expect(courseCard).toBeChecked();
// Click next
expect(nextButton).toBeEnabled();
await user.click(nextButton);
const backButton = await screen.findByRole('button', { name: /back/i });
await user.click(backButton);
expect(courseCard).toBeChecked();
expect(nextButton).toBeEnabled();
});
it('should hide previously imported courses on page change', async () => {
const user = userEvent.setup();
renderComponent();
// use filter modal to show previously imported courses.
await user.click(await screen.findByRole('button', { name: 'Filter settings' }));
await user.click(await screen.findByRole('checkbox', { name: 'Show Previously Imported Courses' }));
await user.click(await screen.findByRole('button', { name: 'Save' }));
const nextButton = await screen.findByRole('button', { name: /next step/i });
expect(nextButton).toBeDisabled();
// Select a course
const courseCard = (await screen.findAllByRole('radio'))[0];
await user.click(courseCard);
expect(courseCard).toBeChecked();
// Click next
expect(nextButton).toBeEnabled();
await user.click(nextButton);
const backButton = await screen.findByRole('button', { name: /back/i });
await user.click(backButton);
expect(screen.queryByText(/managing risk in the information age/i)).not.toBeInTheDocument();
expect((await screen.findAllByRole('radio'))[0]).not.toBeChecked();
// should be disabled
expect(await screen.findByRole('button', { name: /next step/i })).toBeDisabled();
});
it('should reset checkbox if modal is closed or cancelled without saving', async () => {
const user = userEvent.setup();
renderComponent();
// use filter modal to show previously imported courses.
await user.click(await screen.findByRole('button', { name: 'Filter settings' }));
await user.click(await screen.findByRole('checkbox', { name: 'Show Previously Imported Courses' }));
await user.click(await screen.findByRole('button', { name: 'Cancel' }));
await user.click(await screen.findByRole('button', { name: 'Filter settings' }));
const checkbox = await screen.findByRole('checkbox', { name: 'Show Previously Imported Courses' });
expect(checkbox).not.toBeChecked();
});
it('should import selected course on button click', async () => {
const user = userEvent.setup();
renderComponent();
// use filter modal to show previously imported courses.
await user.click(await screen.findByRole('button', { name: 'Filter settings' }));
await user.click(await screen.findByRole('checkbox', { name: 'Show Previously Imported Courses' }));
await user.click(await screen.findByRole('button', { name: 'Save' }));
axiosMock.onPost(bulkModulestoreMigrateUrl()).reply(200);
const courseId = mockGetPreviewModulestoreMigration.sourceKeyGood;
axiosMock.onGet(getCourseDetailsApiUrl(courseId)).reply(200, {
courseId,
title: 'Managing Risk in the Information Age',
subtitle: '',
org: 'HarvardX',
description: 'This is a test course',
});
const nextButton = await screen.findByRole('button', { name: /next step/i });
expect(nextButton).toBeDisabled();
// Select a course
const courseCard = screen.getAllByRole('radio')[0];
await user.click(courseCard);
expect(courseCard).toBeChecked();
// Click next
expect(nextButton).toBeEnabled();
await user.click(nextButton);
expect(await screen.findByText('Analysis Summary')).toBeInTheDocument();
// Analysis completed
expect(await screen.findByText('Import Analysis Completed: Reimport')).toBeInTheDocument();
const importBtn = await screen.findByRole('button', { name: 'Import Course' });
await user.click(importBtn);
await waitFor(() => {
expect(axiosMock.history.post.length).toBe(1);
});
expect(axiosMock.history.post[0].data).toBe(
'{"sources":["course-v1:HarvardX+123+2023"],"target":"lib:Axim:TEST","create_collections":true,"repeat_handling_strategy":"fork","composition_level":"section"}',
);
});
});