Skip to content

Commit 277bcbb

Browse files
committed
test: more tests
1 parent af4384b commit 277bcbb

3 files changed

Lines changed: 28 additions & 14 deletions

File tree

src/generic/DraggableList/DraggableList.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ DraggableList.propTypes = {
9595
children: PropTypes.node.isRequired,
9696
renderOverlay: PropTypes.func,
9797
activeId: PropTypes.string,
98-
onDragEnd: PropTypes.func,
98+
setActiveId: PropTypes.func,
9999
};
100100

101101
export default DraggableList;

src/library-authoring/units/LibraryUnitPage.test.tsx

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import userEvent from '@testing-library/user-event';
2+
import { act } from 'react';
23
import {
34
fireEvent,
45
initializeMocks,
@@ -18,14 +19,11 @@ import { mockContentSearchConfig, mockGetBlockTypes } from '../../search-manager
1819
import { mockClipboardEmpty } from '../../generic/data/api.mock';
1920
import LibraryLayout from '../LibraryLayout';
2021
import { getLibraryContainerChildrenApiUrl } from '../data/api';
21-
import { RequestStatus } from '../../data/constants';
22-
import { closestCorners } from '@dnd-kit/core';
2322
import { ToastActionData } from '../../generic/toast-context';
24-
import { act } from 'react';
2523

2624
const path = '/library/:libraryId/*';
2725
const libraryTitle = mockContentLibrary.libraryData.title;
28-
let axiosMock: import("axios-mock-adapter/types");
26+
let axiosMock: import('axios-mock-adapter/types');
2927
let mockShowToast: (message: string, action?: ToastActionData | undefined) => void;
3028

3129
mockClipboardEmpty.applyMock();
@@ -37,16 +35,15 @@ mockContentLibrary.applyMock();
3735
mockXBlockFields.applyMock();
3836
mockLibraryBlockMetadata.applyMock();
3937

38+
const closestCenter = jest.fn();
4039
jest.mock('@dnd-kit/core', () => ({
4140
...jest.requireActual('@dnd-kit/core'),
4241
// Since jsdom (used by jest) does not support getBoundingClientRect function
4342
// which is required for drag-n-drop calculations, we mock closestCorners fn
4443
// from dnd-kit to return collided elements as per the test. This allows us to
4544
// test all drag-n-drop handlers.
46-
closestCorners: jest.fn(),
45+
closestCenter: () => closestCenter(),
4746
}));
48-
// eslint-disable-next-line no-promise-executor-return
49-
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
5047

5148
describe('<LibraryUnitPage />', () => {
5249
beforeEach(() => {
@@ -139,12 +136,29 @@ describe('<LibraryUnitPage />', () => {
139136
axiosMock
140137
.onPatch(getLibraryContainerChildrenApiUrl(mockGetContainerMetadata.containerId))
141138
.reply(200);
142-
closestCorners.mockReturnValue([{ id: "lb:org1:Demo_course:html:text-1" }]);
139+
closestCenter.mockReturnValue([{ id: 'lb:org1:Demo_course:html:text-1' }]);
140+
await act(async () => {
141+
fireEvent.keyDown(firstDragHandle, { code: 'Space' });
142+
});
143+
await act(async () => {
144+
fireEvent.keyDown(firstDragHandle, { code: 'Space' });
145+
});
146+
await waitFor(() => expect(mockShowToast).toHaveBeenLastCalledWith('Order updated'));
147+
});
148+
149+
it('should show toast error message on update order failure', async () => {
150+
renderLibraryUnitPage();
151+
const firstDragHandle = (await screen.findAllByRole('button', { name: 'Drag to reorder' }))[0];
152+
axiosMock
153+
.onPatch(getLibraryContainerChildrenApiUrl(mockGetContainerMetadata.containerId))
154+
.reply(500);
155+
closestCenter.mockReturnValue([{ id: 'lb:org1:Demo_course:html:text-1' }]);
143156
await act(async () => {
144157
fireEvent.keyDown(firstDragHandle, { code: 'Space' });
145-
await sleep(1);
146-
fireEvent.keyUp(firstDragHandle, { code: 'Space' });
147-
})
148-
await waitFor(() => expect(mockShowToast).toHaveBeenLastCalledWith('test'));
158+
});
159+
await act(async () => {
160+
fireEvent.keyDown(firstDragHandle, { code: 'Space' });
161+
});
162+
await waitFor(() => expect(mockShowToast).toHaveBeenLastCalledWith('Failed to update components order'));
149163
});
150164
});

src/library-authoring/units/LibraryUnitPage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ export const LibraryUnitPage = () => {
132132
return <NotFoundAlert />;
133133
}
134134

135+
// istanbul ignore if
135136
if (isError) {
136-
// istanbul ignore next
137137
return <ErrorAlert error={error} />;
138138
}
139139

0 commit comments

Comments
 (0)