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 f290f99
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
16 changes: 11 additions & 5 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 @@ -1617,6 +1621,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
17 changes: 12 additions & 5 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 @@ -162,7 +163,7 @@ export function editCourseUnitVisibilityAndData(
};
}

export function createNewCourseXBlock(body, callback, blockId) {
export function createNewCourseXBlock(body, callback, blockId, sendMessageToIframe) {
return async (dispatch) => {
if (body.stagedContent) {
dispatch(showProcessingNotification(NOTIFICATION_MESSAGES.pasting));
Expand Down Expand Up @@ -191,6 +192,8 @@ export function createNewCourseXBlock(body, callback, blockId) {
dispatch(hideProcessingNotification());
if (callback) {
callback(result);
} else {
sendMessageToIframe(messageTypes.addXBlock, { data: result });
}
const currentBlockId = body.category === 'vertical' ? formattedResult.locator : blockId;
const courseUnit = await getCourseUnitData(currentBlockId);
Expand Down Expand Up @@ -219,14 +222,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 +304,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
15 changes: 3 additions & 12 deletions src/course-unit/hooks.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,22 +113,13 @@ export const useCourseUnit = ({ courseId, blockId }) => {
}
};

const handleCreateNewCourseXBlock = (
body,
callback = (result) => {
sendMessageToIframe(messageTypes.addXBlock, { data: result });
},
) => (
dispatch(createNewCourseXBlock(body, callback, blockId))
const handleCreateNewCourseXBlock = (body, callback) => (
dispatch(createNewCourseXBlock(body, callback, blockId, sendMessageToIframe))
);

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 f290f99

Please sign in to comment.