Skip to content

Commit

Permalink
refactor: removed remaining router v5 code
Browse files Browse the repository at this point in the history
  • Loading branch information
Syed-Ali-Abbas-Zaidi committed Aug 20, 2023
1 parent a8ca6bd commit abf4bfc
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 16 deletions.
1 change: 0 additions & 1 deletion src/App.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import Head from './head/Head';
jest.mock('react-router-dom', () => ({
BrowserRouter: () => 'BrowserRouter',
Route: () => 'Route',
Switch: () => 'Switch',
}));
jest.mock('@edx/frontend-platform/react', () => ({
AppProvider: () => 'AppProvider',
Expand Down
15 changes: 7 additions & 8 deletions src/containers/GradebookPage/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import GradesView from 'components/GradesView';
import GradebookFilters from 'components/GradebookFilters';
import BulkManagementHistoryView from 'components/BulkManagementHistoryView';

import withParams from '../../utils/hoc';
import { withParams, withNavigate, withLocation } from '../../utils/hoc';

/**
* <GradebookPage />
Expand All @@ -34,6 +34,7 @@ export class GradebookPage extends React.Component {
}

updateQueryParams(queryParams) {
const { pathname } = this.props.location;
const parsed = queryString.parse(this.props.location.search);
Object.keys(queryParams).forEach((key) => {
if (queryParams[key]) {
Expand All @@ -42,7 +43,7 @@ export class GradebookPage extends React.Component {
delete parsed[key];
}
});
this.props.history.push(`?${queryString.stringify(parsed)}`);
this.props.navigate({ pathname, search: `?${queryString.stringify(parsed)}` });
}

render() {
Expand All @@ -62,13 +63,11 @@ export class GradebookPage extends React.Component {
}
}
GradebookPage.defaultProps = {
location: { search: '' },
location: { pathname: '/', search: '' },
};
GradebookPage.propTypes = {
history: PropTypes.shape({
push: PropTypes.func,
}).isRequired,
location: PropTypes.shape({ search: PropTypes.string }),
navigate: PropTypes.func.isRequired,
location: PropTypes.shape({ pathname: PropTypes.string, search: PropTypes.string }),
courseId: PropTypes.string.isRequired,
// redux
activeView: PropTypes.string.isRequired,
Expand All @@ -83,4 +82,4 @@ export const mapDispatchToProps = {
initializeApp: thunkActions.app.initialize,
};

export default connect(mapStateToProps, mapDispatchToProps)(withParams(GradebookPage));
export default connect(mapStateToProps, mapDispatchToProps)(withParams(withNavigate(withLocation(GradebookPage))));
9 changes: 5 additions & 4 deletions src/containers/GradebookPage/test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,15 @@ describe('GradebookPage', () => {
let el;
const props = {
location: {
pathname: '/',
search: 'searchString',
},
courseId,
activeView: views.grades,
};
beforeEach(() => {
props.initializeApp = jest.fn();
props.history = { push: jest.fn() };
props.navigate = jest.fn();
});
test('snapshot - shows BulkManagementHistoryView if activeView === views.bulkManagementHistory', () => {
el = shallow(<GradebookPage {...props} activeView={views.bulkManagementHistory} />);
Expand Down Expand Up @@ -130,7 +131,7 @@ describe('GradebookPage', () => {
const val2 = 'VALTWO!!';
const args = { [newKey]: val1, [props.location.search]: val2 };
el.instance().updateQueryParams(args);
expect(props.history.push).toHaveBeenCalledWith(`?${queryString.stringify(args)}`);
expect(props.navigate).toHaveBeenCalledWith({ pathname: '/', search: `?${queryString.stringify(args)}` });
});
it('clears values for non-truthy values', () => {
queryString.parse.mockImplementation(key => ({ [key]: key }));
Expand All @@ -139,8 +140,8 @@ describe('GradebookPage', () => {
const val2 = false;
const args = { [newKey]: val1, [props.location.search]: val2 };
el.instance().updateQueryParams(args);
expect(props.history.push).toHaveBeenCalledWith(
`?${queryString.stringify({ [newKey]: val1 })}`,
expect(props.navigate).toHaveBeenCalledWith(
{ pathname: '/', search: `?${queryString.stringify({ [newKey]: val1 })}` },
);
});
});
Expand Down
20 changes: 17 additions & 3 deletions src/utils/hoc.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
import React from 'react';

import { useParams } from 'react-router-dom';
import { useLocation, useNavigate, useParams } from 'react-router-dom';

const withParams = WrappedComponent => {
export const withParams = WrappedComponent => {
const WithParamsComponent = props => <WrappedComponent {...useParams()} {...props} />;
return WithParamsComponent;
};

export default withParams;
export const withNavigate = WrappedComponent => {
const WithNavigateComponent = props => {
const navigate = useNavigate();
return <WrappedComponent navigate={navigate} {...props} />;

Check warning on line 13 in src/utils/hoc.jsx

View check run for this annotation

Codecov / codecov/patch

src/utils/hoc.jsx#L12-L13

Added lines #L12 - L13 were not covered by tests
};
return WithNavigateComponent;
};

export const withLocation = WrappedComponent => {
const WithLocationComponent = props => {
const location = useLocation();
return <WrappedComponent location={location} {...props} />;

Check warning on line 21 in src/utils/hoc.jsx

View check run for this annotation

Codecov / codecov/patch

src/utils/hoc.jsx#L20-L21

Added lines #L20 - L21 were not covered by tests
};
return WithLocationComponent;
};

0 comments on commit abf4bfc

Please sign in to comment.