Skip to content

Commit

Permalink
fix: test cases affectewd due to lib-special-exams
Browse files Browse the repository at this point in the history
  • Loading branch information
Syed-Ali-Abbas-Zaidi committed Sep 13, 2023
1 parent 1b7a4e3 commit 86cb5a2
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 47 deletions.
4 changes: 4 additions & 0 deletions src/courseware/course/Course.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import { executeThunk } from '../../utils';
import * as thunks from '../data/thunks';

jest.mock('@edx/frontend-platform/analytics');
jest.mock('@edx/frontend-lib-special-exams/dist/data/thunks.js', () => ({
...jest.requireActual('@edx/frontend-lib-special-exams/dist/data/thunks.js'),
checkExamEntry: () => jest.fn(),
}));

const recordFirstSectionCelebration = jest.fn();
// eslint-disable-next-line no-import-assign
Expand Down
2 changes: 1 addition & 1 deletion src/courseware/course/CourseBreadcrumbs.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ describe('CourseBreadcrumbs', () => {
sequenceId="block-v1:edX+DemoX+Demo_Course+type@sequential+block@basic_questions"
isStaff
/>
</BrowserRouter>
</BrowserRouter>,
</IntlProvider>,
);
it('renders course breadcrumbs as expected', async () => {
Expand Down
11 changes: 3 additions & 8 deletions src/courseware/course/course-exit/CourseExit.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getConfig } from '@edx/frontend-platform';
import { injectIntl, intlShape } from '@edx/frontend-platform/i18n';
import { Button } from '@edx/paragon';
import { useSelector } from 'react-redux';
import { useNavigate } from 'react-router-dom';
import { Navigate } from 'react-router-dom';

import CourseCelebration from './CourseCelebration';
import CourseInProgress from './CourseInProgress';
Expand All @@ -16,7 +16,6 @@ import { unsubscribeFromGoalReminders } from './data/thunks';
import { useModel } from '../../../generic/model-store';

const CourseExit = ({ intl }) => {
const navigate = useNavigate();
const { courseId } = useSelector(state => state.courseware);
const {
certificateData,
Expand Down Expand Up @@ -58,14 +57,10 @@ const CourseExit = ({ intl }) => {
body = (<CourseInProgress />);
} else if (mode === COURSE_EXIT_MODES.celebration) {
body = (<CourseCelebration />);
} else {
return (<Navigate to={`/course/${courseId}`} replace />);
}

useEffect(() => {
if ((mode === COURSE_EXIT_MODES.disabled) || (!(mode in COURSE_EXIT_MODES))) {
navigate(`/course/${courseId}`);
}
}, []);

return (
<>
<div className="row w-100 mt-2 mb-4 justify-content-end">
Expand Down
4 changes: 4 additions & 0 deletions src/courseware/course/sequence/Sequence.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import Sequence from './Sequence';
import { fetchSequenceFailure } from '../../data/slice';

jest.mock('@edx/frontend-platform/analytics');
jest.mock('@edx/frontend-lib-special-exams/dist/data/thunks.js', () => ({
...jest.requireActual('@edx/frontend-lib-special-exams/dist/data/thunks.js'),
checkExamEntry: () => jest.fn(),
}));

describe('Sequence', () => {
let mockData;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React, { useCallback } from 'react';
import PropTypes from 'prop-types';
import { useNavigate } from 'react-router-dom';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faLock } from '@fortawesome/free-solid-svg-icons';
import { injectIntl, intlShape } from '@edx/frontend-platform/i18n';
import { Button } from '@edx/paragon';

import { useNavigate } from 'react-router-dom';
import messages from './messages';

const ContentLock = ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import {
} from '../../../../setupTest';
import ContentLock from './ContentLock';

const mockedNavigator = jest.fn();
const mockNavigate = jest.fn();

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useNavigate: () => mockedNavigator,
useNavigate: () => mockNavigate,
}));

describe('Content Lock', () => {
Expand Down Expand Up @@ -43,6 +43,6 @@ describe('Content Lock', () => {
render(<ContentLock {...mockData} />, { wrapWithRouter: true });
fireEvent.click(screen.getByRole('button'));

expect(mockedNavigator).toHaveBeenCalledWith(`/course/${mockData.courseId}/${mockData.prereqId}`);
expect(mockNavigate).toHaveBeenCalledWith(`/course/${mockData.courseId}/${mockData.prereqId}`);
});
});
6 changes: 3 additions & 3 deletions src/courseware/course/sequence/honor-code/HonorCode.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import {
} from '../../../../setupTest';
import HonorCode from './HonorCode';

const mockedNavigator = jest.fn();
const mockNavigate = jest.fn();

initializeMockApp();
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useNavigate: () => mockedNavigator,
useNavigate: () => mockNavigate,
}));

describe('Honor Code', () => {
Expand All @@ -41,7 +41,7 @@ describe('Honor Code', () => {
render(<HonorCode {...mockData} />, { wrapWithRouter: true });
const cancelButton = screen.getByText('Cancel');
fireEvent.click(cancelButton);
expect(mockedNavigator).toHaveBeenCalledWith(`/course/${mockData.courseId}/home`);
expect(mockNavigate).toHaveBeenCalledWith(`/course/${mockData.courseId}/home`);
});

it('calls to save integrity_signature when agreeing', async () => {
Expand Down
1 change: 1 addition & 0 deletions src/decode-page-route/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import React from 'react';
import {
generatePath, useMatch, Navigate,
} from 'react-router-dom';

import { DECODE_ROUTES } from '../constants';

const ROUTES = [].concat(
Expand Down
5 changes: 2 additions & 3 deletions src/generic/CourseAccessErrorPage.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useEffect } from 'react';
import { LearningHeader as Header } from '@edx/frontend-component-header';
import Footer from '@edx/frontend-component-footer';
import { useParams, useNavigate } from 'react-router-dom';
import { useParams, Navigate } from 'react-router-dom';
import { useDispatch, useSelector } from 'react-redux';
import { injectIntl, intlShape } from '@edx/frontend-platform/i18n';
import useActiveEnterpriseAlert from '../alerts/active-enteprise-alert';
Expand All @@ -13,7 +13,6 @@ import messages from '../tab-page/messages';

const CourseAccessErrorPage = ({ intl }) => {
const { courseId } = useParams();
const navigate = useNavigate();

const dispatch = useDispatch();
const activeEnterpriseAlert = useActiveEnterpriseAlert(courseId);
Expand All @@ -38,7 +37,7 @@ const CourseAccessErrorPage = ({ intl }) => {
);
}
if (courseStatus === LOADED) {
navigate(`/redirect/home/${courseId}`, { replace: true });
return <Navigate to={`/redirect/home/${courseId}`} replace />;
}
return (
<>
Expand Down
10 changes: 5 additions & 5 deletions src/generic/CourseAccessErrorPage.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { initializeTestStore, render, screen } from '../setupTest';
import CourseAccessErrorPage from './CourseAccessErrorPage';

const mockDispatch = jest.fn();
const mockedNavigator = jest.fn();
const mockNavigate = jest.fn();
let mockCourseStatus;

jest.mock('react-redux', () => ({
Expand All @@ -18,7 +18,7 @@ jest.mock('./PageLoading', () => function () {
});
jest.mock('react-router-dom', () => ({
...(jest.requireActual('react-router-dom')),
useNavigate: () => mockedNavigator,
useNavigate: () => mockNavigate,
}));

describe('CourseAccessErrorPage', () => {
Expand All @@ -40,7 +40,7 @@ describe('CourseAccessErrorPage', () => {
{ wrapWithRouter: true },
);
expect(screen.getByTestId('page-loading')).toBeInTheDocument();
expect(history.location.pathname).toBe(accessDeniedUrl);
expect(window.location.pathname).toBe(accessDeniedUrl);
});

it('Redirect user to homepage if user has access', () => {
Expand All @@ -51,7 +51,7 @@ describe('CourseAccessErrorPage', () => {
</Routes>,
{ wrapWithRouter: true },
);
expect(mockedNavigator).toHaveBeenCalledWith('/redirect/home/course-v1:edX+DemoX+Demo_Course', { replace: true });
expect(window.location.pathname).toBe('/redirect/home/course-v1:edX+DemoX+Demo_Course');
});

it('For access denied it should render access denied page', () => {
Expand All @@ -64,6 +64,6 @@ describe('CourseAccessErrorPage', () => {
{ wrapWithRouter: true },
);
expect(screen.getByTestId('access-denied-main')).toBeInTheDocument();
expect(history.location.pathname).toBe(accessDeniedUrl);
expect(window.location.pathname).toBe(accessDeniedUrl);
});
});
42 changes: 19 additions & 23 deletions src/generic/path-fixes/PathFixesProvider.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { useLocation, useNavigate } from 'react-router-dom';
import { Navigate, useLocation } from 'react-router-dom';
import PropTypes from 'prop-types';

import { sendTrackEvent } from '@edx/frontend-platform/analytics';
import { useEffect } from 'react';

/**
* We have seen evidence of learners hitting MFE pages with spaces instead of plus signs (which are used commonly
Expand All @@ -14,27 +13,24 @@ import { useEffect } from 'react';
*/
const PathFixesProvider = ({ children }) => {
const location = useLocation();
const navigate = useNavigate();

useEffect(() => {
// We only check for spaces. That's not the only kind of character that is escaped in URLs, but it would always be
// present for our cases, and I believe it's the only one we use normally.
if (location.pathname.includes(' ') || location.pathname.includes('%20')) {
const newLocation = {
...location,
pathname: (location.pathname.replaceAll(' ', '+')).replaceAll('%20', '+'),
};

sendTrackEvent('edx.ui.lms.path_fixed', {
new_path: newLocation.pathname,
old_path: location.pathname,
referrer: document.referrer,
search: location.search,
});

navigate(newLocation);
}
}, [location.pathname]);

// We only check for spaces. That's not the only kind of character that is escaped in URLs, but it would always be
// present for our cases, and I believe it's the only one we use normally.
if (location.pathname.includes(' ') || location.pathname.includes('%20')) {
const newLocation = {
...location,
pathname: (location.pathname.replaceAll(' ', '+')).replaceAll('%20', '+'),
};

sendTrackEvent('edx.ui.lms.path_fixed', {
new_path: newLocation.pathname,
old_path: location.pathname,
referrer: document.referrer,
search: location.search,
});

return (<Navigate to={newLocation} replace />);
}

return children; // pass through
};
Expand Down

0 comments on commit 86cb5a2

Please sign in to comment.