forked from openedx/frontend-app-authoring
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContainerDeleter.test.tsx
More file actions
172 lines (152 loc) · 5.97 KB
/
ContainerDeleter.test.tsx
File metadata and controls
172 lines (152 loc) · 5.97 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
import type { ToastActionData } from '@src/generic/toast-context';
import {
fireEvent,
render,
screen,
initializeMocks,
waitFor,
} from '@src/testUtils';
import { mockContentSearchConfig, mockSearchResult, hydrateSearchResult } from '@src/search-manager/data/api.mock';
import { LibraryProvider } from '../common/context/LibraryContext';
import { SidebarProvider } from '../common/context/SidebarContext';
import {
mockContentLibrary,
mockGetContainerMetadata,
mockDeleteContainer,
mockRestoreContainer,
mockGetEntityLinks,
} from '../data/api.mocks';
import ContainerDeleter from './ContainerDeleter';
mockContentLibrary.applyMock(); // Not required, but avoids 404 errors in the logs when <LibraryProvider> loads data
mockContentSearchConfig.applyMock();
mockGetEntityLinks.applyMock();
const mockDelete = mockDeleteContainer.applyMock();
const mockRestore = mockRestoreContainer.applyMock();
const { libraryId } = mockContentLibrary;
const getContainerDetails = (context: string) => {
switch (context) {
case 'unit':
return { containerId: mockGetContainerMetadata.unitId, parent: 'subsection' };
case 'subsection':
return { containerId: mockGetContainerMetadata.subsectionId, parent: 'section' };
case 'section':
return { containerId: mockGetContainerMetadata.sectionId, parent: null };
default:
return { containerId: mockGetContainerMetadata.unitId, parent: 'subsection' };
}
};
const renderArgs = {
path: '/library/:libraryId',
params: { libraryId },
extraWrapper: ({ children }) => (
<LibraryProvider libraryId={libraryId}>
<SidebarProvider>
{ children }
</SidebarProvider>
</LibraryProvider>
),
};
let mockShowToast: { (message: string, action?: ToastActionData): void; mock?: any; };
[
'unit' as const,
'section' as const,
'subsection' as const,
].forEach((context) => {
describe('<ContainerDeleter />', () => {
beforeEach(() => {
const mocks = initializeMocks();
mockShowToast = mocks.mockShowToast;
mockSearchResult(hydrateSearchResult([{
blockType: context,
displayName: `Test ${context}`,
}]));
});
it(`<${context}> should show a confirmation prompt the card with title and description`, async () => {
const mockCancel = jest.fn();
const { containerId } = getContainerDetails(context);
render(<ContainerDeleter
containerId={containerId}
close={mockCancel}
/>, renderArgs);
const modal = await screen.findByRole('dialog', { name: new RegExp(`Delete ${context}`, 'i') });
expect(modal).toBeVisible();
// It should mention the component's name in the confirm dialog:
await screen.findByText(`Test ${context}`);
// Clicking cancel will cancel:
const cancelButton = screen.getByRole('button', { name: 'Cancel' });
fireEvent.click(cancelButton);
expect(mockCancel).toHaveBeenCalled();
});
it(`<${context}> deletes the block when confirmed, shows a toast with undo option and restores block on undo`, async () => {
const mockCancel = jest.fn();
const { containerId } = getContainerDetails(context);
render(<ContainerDeleter containerId={containerId} close={mockCancel} />, renderArgs);
const modal = await screen.findByRole('dialog', { name: new RegExp(`Delete ${context}`, 'i') });
expect(modal).toBeVisible();
const deleteButton = await screen.findByRole('button', { name: 'Delete' });
fireEvent.click(deleteButton);
await waitFor(() => {
expect(mockDelete).toHaveBeenCalled();
});
expect(mockCancel).toHaveBeenCalled(); // In order to close the modal, this also gets called.
expect(mockShowToast).toHaveBeenCalled();
// Get restore / undo func from the toast
const restoreFn = mockShowToast.mock.calls[0][1].onClick;
restoreFn();
await waitFor(() => {
expect(mockRestore).toHaveBeenCalled();
expect(mockShowToast).toHaveBeenCalledWith('Undo successful');
});
});
it(`<${context}> should show parents message if parent data is set with one parent`, async () => {
const mockCancel = jest.fn();
const { containerId, parent } = getContainerDetails(context);
if (!parent) {
return;
}
mockSearchResult(hydrateSearchResult([{
[`${parent}s`]: {
displayName: [`${parent} 1`],
key: [`${parent}1`],
},
blockType: context,
}]));
render(
<ContainerDeleter
containerId={containerId}
close={mockCancel}
/>,
renderArgs,
);
const modal = await screen.findByRole('dialog', { name: new RegExp(`Delete ${context}`, 'i') });
expect(modal).toBeVisible();
const textMatch = new RegExp(`By deleting this ${context}, you will also be deleting it from ${parent} 1 in this library.`);
expect((await screen.findAllByText((_, element) => textMatch.test(element?.textContent || ''))).length).toBeGreaterThan(0);
});
it(`<${context}> should show parents message if parents is set with multiple parents`, async () => {
const mockCancel = jest.fn();
const { containerId, parent } = getContainerDetails(context);
if (!parent) {
return;
}
mockSearchResult(hydrateSearchResult([{
[`${parent}s`]: {
displayName: [`${parent} 1`, `${parent} 2`],
key: [`${parent}1`, `${parent}2`],
},
blockType: context,
}]));
render(
<ContainerDeleter
containerId={containerId}
close={mockCancel}
/>,
renderArgs,
);
const modal = await screen.findByRole('dialog', { name: new RegExp(`Delete ${context}`, 'i') });
expect(modal).toBeVisible();
const textMatch = new RegExp(`By deleting this ${context}, you will also be deleting it from 2 ${parent}s in this library.`, 'i');
expect((await screen.findAllByText((_, element) => textMatch.test(element?.textContent || ''))).length).toBeGreaterThan(0);
});
});
});