forked from openedx/frontend-app-account
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: remove history pacakge (openedx#853)
* refactor: remove history pacakge * chore: improve test coverage
- Loading branch information
1 parent
d129eca
commit c7b5bd5
Showing
8 changed files
with
86 additions
and
36 deletions.
There are no files selected for viewing
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,19 @@ | ||
import React from 'react'; | ||
|
||
import { useLocation, useNavigate } from 'react-router-dom'; | ||
|
||
export const withNavigate = Component => { | ||
const WrappedComponent = props => { | ||
const navigate = useNavigate(); | ||
return <Component {...props} navigate={navigate} />; | ||
}; | ||
return WrappedComponent; | ||
}; | ||
|
||
export const withLocation = Component => { | ||
const WrappedComponent = props => { | ||
const location = useLocation(); | ||
return <Component {...props} location={location.pathname} />; | ||
}; | ||
return WrappedComponent; | ||
}; |
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}</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 Pathname to Component', () => { | ||
const wrapper = mount( | ||
<WrappedComponent />, | ||
); | ||
|
||
expect(wrapper.find('#btn').text()).toContain('/current-location'); | ||
}); |
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