forked from openedx/frontend-app-authoring
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnitCard.test.tsx
More file actions
346 lines (299 loc) · 10.8 KB
/
UnitCard.test.tsx
File metadata and controls
346 lines (299 loc) · 10.8 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
import { getConfig, setConfig } from '@edx/frontend-platform';
import {
act, fireEvent, initializeMocks, render, screen, waitFor, within,
} from '@src/testUtils';
import { XBlock } from '@src/data/types';
import { Info } from '@openedx/paragon/icons';
import UnitCard from './UnitCard';
import cardMessages from '../card-header/messages';
import * as OutlineSidebarContext from '../outline-sidebar/OutlineSidebarContext';
import { OutlineInfoSidebar } from '../outline-sidebar/OutlineInfoSidebar';
const mockUseAcceptLibraryBlockChanges = jest.fn();
const mockUseIgnoreLibraryBlockChanges = jest.fn();
jest.mock('@src/course-unit/data/apiHooks', () => ({
useAcceptLibraryBlockChanges: () => ({
mutateAsync: mockUseAcceptLibraryBlockChanges,
}),
useIgnoreLibraryBlockChanges: () => ({
mutateAsync: mockUseIgnoreLibraryBlockChanges,
}),
}));
jest.mock('@src/CourseAuthoringContext', () => ({
useCourseAuthoringContext: () => ({
courseId: 5,
getUnitUrl: (id: string) => `/some/${id}`,
}),
}));
const section = {
id: 'block-v1:UNIX+UX1+2025_T3+type@section+block@0',
displayName: 'Section Name',
published: true,
visibilityState: 'live',
hasChanges: false,
highlights: ['highlight 1', 'highlight 2'],
actions: {
draggable: true,
childAddable: true,
deletable: true,
duplicable: true,
},
} satisfies Partial<XBlock> as XBlock;
const subsection = {
id: 'block-v1:UNIX+UX1+2025_T3+type@subsection+block@0',
displayName: 'Subsection Name',
published: true,
visibilityState: 'live',
hasChanges: false,
actions: {
draggable: true,
childAddable: true,
deletable: true,
duplicable: true,
},
} satisfies Partial<XBlock> as XBlock;
const unit = {
id: 'block-v1:UNIX+UX1+2025_T3+type@unit+block@0',
displayName: 'unit Name',
category: 'vertical',
published: true,
visibilityState: 'live',
hasChanges: false,
actions: {
draggable: true,
childAddable: true,
deletable: true,
duplicable: true,
},
isHeaderVisible: true,
upstreamInfo: {
readyToSync: true,
upstreamRef: 'lct:org1:lib1:unit:1',
versionSynced: 1,
versionAvailable: 2,
versionDeclined: null,
errorMessage: null,
downstreamCustomized: [] as string[],
upstreamName: 'Upstream',
},
} satisfies Partial<XBlock> as XBlock;
const renderComponent = (props?: object) => render(
<UnitCard
section={section}
subsection={subsection}
unit={unit}
index={1}
getPossibleMoves={jest.fn()}
onOrderChange={jest.fn()}
onOpenPublishModal={jest.fn()}
onOpenDeleteModal={jest.fn()}
onOpenUnlinkModal={jest.fn()}
onOpenConfigureModal={jest.fn()}
onEditSubmit={jest.fn()}
onDuplicateSubmit={jest.fn()}
isSelfPaced={false}
isCustomRelativeDatesActive={false}
discussionsSettings={{
providerType: '',
enableGradedUnits: false,
}}
{...props}
/>,
{
path: '/course/:courseId',
params: { courseId: '5' },
extraWrapper: OutlineSidebarContext.OutlineSidebarProvider,
},
);
describe('<UnitCard />', () => {
beforeEach(() => {
initializeMocks();
});
it('render UnitCard component correctly', async () => {
const { findByTestId } = renderComponent();
expect(await findByTestId('unit-card-header')).toBeInTheDocument();
expect(await findByTestId('unit-card-header__title-link')).toHaveAttribute(
'href',
'/some/block-v1:UNIX+UX1+2025_T3+type@unit+block@0',
);
// The card is not selected
const card = screen.getByTestId('unit-card');
expect(card).not.toHaveClass('outline-card-selected');
});
it('render UnitCard component in selected state', () => {
setConfig({
...getConfig(),
ENABLE_COURSE_OUTLINE_NEW_DESIGN: 'true',
});
const { container } = renderComponent();
expect(screen.getByTestId('unit-card-header')).toBeInTheDocument();
// The card is not selected
const card = screen.getByTestId('unit-card');
expect(card).not.toHaveClass('outline-card-selected');
// Get the <Row> that contains the card and click it to select the card
const el = container.querySelector('div.row.mx-0') as HTMLInputElement;
expect(el).not.toBeNull();
fireEvent.click(el!);
// The card is selected
expect(card).toHaveClass('outline-card-selected');
});
it('hides header based on isHeaderVisible flag', async () => {
const { queryByTestId } = renderComponent({
unit: {
...unit,
isHeaderVisible: false,
},
});
expect(queryByTestId('unit-card-header')).not.toBeInTheDocument();
});
it('hides duplicate & delete option based on duplicable & deletable action flag', async () => {
const { findByTestId } = renderComponent({
unit: {
...unit,
actions: {
draggable: true,
childAddable: false,
deletable: false,
duplicable: false,
},
},
});
const element = await findByTestId('unit-card');
const menu = await within(element).findByTestId('unit-card-header__menu-button');
await act(async () => fireEvent.click(menu));
expect(within(element).queryByTestId('unit-card-header__menu-duplicate-button')).not.toBeInTheDocument();
expect(within(element).queryByTestId('unit-card-header__menu-delete-button')).not.toBeInTheDocument();
});
it('hides move, duplicate & delete options if parent was imported from library', async () => {
const { findByTestId } = renderComponent({
subsection: {
...subsection,
upstreamInfo: {
readyToSync: true,
upstreamRef: 'lct:org1:lib1:subsection:1',
versionSynced: 1,
},
},
});
const element = await findByTestId('unit-card');
const menu = await within(element).findByTestId('unit-card-header__menu-button');
await act(async () => fireEvent.click(menu));
expect(within(element).queryByTestId('unit-card-header__menu-duplicate-button')).not.toBeInTheDocument();
expect(within(element).queryByTestId('unit-card-header__menu-delete-button')).not.toBeInTheDocument();
expect(
await within(element).findByTestId('unit-card-header__menu-move-up-button'),
).toHaveAttribute('aria-disabled', 'true');
expect(
await within(element).findByTestId('unit-card-header__menu-move-down-button'),
).toHaveAttribute('aria-disabled', 'true');
});
it('shows copy option based on enableCopyPasteUnits flag', async () => {
const { findByTestId } = renderComponent({
unit: {
...unit,
enableCopyPasteUnits: true,
},
});
const element = await findByTestId('unit-card');
const menu = await within(element).findByTestId('unit-card-header__menu-button');
await act(async () => fireEvent.click(menu));
expect(within(element).queryByText(cardMessages.menuCopy.defaultMessage)).toBeInTheDocument();
});
it('hides status badge for unscheduled units', async () => {
const { queryByRole } = renderComponent({
unit: {
...unit,
visibilityState: 'unscheduled',
hasChanges: false,
},
});
expect(queryByRole('status')).not.toBeInTheDocument();
});
it('should sync unit changes from upstream', async () => {
renderComponent();
expect(await screen.findByTestId('unit-card-header')).toBeInTheDocument();
// Click on sync button
const syncButton = screen.getByRole('button', { name: /update available - click to sync/i });
fireEvent.click(syncButton);
// Should open compare preview modal
expect(screen.getByRole('heading', { name: /preview changes: unit name/i })).toBeInTheDocument();
// Click on accept changes
const acceptChangesButton = screen.getByText(/accept changes/i);
fireEvent.click(acceptChangesButton);
await waitFor(() => expect(mockUseAcceptLibraryBlockChanges).toHaveBeenCalled());
});
it('should decline sync unit changes from upstream', async () => {
renderComponent();
expect(await screen.findByTestId('unit-card-header')).toBeInTheDocument();
// Click on sync button
const syncButton = screen.getByRole('button', { name: /update available - click to sync/i });
fireEvent.click(syncButton);
// Should open compare preview modal
expect(screen.getByRole('heading', { name: /preview changes: unit name/i })).toBeInTheDocument();
// Click on ignore changes
const ignoreChangesButton = screen.getByRole('button', { name: /ignore changes/i });
fireEvent.click(ignoreChangesButton);
// Should open the confirmation modal
expect(screen.getByRole('heading', { name: /ignore these changes\?/i })).toBeInTheDocument();
// Click on ignore button
const ignoreButton = screen.getByRole('button', { name: /ignore/i });
fireEvent.click(ignoreButton);
await waitFor(() => expect(mockUseIgnoreLibraryBlockChanges).toHaveBeenCalled());
});
it('should open legacy manage tags', async () => {
setConfig({
...getConfig(),
ENABLE_TAGGING_TAXONOMY_PAGES: 'true',
ENABLE_COURSE_OUTLINE_NEW_DESIGN: 'false',
});
renderComponent();
const element = await screen.findByTestId('unit-card');
const menu = await within(element).findByTestId('unit-card-header__menu-button');
fireEvent.click(menu);
const manageTagsBtn = await within(element).findByTestId('unit-card-header__menu-manage-tags-button');
expect(manageTagsBtn).toBeInTheDocument();
fireEvent.click(manageTagsBtn);
const drawer = await screen.findByRole('alert');
expect(within(drawer).getByText(/manage tags/i));
});
it('should open align sidebar', async () => {
const mockSetCurrentPageKey = jest.fn();
const testSidebarPage = {
component: OutlineInfoSidebar,
icon: Info,
title: '',
};
jest
.spyOn(OutlineSidebarContext, 'useOutlineSidebarContext')
.mockImplementation(() => ({
setCurrentPageKey: mockSetCurrentPageKey,
currentPageKey: 'info',
sidebarPages: {
info: testSidebarPage,
help: testSidebarPage,
add: testSidebarPage,
},
isOpen: true,
open: jest.fn(),
toggle: jest.fn(),
currentFlow: null,
startCurrentFlow: jest.fn(),
stopCurrentFlow: jest.fn(),
openContainerInfoSidebar: jest.fn(),
}));
setConfig({
...getConfig(),
ENABLE_TAGGING_TAXONOMY_PAGES: 'true',
ENABLE_COURSE_OUTLINE_NEW_DESIGN: 'true',
});
renderComponent();
const element = await screen.findByTestId('unit-card');
const menu = await within(element).findByTestId('unit-card-header__menu-button');
fireEvent.click(menu);
const manageTagsBtn = await within(element).findByTestId('unit-card-header__menu-manage-tags-button');
expect(manageTagsBtn).toBeInTheDocument();
fireEvent.click(manageTagsBtn);
await waitFor(() => {
expect(mockSetCurrentPageKey).toHaveBeenCalledWith('align', unit.id);
});
});
});