Skip to content

Commit

Permalink
Create example tests for async action creators and then reducers too
Browse files Browse the repository at this point in the history
  • Loading branch information
GordyD committed Jan 24, 2016
1 parent e5a0105 commit 3ac32ac
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"presets": ["react", "es2015" , "stage-0"],
"plugins": [
"babel-plugin-rewire"
"rewire"
],
"env": {
"start": {
Expand Down
74 changes: 47 additions & 27 deletions test/actions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ var expect = chai.expect;

import configureStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import nock from 'nock';

import * as actions from '../universal/actions/PulseActions.js';
import * as types from '../universal/constants/ActionTypes';

describe('PulseActions', () => {
describe('Actions', () => {
afterEach(function() {
actions.__ResetDependency__('request')
})
});

/**
* Example of writing a test on a syncronous action creator
Expand All @@ -39,32 +38,53 @@ describe('PulseActions', () => {
/**
* Example of writing a test on an asyncronous action creator
*/
// describe('loadEvents', () => {
// const mockStore = configureStore([thunk]);
// it('should run', (done) => {
// let requestMock = {
// get: () => {
// set: () => {
// end: (x) => {
// x(null, {
// body: [ { name: 'Awesome', value: 54 } ]
// });
// }
// }
// }
// };
describe('loadEvents', () => {
const mockStore = configureStore([thunk]);
it('should trigger a LOAD_EVENTS_REQUEST and LOAD_EVENTS_SUCCESS action when succesful', (done) => {
let requestMock = {
get: () => ({
set: () => ({
end: (x) => x(null, {
body: [ { name: 'Awesome', value: 54 } ]
})
})
})
};

// actions.__Rewire__('request', requestMock);
actions.__Rewire__('request', requestMock);

// let expectedActions = [
// { type: 'LOAD_EVENTS_REQUEST' },
// { type: 'LOAD_EVENTS_SUCCESS', events: [ { name: 'Awesome', value: 54 } ] }
// ];
let expectedActions = [
{ type: 'LOAD_EVENTS_REQUEST' },
{ type: 'LOAD_EVENTS_SUCCESS', events: [ { name: 'Awesome', value: 54 } ] }
];

// let initialState = {pulseApp: { events: [], userId: 'baseUser'} };
// let store = mockStore(initialState, expectedActions, done);
let initialState = {pulseApp: { events: [], userId: 'baseUser'} };
let store = mockStore(initialState, expectedActions, done);

store.dispatch(actions.loadEvents());
});

it('should trigger a LOAD_EVENTS_REQUEST and LOAD_EVENTS_FAILURE action when unsuccessful', (done) => {
let error = 'An Error Occurred!';
let requestMock = {
get: () => ({
set: () => ({
end: (x) => x(error)
})
})
};

actions.__Rewire__('request', requestMock);

// store.dispatch(actions.loadEvents());
// });
// });
let expectedActions = [
{ type: 'LOAD_EVENTS_REQUEST' },
{ type: 'LOAD_EVENTS_FAILURE', error: error }
];

let initialState = {pulseApp: { events: [], userId: 'baseUser'} };
let store = mockStore(initialState, expectedActions, done);

store.dispatch(actions.loadEvents());
});
});
});
88 changes: 88 additions & 0 deletions test/reducers.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/* global describe */
/* global it */
/* global afterEach */

import 'babel-polyfill'; // For use of Object.assign

import sinon from 'sinon';
import chai from 'chai';

var expect = chai.expect;

import reducer from '../universal/reducers';
import * as actions from '../universal/actions/PulseActions.js';

describe('Reducers', () => {

/**
* Example of writing a test on a reducing functiom
*/
describe('setUserId', () => {
it('should set user id', () => {
let initialStateForTest = { userId: null };
let userId = 234;
let action = actions.setUserId(userId);

expect(initialStateForTest.userId).to.be.null

let state = reducer(initialStateForTest, action);
expect(state.userId).to.equal(userId);
});
});

describe('addEvent', () => {
describe('request', () => {
it('should set isWorking to true', () => {
let initialStateForTest = { isWorking: false };
let action = actions.addEventRequest();

expect(initialStateForTest.isWorking).to.be.false

let state = reducer(initialStateForTest, action);
expect(state.isWorking).to.be.true;
});
});

describe('success', () => {
it('should set isWorking to false and add event to events', () => {
let events = [
{ id: 22, name: 'Entry', value: 20 }
];
let initialStateForTest = { isWorking: true, events: events };
let event = { id: 25, name: 'Another Entry', value: 50 };

let action = actions.addEventSuccess(event);

expect(initialStateForTest.isWorking).to.be.true
expect(initialStateForTest.events.length).to.equal(events.length);


let state = reducer(initialStateForTest, action);
expect(state.isWorking).to.be.false;
expect(state.events.length).to.equal(events.length + 1);
});
});

describe('failure', () => {
it('should set isWorking to false and error and not change events', () => {
let events = [
{ id: 22, name: 'Entry', value: 20 }
];
let initialStateForTest = { isWorking: true, events: events, error: null };
let error = 'some error';

let action = actions.addEventFailure(error);

expect(initialStateForTest.isWorking).to.be.true
expect(initialStateForTest.error).to.be.null
expect(initialStateForTest.events.length).to.equal(events.length);


let state = reducer(initialStateForTest, action);
expect(state.isWorking).to.be.false;
expect(state.error).to.equal(error);
expect(state.events.length).to.equal(events.length);
});
});
});
});

0 comments on commit 3ac32ac

Please sign in to comment.