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
285 lines (243 loc) · 9.42 KB
/
ImportStepperPage.test.tsx
File metadata and controls
285 lines (243 loc) · 9.42 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
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 { useGetBlockTypes } from '@src/search-manager';
import { bulkModulestoreMigrateUrl } from '@src/data/api';
import { useLibraryBlockLimits } from '@src/library-authoring/data/apiHooks';
import { ImportStepperPage } from './ImportStepperPage';
let axiosMock;
mockGetMigrationInfo.applyMock();
mockContentLibrary.applyMock();
type StudioHomeState = DeprecatedReduxState['studioHome'];
const libraryKey = mockContentLibrary.libraryId;
const numPages = 1;
const coursesCount = studioHomeMock.courses.length;
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 }),
}));
jest.mock('@src/library-authoring/data/apiHooks', () => ({
...jest.requireActual('@src/library-authoring/data/apiHooks'),
useLibraryBlockLimits: 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: studioHomeMock.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 () => {
renderComponent();
// Renders the stepper header
expect(await screen.findByText('Select Course')).toBeInTheDocument();
expect(await screen.findByText('Review Import Details')).toBeInTheDocument();
// Renders the course list and previously imported chip
expect(screen.getByText(/managing risk in the information age/i)).toBeInTheDocument();
expect(screen.getByText(/run 0/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 () => {
(useLibraryBlockLimits as jest.Mock).mockReturnValue({
isPending: false,
data: { maxBlocksPerContentLibrary: 100 },
});
const user = userEvent.setup();
renderComponent();
axiosMock.onGet(getCourseDetailsApiUrl('course-v1:HarvardX+123+2023')).reply(200, {
courseId: 'course-v1:HarvardX+123+2023',
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);
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 () => {
(useLibraryBlockLimits as jest.Mock).mockReturnValue({
isPending: false,
data: { maxBlocksPerContentLibrary: 20 },
});
(useGetBlockTypes as jest.Mock).mockImplementation((args) => {
// Block types query for children of unsupported blocks
if (args.length === 2) {
return {
isPending: false,
data: {},
};
}
// Block types query from the course
if (args[0] === 'context_key = "course-v1:HarvardX+123+2023"') {
return {
isPending: false,
data: {
chapter: 1,
sequential: 2,
vertical: 3,
'problem-builder': 1,
html: 25,
},
};
}
return {
isPending: true,
data: null,
};
});
const user = userEvent.setup();
renderComponent();
axiosMock.onGet(getCourseDetailsApiUrl('course-v1:HarvardX+123+2023')).reply(200, {
courseId: 'course-v1:HarvardX+123+2023',
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 20 items/i,
)).toBeInTheDocument();
expect(screen.getByRole('button', { name: /import course/i })).toBeDisabled();
});
it('the course should remain selected on back', async () => {
const user = userEvent.setup();
renderComponent();
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);
const backButton = await screen.findByRole('button', { name: /back/i });
await user.click(backButton);
expect(screen.getByText(/managing risk in the information age/i)).toBeInTheDocument();
expect(courseCard).toBeChecked();
});
it('should import selected course on button click', async () => {
(useGetBlockTypes as jest.Mock).mockReturnValue({
isPending: false,
data: {
chapter: 1,
sequential: 2,
vertical: 3,
html: 5,
problem: 3,
},
});
const user = userEvent.setup();
renderComponent();
axiosMock.onPost(bulkModulestoreMigrateUrl()).reply(200);
axiosMock.onGet(getCourseDetailsApiUrl('course-v1:HarvardX+123+2023')).reply(200, {
courseId: 'course-v1:HarvardX+123+2023',
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"}',
);
});
});