Skip to content

Commit

Permalink
fix: fixed rerun link (#1023)
Browse files Browse the repository at this point in the history
* fix: fixed rerun link

* refactor: code refactoring

* refactor: updated tests

* refactor: after review
  • Loading branch information
PKulkoRaccoonGang authored May 21, 2024
1 parent 54003af commit 3647bcb
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 15 deletions.
8 changes: 0 additions & 8 deletions src/course-rerun/hooks.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { useEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { useIntl } from '@edx/frontend-platform/i18n';
import { useNavigate } from 'react-router-dom';

import { RequestStatus } from '../data/constants';
import { updateSavingStatus } from '../generic/data/slice';
import {
getSavingStatus,
getRedirectUrlObj,
getCourseRerunData,
getCourseData,
} from '../generic/data/selectors';
Expand All @@ -17,11 +15,9 @@ import { fetchStudioHomeData } from '../studio-home/data/thunks';
const useCourseRerun = (courseId) => {
const intl = useIntl();
const dispatch = useDispatch();
const navigate = useNavigate();
const savingStatus = useSelector(getSavingStatus);
const courseData = useSelector(getCourseData);
const courseRerunData = useSelector(getCourseRerunData);
const redirectUrlObj = useSelector(getRedirectUrlObj);

const {
displayName = '',
Expand All @@ -46,10 +42,6 @@ const useCourseRerun = (courseId) => {
useEffect(() => {
if (savingStatus === RequestStatus.SUCCESSFUL) {
dispatch(updateSavingStatus({ status: '' }));
const { url } = redirectUrlObj;
if (url) {
navigate('/home');
}
}
}, [savingStatus]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@ import messages from './messages';
import { CreateOrRerunCourseForm } from '.';
import { initialState } from './factories/mockApiResponses';

const mockedUsedNavigate = jest.fn();

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useParams: () => ({
courseId: 'course-id-mock',
}),
useNavigate: () => mockedUsedNavigate,
}));

let axiosMock;
Expand Down Expand Up @@ -134,8 +137,6 @@ describe('<CreateOrRerunCourseForm />', () => {
});

describe('handleOnClickCreate', () => {
delete window.location;
window.location = { assign: jest.fn() };
it('should call window.location.assign with url', async () => {
render(<RootWrapper {...props} />);
await mockStore();
Expand All @@ -156,7 +157,7 @@ describe('<CreateOrRerunCourseForm />', () => {
await axiosMock.onPost(getCreateOrRerunCourseUrl()).reply(200, { url });
await executeThunk(updateCreateOrRerunCourseQuery({ org: 'testX', run: 'some' }), store.dispatch);

expect(window.location.assign).toHaveBeenCalledWith(`${process.env.STUDIO_BASE_URL}${url}`);
expect(mockedUsedNavigate).toHaveBeenCalledWith(url);
});
it('should call window.location.assign with url and destinationCourseKey', async () => {
render(<RootWrapper {...props} />);
Expand All @@ -179,7 +180,7 @@ describe('<CreateOrRerunCourseForm />', () => {
});
await executeThunk(updateCreateOrRerunCourseQuery({ org: 'testX', run: 'some' }), store.dispatch);

expect(window.location.assign).toHaveBeenCalledWith(`${process.env.STUDIO_BASE_URL}${url}${destinationCourseKey}`);
expect(mockedUsedNavigate).toHaveBeenCalledWith(`${url}${destinationCourseKey}`);
});
});

Expand Down
7 changes: 4 additions & 3 deletions src/generic/create-or-rerun-course/hooks.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useEffect, useState } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { getConfig } from '@edx/frontend-platform';
import { useIntl } from '@edx/frontend-platform/i18n';
import { useFormik } from 'formik';
import * as Yup from 'yup';
import { useNavigate } from 'react-router-dom';

import { RequestStatus } from '../../data/constants';
import { getStudioHomeData } from '../../studio-home/data/selectors';
Expand All @@ -20,6 +20,7 @@ import messages from './messages';
const useCreateOrRerunCourse = (initialValues) => {
const intl = useIntl();
const dispatch = useDispatch();
const navigate = useNavigate();
const redirectUrlObj = useSelector(getRedirectUrlObj);
const createOrRerunCourseSavingStatus = useSelector(getSavingStatus);
const allOrganizations = useSelector(getOrganizations);
Expand Down Expand Up @@ -93,9 +94,9 @@ const useCreateOrRerunCourse = (initialValues) => {
// is in the destionationCourseKey attribute from the api.
if (url) {
if (destinationCourseKey) {
window.location.assign(`${getConfig().STUDIO_BASE_URL}${url}${destinationCourseKey}`);
navigate(`${url}${destinationCourseKey}`);
} else {
window.location.assign(`${getConfig().STUDIO_BASE_URL}${url}`);
navigate(url);
}
}
} else if (createOrRerunCourseSavingStatus === RequestStatus.FAILED) {
Expand Down

0 comments on commit 3647bcb

Please sign in to comment.