Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [FC-0044] Course unit page - Unit creation button logic and refactoring #831

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/CourseAuthoringRoutes.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ const CourseAuthoringRoutes = () => {
/>
{DECODED_ROUTES.COURSE_UNIT.map((path) => (
<Route
key={path}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking, just a question. wondering how it was working before. We probably had warnings, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, a warning about the need to add a key for the element that is being mapped

path={path}
element={<PageWrap><CourseUnit courseId={courseId} /></PageWrap>}
/>
Expand Down
21 changes: 13 additions & 8 deletions src/course-unit/CourseUnit.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ const CourseUnit = ({ courseId }) => {
isLoading,
sequenceId,
unitTitle,
isQueryPending,
savingStatus,
isTitleEditFormOpen,
isEditTitleFormOpen,
isErrorAlert,
isInternetConnectionAlertFailed,
handleTitleEditSubmit,
headerNavigationsActions,
Expand All @@ -52,15 +54,15 @@ const CourseUnit = ({ courseId }) => {
<>
<Container size="xl" className="course-unit px-4">
<section className="course-unit-container mb-4 mt-5">
<ErrorAlert hideHeading isError={savingStatus === RequestStatus.FAILED}>
<ErrorAlert hideHeading isError={savingStatus === RequestStatus.FAILED && isErrorAlert}>
{intl.formatMessage(messages.alertFailedGeneric, { actionName: 'save', type: 'changes' })}
</ErrorAlert>
<SubHeader
hideBorder
title={(
<HeaderTitle
unitTitle={unitTitle}
isTitleEditFormOpen={isTitleEditFormOpen}
isEditTitleFormOpen={isEditTitleFormOpen}
arbrandes marked this conversation as resolved.
Show resolved Hide resolved
handleTitleEdit={handleTitleEdit}
handleTitleEditSubmit={handleTitleEditSubmit}
/>
Expand All @@ -78,6 +80,7 @@ const CourseUnit = ({ courseId }) => {
courseId={courseId}
sequenceId={sequenceId}
unitId={blockId}
handleCreateNewCourseXblock={handleCreateNewCourseXblock}
/>
<Layout
lg={[{ span: 9 }, { span: 3 }]}
Expand All @@ -101,11 +104,13 @@ const CourseUnit = ({ courseId }) => {
isShow={isShowProcessingNotification}
title={processingNotificationTitle}
/>
<InternetConnectionAlert
isFailed={isInternetConnectionAlertFailed}
isQueryPending={savingStatus === RequestStatus.PENDING}
onInternetConnectionFailed={handleInternetConnectionFailed}
/>
{isQueryPending && (
<InternetConnectionAlert
isFailed={isInternetConnectionAlertFailed}
isQueryPending={savingStatus === RequestStatus.PENDING}
onInternetConnectionFailed={handleInternetConnectionFailed}
/>
)}
</div>
</>
);
Expand Down
90 changes: 90 additions & 0 deletions src/course-unit/CourseUnit.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { IntlProvider } from '@edx/frontend-platform/i18n';
import { AppProvider } from '@edx/frontend-platform/react';
import { initializeMockApp } from '@edx/frontend-platform';
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth';
import { cloneDeep, set } from 'lodash';

import {
getCourseSectionVerticalApiUrl,
Expand All @@ -23,11 +24,13 @@ import {
courseCreateXblockMock,
courseSectionVerticalMock,
courseUnitIndexMock,
courseUnitMock,
} from './__mocks__';
import { executeThunk } from '../utils';
import CourseUnit from './CourseUnit';
import headerNavigationsMessages from './header-navigations/messages';
import headerTitleMessages from './header-title/messages';
import courseSequenceMessages from './course-sequence/messages';
import messages from './add-component/messages';

let axiosMock;
Expand Down Expand Up @@ -192,6 +195,93 @@ describe('<CourseUnit />', () => {
});
});

it('correct addition of a new course unit after click on the "Add new unit" button', async () => {
const { getByRole, getAllByTestId } = render(<RootWrapper />);
let units = null;
const updatedCourseSectionVerticalData = cloneDeep(courseSectionVerticalMock);
const updatedAncestorsChild = updatedCourseSectionVerticalData.xblock_info.ancestor_info.ancestors[0];
set(updatedCourseSectionVerticalData, 'xblock_info.ancestor_info.ancestors[0].child_info.children', [
...updatedAncestorsChild.child_info.children,
courseUnitMock,
]);

await waitFor(async () => {
units = getAllByTestId('course-unit-btn');
const courseUnits = courseSectionVerticalMock.xblock_info.ancestor_info.ancestors[0].child_info.children;
expect(units.length).toEqual(courseUnits.length);
});

axiosMock
.onPost(postXBlockBaseApiUrl(), { parent_locator: blockId, category: 'vertical', display_name: 'Unit' })
.reply(200, { dummy: 'value' });
axiosMock.reset();
axiosMock
.onGet(getCourseSectionVerticalApiUrl(blockId))
.reply(200, {
...updatedCourseSectionVerticalData,
});

await executeThunk(fetchCourseSectionVerticalData(blockId), store.dispatch);

const addNewUnitBtn = getByRole('button', { name: courseSequenceMessages.newUnitBtnText.defaultMessage });
units = getAllByTestId('course-unit-btn');
const updatedCourseUnits = updatedCourseSectionVerticalData
.xblock_info.ancestor_info.ancestors[0].child_info.children;

userEvent.click(addNewUnitBtn);
expect(units.length).toEqual(updatedCourseUnits.length);
expect(mockedUsedNavigate).toHaveBeenCalled();
expect(mockedUsedNavigate)
.toHaveBeenCalledWith(`/course/${courseId}/container/${blockId}/${updatedAncestorsChild.id}`, { replace: true });
});

it('the sequence unit is updated after changing the unit header', async () => {
const { getAllByTestId, getByRole } = render(<RootWrapper />);
const updatedCourseSectionVerticalData = cloneDeep(courseSectionVerticalMock);
const updatedAncestorsChild = updatedCourseSectionVerticalData.xblock_info.ancestor_info.ancestors[0];
set(updatedCourseSectionVerticalData, 'xblock_info.ancestor_info.ancestors[0].child_info.children', [
...updatedAncestorsChild.child_info.children,
courseUnitMock,
]);

const newDisplayName = `${unitDisplayName} new`;

axiosMock
.onPost(getXBlockBaseApiUrl(blockId, {
metadata: {
display_name: newDisplayName,
},
}))
.reply(200, { dummy: 'value' })
.onGet(getCourseUnitApiUrl(blockId))
.reply(200, {
...courseUnitIndexMock,
metadata: {
...courseUnitIndexMock.metadata,
display_name: newDisplayName,
},
})
.onGet(getCourseSectionVerticalApiUrl(blockId))
.reply(200, {
...updatedCourseSectionVerticalData,
});

await executeThunk(fetchCourseSectionVerticalData(blockId), store.dispatch);

const editTitleButton = getByRole('button', { name: headerTitleMessages.altButtonEdit.defaultMessage });
fireEvent.click(editTitleButton);

const titleEditField = getByRole('textbox', { name: headerTitleMessages.ariaLabelButtonEdit.defaultMessage });
fireEvent.change(titleEditField, { target: { value: newDisplayName } });

await act(async () => fireEvent.blur(titleEditField));

await waitFor(async () => {
const units = getAllByTestId('course-unit-btn');
expect(units.some(unit => unit.title === newDisplayName)).toBe(true);
});
});

it('handles creating Video xblock and navigates to editor page', async () => {
const { courseKey, locator } = courseCreateXblockMock;
axiosMock
Expand Down
9 changes: 9 additions & 0 deletions src/course-unit/__mocks__/courseSectionVertical.js
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,7 @@ module.exports = {
selected_groups_label: '',
},
enable_copy_paste_units: false,
xblock_type: 'other',
},
{
id: 'block-v1:edX+DemoX+Demo_Course+type@vertical+block@4f6c1b4e316a419ab5b6bf30e6c708e9',
Expand Down Expand Up @@ -581,6 +582,7 @@ module.exports = {
selected_groups_label: '',
},
enable_copy_paste_units: false,
xblock_type: 'video',
},
{
id: 'block-v1:edX+DemoX+Demo_Course+type@vertical+block@3dc16db8d14842e38324e95d4030b8a0',
Expand Down Expand Up @@ -664,6 +666,7 @@ module.exports = {
selected_groups_label: '',
},
enable_copy_paste_units: false,
xblock_type: 'other',
},
{
id: 'block-v1:edX+DemoX+Demo_Course+type@vertical+block@4a1bba2a403f40bca5ec245e945b0d76',
Expand Down Expand Up @@ -747,6 +750,7 @@ module.exports = {
selected_groups_label: '',
},
enable_copy_paste_units: false,
xblock_type: 'video',
},
{
id: 'block-v1:edX+DemoX+Demo_Course+type@vertical+block@256f17a44983429fb1a60802203ee4e0',
Expand Down Expand Up @@ -830,6 +834,7 @@ module.exports = {
selected_groups_label: '',
},
enable_copy_paste_units: false,
xblock_type: 'other',
},
{
id: 'block-v1:edX+DemoX+Demo_Course+type@vertical+block@e3601c0abee6427d8c17e6d6f8fdddd1',
Expand Down Expand Up @@ -913,6 +918,7 @@ module.exports = {
selected_groups_label: '',
},
enable_copy_paste_units: false,
xblock_type: 'video',
},
{
id: 'block-v1:edX+DemoX+Demo_Course+type@vertical+block@a79d59cd72034188a71d388f4954a606',
Expand Down Expand Up @@ -996,6 +1002,7 @@ module.exports = {
selected_groups_label: '',
},
enable_copy_paste_units: false,
xblock_type: 'other',
},
{
id: 'block-v1:edX+DemoX+Demo_Course+type@vertical+block@134df56c516a4a0dbb24dd5facef746e',
Expand Down Expand Up @@ -1079,6 +1086,7 @@ module.exports = {
selected_groups_label: '',
},
enable_copy_paste_units: false,
xblock_type: 'other',
},
{
id: 'block-v1:edX+DemoX+Demo_Course+type@vertical+block@d91b9e5d8bc64d57a1332d06bf2f2193',
Expand Down Expand Up @@ -1162,6 +1170,7 @@ module.exports = {
selected_groups_label: '',
},
enable_copy_paste_units: false,
xblock_type: 'video',
},
],
},
Expand Down
84 changes: 84 additions & 0 deletions src/course-unit/__mocks__/courseUnit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
module.exports = {
id: 'block-v1:edX+DemoX+Demo_Course+type@vertical+block@d91b9e5d8bc64d57a1332d06bf2f2144',
display_name: 'Getting Started new',
category: 'vertical',
has_children: true,
edited_on: 'Dec 28, 2023 at 10:00 UTC',
published: true,
published_on: 'Dec 28, 2023 at 10:00 UTC',
studio_url: '/container/block-v1:edX+DemoX+Demo_Course+type@vertical+block@d91b9e5d8bc64d57a1332d06bf2f2193',
released_to_students: true,
release_date: 'Feb 05, 2013 at 05:00 UTC',
visibility_state: 'live',
has_explicit_staff_lock: false,
start: '2013-02-05T05:00:00Z',
graded: false,
due_date: '',
due: null,
relative_weeks_due: null,
format: null,
course_graders: [
'Homework',
'Exam',
],
has_changes: false,
actions: {
deletable: true,
draggable: true,
childAddable: true,
duplicable: true,
},
explanatory_message: null,
group_access: {},
user_partitions: [
{
id: 50,
name: 'Enrollment Track Groups',
scheme: 'enrollment_track',
groups: [
{
id: 2,
name: 'Verified Certificate',
selected: false,
deleted: false,
},
{
id: 1,
name: 'Audit',
selected: false,
deleted: false,
},
],
},
],
show_correctness: 'always',
discussion_enabled: true,
ancestor_has_staff_lock: false,
user_partition_info: {
selectable_partitions: [
{
id: 50,
name: 'Enrollment Track Groups',
scheme: 'enrollment_track',
groups: [
{
id: 2,
name: 'Verified Certificate',
selected: false,
deleted: false,
},
{
id: 1,
name: 'Audit',
selected: false,
deleted: false,
},
],
},
],
selected_partition_index: -1,
selected_groups_label: '',
},
enable_copy_paste_units: false,
xblock_type: 'other',
};
1 change: 1 addition & 0 deletions src/course-unit/__mocks__/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { default as courseUnitIndexMock } from './courseUnitIndex';
export { default as courseSectionVerticalMock } from './courseSectionVertical';
export { default as courseUnitMock } from './courseUnit';
export { default as courseCreateXblockMock } from './courseCreateXblock';
3 changes: 3 additions & 0 deletions src/course-unit/course-sequence/Sequence.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const Sequence = ({
courseId,
sequenceId,
unitId,
handleCreateNewCourseXblock,
}) => {
const intl = useIntl();
const { IN_PROGRESS, FAILED, SUCCESSFUL } = RequestStatus;
Expand All @@ -26,6 +27,7 @@ const Sequence = ({
sequenceId={sequenceId}
unitId={unitId}
courseId={courseId}
handleCreateNewCourseXblock={handleCreateNewCourseXblock}
/>
</div>
</div>
Expand Down Expand Up @@ -58,6 +60,7 @@ Sequence.propTypes = {
unitId: PropTypes.string,
courseId: PropTypes.string.isRequired,
sequenceId: PropTypes.string,
handleCreateNewCourseXblock: PropTypes.func.isRequired,
};

Sequence.defaultProps = {
Expand Down
Loading
Loading