Skip to content

Commit

Permalink
refactor: updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PKulkoRaccoonGang committed Nov 22, 2024
1 parent a18a2e9 commit 832ebf4
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 2 deletions.
117 changes: 117 additions & 0 deletions src/course-unit/CourseUnit.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ const blockId = '567890';
const unitDisplayName = courseUnitIndexMock.metadata.display_name;
const mockedUsedNavigate = jest.fn();
const userName = 'openedx';
const handleConfigureSubmitMock = jest.fn();

const {
block_id: id,
user_partition_info: userPartitionInfo,
} = courseVerticalChildrenMock.children[0];
const userPartitionInfoFormatted = camelCaseObject(userPartitionInfo);

const postXBlockBody = {
parent_locator: blockId,
Expand Down Expand Up @@ -1653,4 +1660,114 @@ describe('<CourseUnit />', () => {
);
});
});

describe('xblock restrict access', () => {
it('opens xblock restrict access modal successfully', () => {
const {
getByTitle, getByTestId,
} = render(<RootWrapper />);

const modalSubtitleText = configureModalMessages.restrictAccessTo.defaultMessage;
const modalCancelBtnText = configureModalMessages.cancelButton.defaultMessage;
const modalSaveBtnText = configureModalMessages.saveButton.defaultMessage;

waitFor(() => {
const iframe = getByTitle(xblockContainerIframeMessages.xblockIframeTitle.defaultMessage);
expect(iframe).toBeInTheDocument();

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

waitFor(() => {
const configureModal = getByTestId('configure-modal');

expect(within(configureModal).getByText(modalSubtitleText)).toBeInTheDocument();
expect(within(configureModal).getByRole('button', { name: modalCancelBtnText })).toBeInTheDocument();
expect(within(configureModal).getByRole('button', { name: modalSaveBtnText })).toBeInTheDocument();
});
});

it('closes xblock restrict access modal when cancel button is clicked', async () => {
const {
getByTitle, queryByTestId, getByTestId,
} = render(<RootWrapper />);

waitFor(() => {
const iframe = getByTitle(xblockContainerIframeMessages.xblockIframeTitle.defaultMessage);
expect(iframe).toBeInTheDocument();
simulatePostMessageEvent(messageTypes.manageXBlockAccess, {
id: courseVerticalChildrenMock.children[0].block_id,
});
});

waitFor(() => {
const configureModal = getByTestId('configure-modal');
expect(configureModal).toBeInTheDocument();
userEvent.click(within(configureModal).getByRole('button', {
name: configureModalMessages.cancelButton.defaultMessage,
}));
expect(handleConfigureSubmitMock).not.toHaveBeenCalled();
});

expect(queryByTestId('configure-modal')).not.toBeInTheDocument();
});

it('handles submit xblock restrict access data when save button is clicked', async () => {
axiosMock
.onPost(getXBlockBaseApiUrl(id), {
publish: PUBLISH_TYPES.republish,
metadata: { visible_to_staff_only: false, group_access: { 970807507: [1959537066] } },
})
.reply(200, { dummy: 'value' });

const {
getByTitle, getByRole, getByTestId,
} = render(<RootWrapper />);

const accessGroupName1 = userPartitionInfoFormatted.selectablePartitions[0].groups[0].name;
const accessGroupName2 = userPartitionInfoFormatted.selectablePartitions[0].groups[1].name;

waitFor(() => {
const iframe = getByTitle(xblockContainerIframeMessages.xblockIframeTitle.defaultMessage);
expect(iframe).toBeInTheDocument();
simulatePostMessageEvent(messageTypes.manageXBlockAccess, {
id: courseVerticalChildrenMock.children[0].block_id,
});
});

waitFor(() => {
const configureModal = getByTestId('configure-modal');
expect(configureModal).toBeInTheDocument();

expect(within(configureModal).queryByText(accessGroupName1)).not.toBeInTheDocument();
expect(within(configureModal).queryByText(accessGroupName2)).not.toBeInTheDocument();

const restrictAccessSelect = getByRole('combobox', {
name: configureModalMessages.restrictAccessTo.defaultMessage,
});

userEvent.selectOptions(restrictAccessSelect, '0');

// eslint-disable-next-line array-callback-return
userPartitionInfoFormatted.selectablePartitions[0].groups.map((group) => {
expect(within(configureModal).getByRole('checkbox', { name: group.name })).not.toBeChecked();
expect(within(configureModal).queryByText(group.name)).toBeInTheDocument();
});

const group1Checkbox = within(configureModal).getByRole('checkbox', { name: accessGroupName1 });
userEvent.click(group1Checkbox);
expect(group1Checkbox).toBeChecked();

const saveModalBtnText = within(configureModal).getByRole('button', {
name: configureModalMessages.saveButton.defaultMessage,
});
expect(saveModalBtnText).toBeInTheDocument();

userEvent.click(saveModalBtnText);
expect(handleConfigureSubmitMock).toHaveBeenCalledTimes(1);
});
});
});
});
2 changes: 1 addition & 1 deletion src/course-unit/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const messageTypes = {
manageXBlockAccess: 'manageXBlockAccess',
deleteXBlock: 'deleteXBlock',
duplicateXBlock: 'duplicateXBlock',
refreshPositions: 'refreshPositions',
refreshXBlockPositions: 'refreshPositions',
newXBlockEditor: 'newXBlockEditor',
currentXBlockId: 'currentXBlockId',
toggleCourseXBlockDropdown: 'toggleCourseXBlockDropdown',
Expand Down
2 changes: 1 addition & 1 deletion src/course-unit/xblock-container-iframe/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ const XBlockContainerIframe: FC<XBlockContainerIframeProps> = ({
[messageTypes.manageXBlockAccess]: () => openConfigureModal(),
[messageTypes.copyXBlock]: () => dispatch(copyToClipboard(currentXBlockId)),
[messageTypes.duplicateXBlock]: () => handleDuplicateXBlock(currentXBlockData),
[messageTypes.refreshPositions]: handleRefetchXBlocks,
[messageTypes.refreshXBlockPositions]: handleRefetchXBlocks,
[messageTypes.newXBlockEditor]: ({ url }) => navigate(`/course/${courseId}/editor${url}`),
[messageTypes.currentXBlockId]: ({ id }) => setCurrentXBlockId(id),

Check warning on line 104 in src/course-unit/xblock-container-iframe/index.tsx

View check run for this annotation

Codecov / codecov/patch

src/course-unit/xblock-container-iframe/index.tsx#L103-L104

Added lines #L103 - L104 were not covered by tests
[messageTypes.toggleCourseXBlockDropdown]: ({
Expand Down

0 comments on commit 832ebf4

Please sign in to comment.