Skip to content

Commit

Permalink
refactor: updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PKulkoRaccoonGang committed Jan 16, 2025
1 parent b27fa3d commit 9c320e4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
22 changes: 16 additions & 6 deletions src/course-unit/CourseUnit.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ describe('<CourseUnit />', () => {
);

simulatePostMessageEvent(messageTypes.deleteXBlock, {
id: courseVerticalChildrenMock.children[0].block_id,
usageId: courseVerticalChildrenMock.children[0].block_id,
});

expect(getByText(/Delete this component?/i)).toBeInTheDocument();
Expand All @@ -257,10 +257,10 @@ describe('<CourseUnit />', () => {
const deleteButton = getAllByRole('button', { name: /Delete/i })
.find(({ classList }) => classList.contains('btn-primary'));

userEvent.click(cancelButton);
expect(cancelButton).toBeInTheDocument();

simulatePostMessageEvent(messageTypes.deleteXBlock, {
id: courseVerticalChildrenMock.children[0].block_id,
usageId: courseVerticalChildrenMock.children[0].block_id,
});

expect(getByRole('dialog')).toBeInTheDocument();
Expand Down Expand Up @@ -296,8 +296,12 @@ describe('<CourseUnit />', () => {

axiosMock
.onDelete(getXBlockBaseApiUrl(courseVerticalChildrenMock.children[0].block_id))
.replyOnce(200, { dummy: 'value' });
await executeThunk(deleteUnitItemQuery(courseId, blockId), store.dispatch);
.reply(200, { dummy: 'value' });
await executeThunk(deleteUnitItemQuery(
courseId,
courseVerticalChildrenMock.children[0].block_id,
simulatePostMessageEvent,
), store.dispatch);

const updatedCourseVerticalChildren = courseVerticalChildrenMock.children.filter(
child => child.block_id !== courseVerticalChildrenMock.children[0].block_id,
Expand Down Expand Up @@ -1392,7 +1396,11 @@ describe('<CourseUnit />', () => {

global.localStorage.setItem('staticFileNotices', JSON.stringify(clipboardMockResponse.staticFileNotices));
await executeThunk(fetchCourseSectionVerticalData(blockId), store.dispatch);
await executeThunk(createNewCourseXBlock(camelCaseObject(postXBlockBody), null, blockId), store.dispatch);
await executeThunk(createNewCourseXBlock(
camelCaseObject(postXBlockBody),
() => simulatePostMessageEvent(messageTypes.addXBlock, { data: updatedAncestorsChild }),
blockId,
), store.dispatch);
const errorFilesAlert = getByTestId('has-error-files-alert');

expect(within(errorFilesAlert)
Expand Down Expand Up @@ -1617,6 +1625,8 @@ describe('<CourseUnit />', () => {
callbackFn: requestData.callbackFn,
}), store.dispatch);

simulatePostMessageEvent(messageTypes.rollbackMovedXBlock, { locator: requestData.sourceLocator });

const dismissButton = queryByRole('button', {
name: /dismiss/i, hidden: true,
});
Expand Down
13 changes: 9 additions & 4 deletions src/course-unit/data/thunk.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { RequestStatus } from '../../data/constants';
import { NOTIFICATION_MESSAGES } from '../../constants';
import { updateModel, updateModels } from '../../generic/model-store';
import { updateClipboardData } from '../../generic/data/slice';
import { messageTypes } from '../constants';
import {
getCourseUnitData,
editUnitDisplayName,
Expand Down Expand Up @@ -219,14 +220,14 @@ export function fetchCourseVerticalChildrenData(itemId) {
};
}

export function deleteUnitItemQuery(itemId, xblockId, callback) {
export function deleteUnitItemQuery(itemId, xblockId, sendMessageToIframe) {
return async (dispatch) => {
dispatch(updateSavingStatus({ status: RequestStatus.PENDING }));
dispatch(showProcessingNotification(NOTIFICATION_MESSAGES.deleting));

try {
await deleteUnitItem(xblockId);
callback();
sendMessageToIframe(messageTypes.completeXBlockDeleting, null);
const { userClipboard } = await getCourseSectionVerticalData(itemId);
dispatch(updateClipboardData(userClipboard));
const courseUnit = await getCourseUnitData(itemId);
Expand Down Expand Up @@ -301,8 +302,12 @@ export function patchUnitItemQuery({
dispatch(updateMovedXBlockParams(xBlockParams));
dispatch(updateCourseOutlineInfo({}));
dispatch(updateCourseOutlineInfoLoadingStatus({ status: RequestStatus.IN_PROGRESS }));
const courseUnit = await getCourseUnitData(currentParentLocator);
dispatch(fetchCourseItemSuccess(courseUnit));
try {
const courseUnit = await getCourseUnitData(currentParentLocator);
dispatch(fetchCourseItemSuccess(courseUnit));
} catch (error) {
handleResponseErrors(error, dispatch, updateSavingStatus);
}
callbackFn(sourceLocator);
} catch (error) {
handleResponseErrors(error, dispatch, updateSavingStatus);
Expand Down
6 changes: 1 addition & 5 deletions src/course-unit/hooks.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,7 @@ export const useCourseUnit = ({ courseId, blockId }) => {

const unitXBlockActions = {
handleDelete: (XBlockId) => {
dispatch(deleteUnitItemQuery(
blockId,
XBlockId,
() => sendMessageToIframe(messageTypes.completeXBlockDeleting, null),
));
dispatch(deleteUnitItemQuery(blockId, XBlockId, sendMessageToIframe));
},
handleDuplicate: (XBlockId) => {
dispatch(duplicateUnitItemQuery(
Expand Down

0 comments on commit 9c320e4

Please sign in to comment.