Skip to content

Commit

Permalink
Support redirect to previous location after login, test
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwr18 authored and joaopapereira committed Feb 4, 2019
1 parent 22fa1a1 commit 81bb42d
Show file tree
Hide file tree
Showing 15 changed files with 91 additions and 25 deletions.
5 changes: 5 additions & 0 deletions src/actions/setLastLocationAction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { SET_LAST_LOCATION } from '../types'

export let setLastLocation = props => dispatch => {
dispatch({ type: SET_LAST_LOCATION, payload: props.location.pathname })
}
4 changes: 2 additions & 2 deletions src/containers/LogIn.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class LogIn extends Component {
await this.props
.postLogInInfo({ email, password })
.then(() => {
this.props.history.push('/')
this.props.history.push(this.props.lastLocation)
iziToast.show({
theme: 'light',
title: 'Success',
Expand Down Expand Up @@ -92,7 +92,7 @@ export class LogIn extends Component {
}
}

const mapStateToProps = store => ({ loggedInUser: store.loggedInUser })
const mapStateToProps = store => ({ loggedInUser: store.loggedInUser, lastLocation: store.lastLocation })
export default connect(
mapStateToProps,
{ postLogInInfo }
Expand Down
6 changes: 4 additions & 2 deletions src/containers/ProjectsList.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { Component, Fragment } from 'react'
import { Header, Card, Grid } from 'semantic-ui-react'
import { connect } from 'react-redux'
import { fetchProjects } from '../actions/getProjectsAction'
import { setLastLocation } from '../actions/setLastLocationAction'
import { Link } from 'react-router-dom'
import Select from 'react-select'
import Project from '../components/Project'
Expand Down Expand Up @@ -37,7 +38,8 @@ export class ProjectsList extends Component {
} else {
this.paginateProjects(this.props.projects)
}
};
this.props.setLastLocation(this.props)
}

componentWillReceiveProps (nextProps) {
if (
Expand Down Expand Up @@ -223,5 +225,5 @@ export class ProjectsList extends Component {
const mapStateToProps = store => ({ projects: store.projects, error: store.error })
export default connect(
mapStateToProps,
{ fetchProjects }
{ fetchProjects, setLastLocation }
)(ProjectsList)
4 changes: 3 additions & 1 deletion src/containers/UsersList.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Paginate from '../components/Paginate'
import PaginationLinks from '../components/PaginationLinks'
import { connect } from 'react-redux'
import { fetchUsers } from '../actions/getUsersAction'
import { setLastLocation } from '../actions/setLastLocationAction'
import User from '../components/User'
import '../assets/UsersList.css'
export class UsersList extends Component {
Expand All @@ -25,6 +26,7 @@ export class UsersList extends Component {
} else {
this.normalizeUsers(this.props.users)
}
this.props.setLastLocation(this.props)
}

componentWillReceiveProps (nextProps) {
Expand Down Expand Up @@ -105,5 +107,5 @@ export class UsersList extends Component {
const mapStateToProps = store => ({ users: store.users })
export default connect(
mapStateToProps,
{ fetchUsers }
{ fetchUsers, setLastLocation }
)(UsersList)
9 changes: 7 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@ render(
<Navbar />
<Switch>
<Route path='/' exact component={Homepage} />
<Container className='main-content'>
<Container>
<Route path='/users' component={UsersList} />
<Route path='/login' component={LogIn} />
<Route
path='/login'
render={props => {
return <LogIn {...props} lastLocation={props} />
}}
/>
<Route path='/signup' component={SignUp} />
<Route path='/projects' component={ProjectsList} />
</Container>
Expand Down
3 changes: 2 additions & 1 deletion src/reducers/initialState.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ export default {
loggedInUser: {},
signedUpUser: {},
projects: [],
error: []
error: [],
lastLocation: ''
}
13 changes: 13 additions & 0 deletions src/reducers/lastLocationReducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { SET_LAST_LOCATION } from '../types'
import initialState from './initialState'

const lastLocationReducer = (state = initialState.lastLocation, action) => {
switch (action.type) {
case SET_LAST_LOCATION:
return action.payload
default:
return state
}
}

export default lastLocationReducer
4 changes: 3 additions & 1 deletion src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import projects from '../reducers/projectsReducer'
import loggedInUser from '../reducers/loggedInUserReducer'
import signedUpUser from '../reducers/signedUpUserReducer'
import error from '../reducers/errorReducer'
import lastLocation from '../reducers/lastLocationReducer'

const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose
const rootReducer = combineReducers({
users,
projects,
loggedInUser,
signedUpUser,
error
error,
lastLocation
})

export default createStore(
Expand Down
20 changes: 20 additions & 0 deletions src/tests/actions/setLastLocationAction.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import thunk from 'redux-thunk'
import configureMockStore from 'redux-mock-store'
import { SET_LAST_LOCATION } from '../../types'
import { setLastLocation } from '../../actions/setLastLocationAction'

const middlewares = [thunk]
const mockStore = configureMockStore(middlewares)
let store

describe('setLastLocation action', () => {
beforeEach(() => {
store = mockStore({})
})

it('sets last location to /users', () => {
let props = { location: { pathname: '/users' } }
store.dispatch(setLastLocation(props))
expect(store.getActions()).toEqual([{ type: SET_LAST_LOCATION, payload: '/users' }])
})
})
12 changes: 7 additions & 5 deletions src/tests/containers/ProjectsList.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ describe('ProjectsList', () => {
}, 300)
}),
filteredProjectsList: null,
error: false
error: false,
setLastLocation: () => {}
}
wrapper = mount(
<StaticRouter context={context}>
Expand Down Expand Up @@ -51,6 +52,7 @@ describe('ProjectsList', () => {
<ProjectsList
projects={paginatedProjectsFixture}
fetchProjects={() => {}}
setLastLocation={() => {}}
/>
)
wrapper.setState(
Expand Down Expand Up @@ -89,15 +91,15 @@ describe('ProjectsList', () => {
it("shouldn't render a Project component without projects", () => {
const wrapper = mount(
<StaticRouter context={context}>
<ProjectsList projects={{ 1: [] }} fetchProjects={() => {}} />
<ProjectsList projects={{ 1: [] }} fetchProjects={() => {}} setLastLocation={() => {}} />
</StaticRouter>
)
expect(wrapper.find('Project')).toHaveLength(0)
})

it('should test componentWillReceiveProps', () => {
const wrapper = shallow(
<ProjectsList projects={[]} fetchProjects={() => {}} />
<ProjectsList projects={[]} fetchProjects={() => {}} setLastLocation={() => {}} />
)
wrapper.setProps({ projects: [{ id: 1, languages: [] }] })
expect(wrapper.instance().state.projects).toEqual({
Expand All @@ -107,7 +109,7 @@ describe('ProjectsList', () => {

it('should call normalizeFilteredProjects', () => {
const wrapper = shallow(
<ProjectsList projects={[]} fetchProjects={() => {}} />
<ProjectsList projects={[]} fetchProjects={() => {}} setLastLocation={() => {}} />
)
wrapper.setProps({ projects: projectsFixture })
expect(wrapper.instance().state.projects).toEqual(
Expand All @@ -131,7 +133,7 @@ describe('ProjectsList', () => {

it('adds error to the state if fetchProjects fails', async () => {
const wrapper = shallow(
<ProjectsList projects={[]} error={[]} fetchProjects={() => {}} />
<ProjectsList projects={[]} error={[]} fetchProjects={() => {}} setLastLocation={() => {}} />
)
await wrapper.instance().componentDidMount()
expect(wrapper.state().error).toEqual(true)
Expand Down
7 changes: 4 additions & 3 deletions src/tests/containers/UserList.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ describe('UsersList', () => {
}, 300)
})
}
setLastLocation={() => {}}
/>
</StaticRouter>
)
Expand Down Expand Up @@ -66,20 +67,20 @@ describe('UsersList', () => {
it("shouldn't render a Project component without users", () => {
const wrapper = mount(
<StaticRouter context={context}>
<UsersList users={[]} fetchUsers={() => {}} />
<UsersList users={[]} fetchUsers={() => {}} setLastLocation={() => {}} />
</StaticRouter>
)
expect(wrapper.find('User')).toHaveLength(0)
})

it('should test componentWillReceiveProps', () => {
const wrapper = shallow(<UsersList users={[]} fetchUsers={() => {}} />)
const wrapper = shallow(<UsersList users={[]} fetchUsers={() => {}} setLastLocation={() => {}} />)
wrapper.setProps({ users: ['something'] })
expect(wrapper.instance().state.users).toEqual({ '1': ['something'] })
})

it('should test componentWillReceiveProps', () => {
const wrapper = shallow(<UsersList users={usersFixture} fetchUsers={() => {}} />)
const wrapper = shallow(<UsersList users={usersFixture} fetchUsers={() => {}} setLastLocation={() => {}} />)
wrapper.setProps(usersFixture)
expect(wrapper.instance().state.users[1][0]).toEqual(usersFixture[0])
})
Expand Down
17 changes: 17 additions & 0 deletions src/tests/reducers/lastLocationReducer.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import lastLocationReducer from '../../reducers/lastLocationReducer'
import { SET_LAST_LOCATION } from '../../types'

describe('reduces a user', () => {
it('defaults to empty projects if none are passed in', () => {
expect(lastLocationReducer(undefined, {})).toEqual('')
})

it('reduces the signed in user', () => {
expect(
lastLocationReducer([], {
type: SET_LAST_LOCATION,
payload: { lastLocation: '/users' }
})
).toEqual({ lastLocation: '/users' })
})
})
8 changes: 1 addition & 7 deletions src/tests/store/error.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ describe('Store', () => {
message: 'Network Error'
})

expect(store.getState()).toEqual({
users: [],
projects: [],
loggedInUser: {},
signedUpUser: {},
error: ['Network Error']
})
expect(store.getState().error).toEqual(['Network Error'])
})
})
3 changes: 2 additions & 1 deletion src/tests/store/store.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ describe('Store', () => {
loggedInUser: {},
signedUpUser: {},
error: [],
projects: []
projects: [],
lastLocation: ''
})
})
})
1 change: 1 addition & 0 deletions src/types/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export const GET_PROJECTS = 'GET_PROJECTS'
export const POST_LOGIN_INFO = 'POST_LOGIN_INFO'
export const POST_SIGNUP_INFO = 'POST_SIGNUP_INFO'
export const FETCH_PROJECTS_FAILURE = 'FETCH_PROJECTS_FAILURE'
export const SET_LAST_LOCATION = 'SET_LAST_LOCATION'

0 comments on commit 81bb42d

Please sign in to comment.