Skip to content

Commit

Permalink
fix tests to use redux-mock-store see #112
Browse files Browse the repository at this point in the history
  • Loading branch information
bartonhammond committed Aug 10, 2016
1 parent 0f502f6 commit 513612f
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 75 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"jest": "14.0.0",
"jest-react-native": "14.0.0",
"react-addons-test-utils": "15.0.2",
"redux-mock-store": "1.0.3",
"redux-mock-store": "1.1.2",
"rnpm-plugin-upgrade": "0.26.0"
}
}
8 changes: 4 additions & 4 deletions src/__mocks__/react-native-router-flux.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = {
Actions: {
push: function() {},
reset: function() {},
pop: function() {}
Login: function() {},
Tabbar: function() {},
InitialLoginForm: function() {}
}
}
};
91 changes: 66 additions & 25 deletions src/reducers/auth/__tests__/authActions-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@
/**
* ## Mocks
*
* We don't want to use the devices storage, nor actually call Parse.com
* We don't want to use the devices storage, nor actually call
* Parse.com
*
* Need to mock router so the "keys" are available (see src/__mocks__)
*/
jest.mock('../../../lib/AppAuthToken');
jest.mock('../../../lib/BackendFactory');
jest.mock('react-native-router-flux');

/**
* ## Mock Store
Expand All @@ -21,8 +25,11 @@ jest.mock('../../../lib/BackendFactory');
* in the correct order
*
*/
var mockStore = require('../../mocks/Store');
import configureStore from 'redux-mock-store';
import thunk from 'redux-thunk';

const middlewares = [thunk]; // add your middlewares like `redux-thunk`
const mockStore = configureStore(middlewares);
/**
* ## Class under test
*
Expand All @@ -40,6 +47,7 @@ const {
SESSION_TOKEN_FAILURE,

DELETE_TOKEN_REQUEST,
DELETE_TOKEN_SUCCESS,

LOGOUT,
REGISTER,
Expand Down Expand Up @@ -187,47 +195,73 @@ describe('authActions', () => {
it('should logout', () => {
const expectedActions = [
{type: LOGOUT_REQUEST},
{type: REGISTER},
{type: LOGIN},
{type: LOGOUT_SUCCESS},
{type: SESSION_TOKEN_REQUEST},
{type: SESSION_TOKEN_SUCCESS}
{type: DELETE_TOKEN_REQUEST}
];

const store = mockStore({}, expectedActions);
return store.dispatch(actions.logout());
const store = mockStore({});
return store.dispatch(actions.logout())
.then(() => {
expect(store.getActions()).toEqual(expectedActions);
});
});

it('should login', () => {
const expectedActions = [
{type: LOGIN_REQUEST},
{type: LOGOUT},
{type: LOGIN_SUCCESS}
{type: LOGIN_SUCCESS, payload: {status: 201}},
{type: LOGOUT}
];

const store = mockStore({}, expectedActions);
return store.dispatch(actions.login('foo','bar'));
const store = mockStore({});
return store.dispatch(actions.login('foo','bar'))
.then(() => {
expect(store.getActions()[0].type).toEqual(expectedActions[0].type);
expect(store.getActions()[1].type).toEqual(expectedActions[1].type);
expect(store.getActions()[1].payload.status).toEqual(expectedActions[1].payload.status);
expect(store.getActions()[2].type).toEqual(expectedActions[2].type);
});
});

it('should getSessionToken', () => {
const expectedActions = [
{type: SESSION_TOKEN_REQUEST},
{type: LOGOUT},
{type: SESSION_TOKEN_SUCCESS}
{type: SESSION_TOKEN_SUCCESS, payload: {sessionToken:
{sessionToken: 'token'}}},
{type: LOGOUT}
];

const store = mockStore({}, expectedActions);
return store.dispatch(actions.getSessionToken());
const store = mockStore({});
return store.dispatch(actions.getSessionToken())
.then(() => {
expect(store.getActions()[0].type).toEqual(expectedActions[0].type);
expect(store.getActions()[1].type).toEqual(expectedActions[1].type);
expect(store.getActions()[1].payload.sessionToken.sessionToken).toEqual(expectedActions[1].payload.sessionToken.sessionToken);
expect(store.getActions()[2].type).toEqual(expectedActions[2].type);
});
});

it('should signup', () => {
const expectedActions = [
{type: SIGNUP_REQUEST},
{type: LOGOUT},
{type: SIGNUP_SUCCESS}
{type: SIGNUP_SUCCESS, payload: {status: 201,
username: 'user',
email: 'email'}},
{type: LOGOUT}
];

const store = mockStore({}, expectedActions);
return store.dispatch(actions.signup('user','email','password'));
const store = mockStore({});

return store.dispatch(actions.signup('user','email','password'))
.then(() => {
expect(store.getActions()[0]).toEqual(expectedActions[0]);
expect(store.getActions()[1].type).toEqual(expectedActions[1].type);
expect(store.getActions()[1].payload.status).toEqual(expectedActions[1].payload.status);
expect(store.getActions()[1].payload.username).toEqual(expectedActions[1].payload.username);
expect(store.getActions()[1].payload.email).toEqual(expectedActions[1].payload.email);
expect(store.getActions()[2]).toEqual(expectedActions[2]);
});
});

it('should resetPassword', () => {
Expand All @@ -237,19 +271,26 @@ describe('authActions', () => {
{type: RESET_PASSWORD_SUCCESS}
];

const store = mockStore({}, expectedActions);
return store.dispatch(actions.resetPassword('email'));
const store = mockStore({});

return store.dispatch(actions.resetPassword('email'))
.then(() => {
expect(store.getActions()).toEqual(expectedActions);
});
});

it('should deleteSessionToken', () => {
const expectedActions = [
{type: DELETE_TOKEN_REQUEST},
{type: SESSION_TOKEN_REQUEST},
{type: SESSION_TOKEN_SUCCESS}
{type: DELETE_TOKEN_SUCCESS}
];

const store = mockStore({}, expectedActions);
return store.dispatch(actions.deleteSessionToken());
const store = mockStore({});

return store.dispatch(actions.deleteSessionToken())
.then(() => {
expect(store.getActions()).toEqual(expectedActions);
});
});

});
3 changes: 0 additions & 3 deletions src/reducers/auth/authActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,18 +238,15 @@ export function getSessionToken() {
return new AppAuthToken().getSessionToken()

.then((token) => {
console.log('authactions.token',token);
if (token) {
dispatch(sessionTokenRequestSuccess(token));
dispatch(logoutState());
console.log('authactions.Actions',Actions);
} else {
dispatch(sessionTokenRequestFailure());
}
})

.catch((error) => {
console.log('authactions.error',error);
dispatch(sessionTokenRequestFailure(error));
dispatch(loginState());
Actions.InitialLoginForm();
Expand Down
13 changes: 3 additions & 10 deletions src/reducers/device/__tests__/deviceActions-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,13 @@
/**
* ## Mocks
*
* We don't want to use the devices storage, nor actually call Parse.com
* We don't want to use the devices storage, nor actually call
* Parse.com
*
*/
jest.mock('../../../lib/AppAuthToken');
jest.mock('../../../lib/BackendFactory');

/**
* ## Mock Store
*
* The ```mockStore``` confirms the all the actions are dispatched and
* in the correct order
*
*/
var mockStore = require('../../mocks/Store').default;

/**
* ## Class under test
*
Expand Down
25 changes: 0 additions & 25 deletions src/reducers/mocks/Store.js

This file was deleted.

30 changes: 23 additions & 7 deletions src/reducers/profile/__tests__/profileActions-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,25 @@
* ## Mocks
*
* turn mocking off but mock AppAuthToken and Parse
* also mock the router (see src/__mocks__)
*
*/
jest.mock('../../../lib/AppAuthToken');
jest.mock('../../../lib/BackendFactory');
jest.mock('react-native-router-flux');
/**
* ## Store
* The mockStore will validate the actions are performed
*/
const mockStore = require('../../mocks/Store');
import configureStore from 'redux-mock-store';
import thunk from 'redux-thunk';

const middlewares = [thunk]; // add your middlewares like `redux-thunk`
const mockStore = configureStore(middlewares);
/**
* ## Class under test
*
*/
const actions = require('../profileActions');

/**
Expand Down Expand Up @@ -104,20 +114,26 @@ describe('profileActions', () => {
{type: GET_PROFILE_SUCCESS}
];

const store = mockStore({}, expectedActions);
return store.dispatch(actions.getProfile());
const store = mockStore({});
return store.dispatch(actions.getProfile())
.then(() => {
expect(store.getActions()[0].type).toEqual(expectedActions[0].type);
expect(store.getActions()[1].type).toEqual(expectedActions[1].type);
});
});

it('should updateProfile', () => {
const expectedActions = [
{type: PROFILE_UPDATE_REQUEST},
{type: PROFILE_UPDATE_SUCCESS},
{type: GET_PROFILE_REQUEST},
{type: GET_PROFILE_SUCCESS}
{type: GET_PROFILE_REQUEST}
];

const store = mockStore({}, expectedActions);
return store.dispatch(actions.updateProfile('userid','username','email'));
const store = mockStore({});
return store.dispatch(actions.updateProfile('userid','username','email'))
.then(() => {
expect(store.getActions()).toEqual(expectedActions);
});
});

});

0 comments on commit 513612f

Please sign in to comment.