This repository has been archived by the owner on Apr 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #509 from alepop/499-enhancments-in-routing
Enhancements in routing - Closes #499
- Loading branch information
Showing
7 changed files
with
131 additions
and
33 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
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,15 @@ | ||
import React from 'react'; | ||
import { Route, Redirect, withRouter } from 'react-router-dom'; | ||
import { connect } from 'react-redux'; | ||
|
||
export const PrivateRouteRender = ({ render, isAuthenticated, ...rest }) => ( | ||
<Route {...rest} render={ matchProps => ( | ||
isAuthenticated ? render(matchProps) : <Redirect to='/' /> | ||
)}/> | ||
); | ||
|
||
const mapStateToProps = state => ({ | ||
isAuthenticated: !!state.account.publicKey, | ||
}); | ||
|
||
export default withRouter(connect(mapStateToProps)(PrivateRouteRender)); |
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,33 @@ | ||
import React from 'react'; | ||
import { expect } from 'chai'; | ||
import { mount } from 'enzyme'; | ||
import { MemoryRouter, Route } from 'react-router'; | ||
import { PrivateRouteRender } from './index'; | ||
|
||
const Public = () => <h1>Public</h1>; | ||
const Private = () => <h1>Private</h1>; | ||
|
||
describe('PrivateRouteRender', () => { | ||
const isAuth = isAuthenticated => ( | ||
mount( | ||
<MemoryRouter initialEntries={['/private/test']}> | ||
<div> | ||
<Route path='/' component={Public} /> | ||
<PrivateRouteRender | ||
path='/private' | ||
render={({ match }) => <Route to={`${match.url}/test`} component={Private}/>} | ||
isAuthenticated={isAuthenticated} /> | ||
</div> | ||
</MemoryRouter>, | ||
) | ||
); | ||
it('should render Component if user is authenticated', () => { | ||
const wrapper = isAuth(true); | ||
expect(wrapper.find(Private)).to.have.length(1); | ||
}); | ||
|
||
it('should redirect to root path if user is not authenticated', () => { | ||
const wrapper = isAuth(false); | ||
expect(wrapper.find(Public)).to.have.length(1); | ||
}); | ||
}); |
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