Skip to content

Commit 812d4fc

Browse files
Updated the test based on reviwer feedback
1 parent 03c1662 commit 812d4fc

1 file changed

Lines changed: 58 additions & 18 deletions

File tree

  • contentcuration/contentcuration/frontend/channelEdit/views/trash/__tests__

contentcuration/contentcuration/frontend/channelEdit/views/trash/__tests__/trashModal.spec.js

Lines changed: 58 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
import { render, screen, waitFor, configure } from '@testing-library/vue';
1+
import { render, screen, waitFor, configure, within } from '@testing-library/vue';
22
import userEvent from '@testing-library/user-event';
33
import VueRouter from 'vue-router';
44

55
import { factory } from '../../../store';
66
import { RouteNames } from '../../../constants';
77
import TrashModal from '../TrashModal';
88

9-
const store = factory();
10-
119
const TRASH_ID = 'trash-root-id';
1210

1311
const testChildren = [
@@ -16,19 +14,23 @@ const testChildren = [
1614
{ id: 'test3', title: 'Topic', kind: 'topic', modified: new Date(2020, 1, 1) },
1715
];
1816

19-
async function makeWrapper(items = testChildren, isLoading = false) {
17+
async function makeWrapper(items = testChildren, { isLoading = false, hasMore = false } = {}) {
18+
const store = factory();
19+
2020
const loadContentNodesSpy = jest
2121
.spyOn(TrashModal.methods, 'loadContentNodes')
2222
.mockResolvedValue({});
2323
jest.spyOn(TrashModal.methods, 'loadAncestors').mockResolvedValue();
2424
jest.spyOn(TrashModal.methods, 'removeContentNodes').mockResolvedValue();
25+
// loadNodes is not mocked intentionally: we let the real implementation run so that
26+
// the loading state and `more` data are exercised through the mocked loadChildren below.
2527
const loadNodesSpy = jest.spyOn(TrashModal.methods, 'loadNodes');
2628

2729
if (isLoading) {
2830
jest.spyOn(TrashModal.methods, 'loadChildren').mockReturnValue(new Promise(() => {}));
2931
} else {
3032
jest.spyOn(TrashModal.methods, 'loadChildren').mockResolvedValue({
31-
more: items === testChildren ? null : { parent: TRASH_ID, page: 2 },
33+
more: hasMore ? { parent: TRASH_ID, page: 2 } : null,
3234
results: [],
3335
});
3436
}
@@ -79,7 +81,7 @@ async function makeWrapper(items = testChildren, isLoading = false) {
7981
}
8082

8183
const user = userEvent.setup();
82-
return { ...utils, routerPush, user, loadNodesSpy, loadContentNodesSpy };
84+
return { ...utils, routerPush, user, loadNodesSpy, loadContentNodesSpy, store };
8385
}
8486

8587
describe('TrashModal', () => {
@@ -92,7 +94,7 @@ describe('TrashModal', () => {
9294

9395
describe('on load', () => {
9496
it('shows a loading indicator while content is loading', async () => {
95-
await makeWrapper(testChildren, true);
97+
await makeWrapper(testChildren, { isLoading: true });
9698
expect(screen.getByTestId('loading')).toBeInTheDocument();
9799
});
98100

@@ -114,7 +116,7 @@ describe('TrashModal', () => {
114116
expect(screen.getByTestId('delete')).toBeDisabled();
115117
expect(screen.getByTestId('restore')).toBeDisabled();
116118

117-
await user.click(screen.getAllByRole('checkbox')[1]);
119+
await user.click(within(screen.getAllByTestId('checkbox')[0]).getByRole('checkbox'));
118120

119121
await waitFor(() => {
120122
expect(screen.getByTestId('delete')).toBeEnabled();
@@ -127,12 +129,23 @@ describe('TrashModal', () => {
127129
await user.click(screen.getByTestId('selectall'));
128130

129131
await waitFor(() => {
130-
screen
131-
.getAllByRole('checkbox')
132-
.slice(1)
133-
.forEach(cb => {
134-
expect(cb).toBeChecked();
135-
});
132+
screen.getAllByTestId('checkbox').forEach(container => {
133+
expect(within(container).getByRole('checkbox')).toBeChecked();
134+
});
135+
});
136+
});
137+
138+
it('clicking an item link sets previewNodeId, opening the ResourceDrawer', async () => {
139+
const { user } = await makeWrapper();
140+
141+
const itemLinks = screen.getAllByTestId('item');
142+
await user.click(itemLinks[0]);
143+
144+
await waitFor(() => {
145+
expect(document.querySelector('resourcedrawer-stub')).toHaveAttribute(
146+
'nodeid',
147+
testChildren[0].id,
148+
);
136149
});
137150
});
138151
});
@@ -197,9 +210,10 @@ describe('TrashModal', () => {
197210

198211
it('successful deletion triggers snackbar and reloads nodes', async () => {
199212
jest.spyOn(TrashModal.methods, 'deleteContentNodes').mockResolvedValue();
213+
214+
const { user, loadNodesSpy, store } = await makeWrapper();
200215
const dispatchSpy = jest.spyOn(store, 'dispatch').mockImplementation(() => Promise.resolve());
201216

202-
const { user, loadNodesSpy } = await makeWrapper();
203217
await user.click(screen.getByTestId('selectall'));
204218
await user.click(screen.getByTestId('delete'));
205219
await user.click(await screen.findByRole('button', { name: /Delete permanently/i }));
@@ -225,6 +239,32 @@ describe('TrashModal', () => {
225239
await user.click(screen.getByTestId('restore'));
226240
expect(await screen.findByRole('button', { name: /Move here/i })).toBeInTheDocument();
227241
});
242+
243+
it('successful restore clears selection and previewNodeId, and reloads nodes', async () => {
244+
jest.spyOn(TrashModal.methods, 'moveContentNodes').mockResolvedValue();
245+
246+
const { user, loadNodesSpy } = await makeWrapper();
247+
248+
await user.click(screen.getByTestId('selectall'));
249+
250+
const itemLinks = screen.getAllByTestId('item');
251+
await user.click(itemLinks[0]);
252+
253+
await user.click(screen.getByTestId('restore'));
254+
255+
await user.click(await screen.findByRole('button', { name: /Move here/i }));
256+
257+
await waitFor(() => {
258+
expect(loadNodesSpy).toHaveBeenCalled();
259+
260+
screen.getAllByTestId('checkbox').forEach(container => {
261+
expect(within(container).getByRole('checkbox')).not.toBeChecked();
262+
});
263+
264+
const drawer = document.querySelector('resourcedrawer-stub');
265+
expect(drawer).not.toHaveAttribute('nodeid');
266+
});
267+
});
228268
});
229269

230270
describe('selection count', () => {
@@ -241,18 +281,18 @@ describe('TrashModal', () => {
241281

242282
describe('pagination', () => {
243283
it('shows a Show more button when there is more paginated content', async () => {
244-
await makeWrapper([testChildren[0]]);
284+
await makeWrapper([testChildren[0]], { hasMore: true });
245285
expect(await screen.findByRole('button', { name: /Show more/i })).toBeInTheDocument();
246286
});
247287

248288
it('clicking Show more calls loadContentNodes with pagination params', async () => {
249-
const { user, loadContentNodesSpy } = await makeWrapper([testChildren[0]]);
289+
const { user, loadContentNodesSpy } = await makeWrapper([testChildren[0]], { hasMore: true });
250290
const showMoreBtn = await screen.findByRole('button', { name: /Show more/i });
251291

252292
await user.click(showMoreBtn);
253293

254294
await waitFor(() => {
255-
expect(loadContentNodesSpy).toHaveBeenCalledWith({ parent: TRASH_ID, page: 2 });
295+
expect(loadContentNodesSpy).toHaveBeenLastCalledWith({ parent: TRASH_ID, page: 2 });
256296
});
257297
});
258298
});

0 commit comments

Comments
 (0)