-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: upgrade react router to v6 (#319)
* feat: upgrade react router to v6 * refactor: removed remaining router v5 code * refactor: improve code coverage
- Loading branch information
1 parent
5e96dbf
commit 247e9f3
Showing
9 changed files
with
400 additions
and
225 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import React from 'react'; | ||
|
||
import { useLocation, useNavigate, useParams } from 'react-router-dom'; | ||
|
||
export const withParams = WrappedComponent => { | ||
const WithParamsComponent = props => <WrappedComponent {...useParams()} {...props} />; | ||
return WithParamsComponent; | ||
}; | ||
|
||
export const withNavigate = WrappedComponent => { | ||
const WithNavigateComponent = props => { | ||
const navigate = useNavigate(); | ||
return <WrappedComponent navigate={navigate} {...props} />; | ||
}; | ||
return WithNavigateComponent; | ||
}; | ||
|
||
export const withLocation = WrappedComponent => { | ||
const WithLocationComponent = props => { | ||
const location = useLocation(); | ||
return <WrappedComponent location={location} {...props} />; | ||
}; | ||
return WithLocationComponent; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import React from 'react'; | ||
import { mount } from 'enzyme'; | ||
|
||
import { withLocation, withNavigate } from './hoc'; | ||
|
||
const mockedNavigator = jest.fn(); | ||
|
||
jest.mock('react-router-dom', () => ({ | ||
useNavigate: () => mockedNavigator, | ||
useLocation: () => ({ | ||
pathname: '/current-location', | ||
}), | ||
})); | ||
|
||
// eslint-disable-next-line react/prop-types | ||
const MockComponent = ({ navigate, location }) => ( | ||
// eslint-disable-next-line react/button-has-type, react/prop-types | ||
<button id="btn" onClick={() => navigate('/some-route')}>{location.pathname}</button> | ||
); | ||
const WrappedComponent = withNavigate(withLocation(MockComponent)); | ||
|
||
test('Provide Navigation to Component', () => { | ||
const wrapper = mount( | ||
<WrappedComponent />, | ||
); | ||
const btn = wrapper.find('#btn'); | ||
btn.simulate('click'); | ||
|
||
expect(mockedNavigator).toHaveBeenCalledWith('/some-route'); | ||
}); | ||
|
||
test('Provide Location object to Component', () => { | ||
const wrapper = mount( | ||
<WrappedComponent />, | ||
); | ||
|
||
expect(wrapper.find('#btn').text()).toContain('/current-location'); | ||
}); |