Skip to content

Commit

Permalink
Merge pull request #22 from Syed-Ali-Abbas-Zaidi/Ali-Abbas/react-rout…
Browse files Browse the repository at this point in the history
…er-upgrade

feat: upgrade react router to v6
  • Loading branch information
muselesscreator authored Sep 11, 2023
2 parents 808388e + bc61962 commit 868c4d0
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 88 deletions.
86 changes: 35 additions & 51 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"@edx/brand": "npm:@edx/brand-openedx@1.2.0",
"@edx/frontend-component-footer": "12.2.1",
"@edx/frontend-component-header": "4.6.0",
"@edx/frontend-platform": "4.6.3",
"@edx/frontend-platform": "5.1.0",
"@edx/paragon": "^20.20.0",
"@edx/react-unit-test-utils": "1.7.0",
"@edx/tinymce-language-selector": "1.1.0",
Expand All @@ -58,8 +58,8 @@
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-redux": "7.2.9",
"react-router": "5.3.4",
"react-router-dom": "5.3.4",
"react-router": "6.15.0",
"react-router-dom": "6.15.0",
"redux": "4.2.1",
"redux-devtools-extension": "^2.13.9",
"redux-logger": "^3.0.6",
Expand Down
28 changes: 8 additions & 20 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BrowserRouter as Router, Switch, Route } from 'react-router-dom';
import { Routes, Route } from 'react-router-dom';
import { ErrorPage } from '@edx/frontend-platform/react';
import { useIntl } from '@edx/frontend-platform/i18n';

Expand All @@ -13,25 +13,13 @@ const RouterRoot = () => {
const { formatMessage } = useIntl();

return (
<Router>
<Switch>
<Route path={routes.peerAssessment}>
<PeerAssessmentView />
</Route>
<Route path={routes.selfAssessment}>
<SelfAssessmentView />
</Route>
<Route path={routes.studentTraining}>
<StudentTrainingView />
</Route>
<Route path={routes.submission}>
<SubmissionView />
</Route>
<Route path={routes.root}>
<ErrorPage message={formatMessage(messages.error404Message)} />
</Route>
</Switch>
</Router>
<Routes>
<Route path={routes.peerAssessment} element={<PeerAssessmentView />} />
<Route path={routes.selfAssessment} element={<SelfAssessmentView />} />
<Route path={routes.studentTraining} element={<StudentTrainingView />} />
<Route path={routes.submission} element={<SubmissionView />} />
<Route path={routes.root} element={<ErrorPage message={formatMessage(messages.error404Message)} />} />
</Routes>
);
};

Expand Down
22 changes: 11 additions & 11 deletions src/data/services/lms/hooks/api.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useQuery } from '@tanstack/react-query';
import { useRouteMatch } from 'react-router-dom';
import { useMatch } from 'react-router-dom';
import { camelCaseObject } from '@edx/frontend-platform';
import { when } from 'jest-when';

Expand All @@ -12,7 +12,7 @@ import { useORAConfig, usePageData } from './api';

jest.mock('@tanstack/react-query', () => ({ useQuery: jest.fn() }));

jest.mock('react-router-dom', () => ({ useRouteMatch: jest.fn() }));
jest.mock('react-router-dom', () => ({ useMatch: jest.fn() }));

interface QueryFn { (): string }
interface QueryArgs { queryKey: string, queryFn: QueryFn }
Expand Down Expand Up @@ -52,8 +52,8 @@ describe('lms api hooks', () => {
});
it('initializes query with promise pointing to assessment text', async () => {
const old = window.location;
Object.defineProperty(window, "location", {
value: new URL(`http://dummy.com/text`),
Object.defineProperty(window, 'location', {
value: new URL('http://dummy.com/text'),
writable: true,
});
const response = await out.queryFn();
Expand Down Expand Up @@ -86,16 +86,16 @@ describe('lms api hooks', () => {
.mockImplementationOnce(mockUseQuery(data));
};

const mockUseRouteMatch = (path) => {
when(useRouteMatch)
.calledWith()
.mockReturnValueOnce({ path });
const mockUseMatch = (path) => {
when(useMatch)
.calledWith(path)
.mockReturnValueOnce({ pattern: { path } });
};

const testUsePageData = usePageData as unknown as MockPageDataUseConfigHook;
describe('submission', () => {
beforeEach(() => {
mockUseRouteMatch(routes.submission);
mockUseMatch(routes.submission);
mockUseQueryForPageData(fakeData.pageData.shapes.emptySubmission, false);
out = testUsePageData();
});
Expand All @@ -112,7 +112,7 @@ describe('lms api hooks', () => {
});
describe('assessment', () => {
beforeEach(() => {
mockUseRouteMatch(routes.peerAssessment);
mockUseMatch(routes.peerAssessment);
mockUseQueryForPageData(fakeData.pageData.shapes.peerAssessment, true);
out = testUsePageData();
});
Expand All @@ -128,7 +128,7 @@ describe('lms api hooks', () => {
});
});
it('returns empty object from data if data has not been returned', () => {
mockUseRouteMatch(routes.submission);
mockUseMatch(routes.submission);
mockUseQueryForPageData(undefined, false);
out = testUsePageData();
expect(out.data).toEqual({});
Expand Down
6 changes: 3 additions & 3 deletions src/data/services/lms/hooks/api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useQuery } from '@tanstack/react-query';

import { useRouteMatch } from 'react-router-dom';
import { useMatch } from 'react-router-dom';
import { camelCaseObject } from '@edx/frontend-platform';

import routes from 'routes';
Expand All @@ -24,8 +24,8 @@ export const useORAConfig = (): types.QueryData<types.ORAConfig> => {
};

export const usePageData = (): types.QueryData<types.PageData> => {
const route = useRouteMatch();
const isAssessment = route.path === routes.peerAssessment;
const route = useMatch(routes.peerAssessment);
const isAssessment = !!route && route.pattern.path === routes.peerAssessment;
const returnData = isAssessment
? fakeData.pageData.shapes.peerAssessment
: fakeData.pageData.shapes.emptySubmission;
Expand Down

0 comments on commit 868c4d0

Please sign in to comment.