Skip to content

Commit

Permalink
specs for private route
Browse files Browse the repository at this point in the history
  • Loading branch information
caiokf committed Nov 18, 2019
1 parent 8747645 commit 4a6ddc0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
42 changes: 42 additions & 0 deletions spec/route.private.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import React from 'react'
import { shallow } from 'enzyme'
import { Redirect, Route } from 'react-router-dom'

import PrivateRoute from '../src/route.private'

describe('Private Route', () => {
let auth0MockResult

beforeEach(() => {
auth0MockResult = {
isLoggedIn: jest.fn(),
setNextPath: jest.fn(),
options: {
loginRoot: '/login',
},
}

window.ReactRouterAuth0Provider = auth0MockResult
})

it('should set a return path and redirect when user is not logged in', () => {
auth0MockResult.isLoggedIn = jest.fn().mockReturnValue(false)

const wrapper = shallow(<PrivateRoute path="/home" />)

expect(window.ReactRouterAuth0Provider.isLoggedIn).toHaveBeenCalled()
expect(window.ReactRouterAuth0Provider.setNextPath).toHaveBeenCalled()
expect(wrapper.find(Redirect)).toHaveLength(1)
})


it('should render a react router Route when user is logged in', () => {
auth0MockResult.isLoggedIn = jest.fn().mockReturnValue(true)

const wrapper = shallow(<PrivateRoute path="/home" />)

expect(window.ReactRouterAuth0Provider.isLoggedIn).toHaveBeenCalled()
expect(window.ReactRouterAuth0Provider.setNextPath).not.toHaveBeenCalled()
expect(wrapper.find(Route)).toHaveLength(1)
})
})
2 changes: 1 addition & 1 deletion src/route.private.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Route, Redirect } from 'react-router-dom'
export default class PrivateRoute extends Route {
render() {
if (!window.ReactRouterAuth0Provider.isLoggedIn()) {
window.ReactRouterAuth0Provider.setNextPath(this.props.location.pathname)
window.ReactRouterAuth0Provider.setNextPath(this.props.location && this.props.location.pathname)
return <Redirect to={{ pathname: window.ReactRouterAuth0Provider.options.loginRoute, state: { from: this.props.location } }} />
}

Expand Down

0 comments on commit 4a6ddc0

Please sign in to comment.