This repository has been archived by the owner on May 28, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
140 additions
and
2 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,14 @@ | ||
import * as types from './actionTypes' | ||
import userApi from '../api/userApi' | ||
|
||
export function loginUserSuccess(user) { | ||
return { type: types.LOGIN_USER_SUCCESS, user } | ||
} | ||
|
||
export function loginUser(user) { | ||
return (dispatch) => { | ||
return userApi.loginUser(user).then((token) => { | ||
dispatch(loginUserSuccess({username: user.email, token, isAuthenticated: true})) | ||
}) | ||
} | ||
} |
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,66 @@ | ||
import expect from 'expect' | ||
import * as userActions from './userActions' | ||
import * as types from './actionTypes' | ||
|
||
import thunk from 'redux-thunk' | ||
import configureMockStore from 'redux-mock-store' | ||
import moxios from 'moxios' | ||
|
||
describe('User Actions', function () { | ||
describe('loginUserSuccess', () => { | ||
it('should create a LOGIN_USER_SUCCESS action', () => { | ||
const user = { someUser: 'test' } | ||
const expectedAction = { | ||
type: types.LOGIN_USER_SUCCESS, | ||
user | ||
} | ||
|
||
const action = userActions.loginUserSuccess(user) | ||
|
||
expect(action).toEqual(expectedAction) | ||
}) | ||
}) | ||
|
||
const middleware = [thunk] | ||
const mockStore = configureMockStore(middleware) | ||
|
||
describe('Async Actions', () => { | ||
beforeEach(function () { | ||
moxios.install() | ||
}) | ||
|
||
afterEach(function () { | ||
moxios.uninstall() | ||
}) | ||
|
||
describe('loginUser', () => { | ||
it('should create an LOGIN_USER_SUCCESS action', () => { | ||
const expectedToken = 'expectedValidToken' | ||
|
||
moxios.wait(() => { | ||
let request = moxios.requests.mostRecent() | ||
request.respondWith({ | ||
status: 200, | ||
response: { token: expectedToken } | ||
}) | ||
}) | ||
|
||
const expectedUser = { | ||
username: 'test', | ||
token: expectedToken, | ||
isAuthenticated: true | ||
} | ||
const expecedValidPassword = 'test' | ||
const expectedActions = [ | ||
{ type: types.LOGIN_USER_SUCCESS, user: expectedUser } | ||
] | ||
const store = mockStore({}) | ||
return store.dispatch(userActions.loginUser({ email: expectedUser.username, password: expecedValidPassword })).then(() => { | ||
const actions = store.getActions() | ||
expect(actions[0].type).toEqual(types.LOGIN_USER_SUCCESS) | ||
expect(actions[0].user).toEqual(expectedUser) | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) |
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,18 @@ | ||
import axios from 'axios' | ||
// eslint-disable-next-line no-undef | ||
axios.defaults.baseURL = API_URI | ||
|
||
class userApi { | ||
static loginUser(user) { | ||
return axios.post('/auth', user) | ||
.then((response) => { | ||
return response.data.token | ||
}) | ||
.catch((error) => { | ||
const errorMessage = error.response.data.message ? error.response.data.message : error | ||
throw new Error(errorMessage) | ||
}) | ||
} | ||
} | ||
|
||
export default userApi |
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,11 @@ | ||
import * as types from '../actions/actionTypes' | ||
import initialState from './initialState' | ||
|
||
export default function userReducer(state = initialState.user, action) { | ||
switch (action.type) { | ||
case types.LOGIN_USER_SUCCESS: | ||
return Object.assign({}, state.user, action.user ) | ||
default: | ||
return state | ||
} | ||
} |