Skip to content

Commit

Permalink
Add tests for actions and reducers from redux counter example
Browse files Browse the repository at this point in the history
  • Loading branch information
zalmoxisus committed Oct 14, 2015
1 parent 9815ca7 commit 65c52ca
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 1 deletion.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
"compress:extension": "npm run build:extension && gulp compress:extension",
"compress:app": "npm run build:app && gulp compress:app",
"clean": "rm -rf build/ && rm -rf dev/",
"test": "eslint ."
"lint": "eslint .",
"test:app": "BABEL_ENV=test mocha --recursive --compilers js:babel-core/register test/app",
"test": "npm run test:app"
},
"repository": {
"type": "git",
Expand Down
77 changes: 77 additions & 0 deletions test/app/actions/counter.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import expect from 'expect';
import { applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import { INCREMENT_COUNTER, DECREMENT_COUNTER } from '../../../src/app/constants/ActionTypes';
import * as actions from '../../../src/app/actions/counter';

const middlewares = [thunk];

/*
* Creates a mock of Redux store with middleware.
*/
function mockStore(getState, expectedActions, onLastAction) {
if (!Array.isArray(expectedActions)) {
throw new Error('expectedActions should be an array of expected actions.');
}
if (typeof onLastAction !== 'undefined' && typeof onLastAction !== 'function') {
throw new Error('onLastAction should either be undefined or function.');
}

function mockStoreWithoutMiddleware() {
return {
getState() {
return typeof getState === 'function' ?
getState() :
getState;
},

dispatch(action) {
const expectedAction = expectedActions.shift();
expect(action).toEqual(expectedAction);
if (onLastAction && !expectedActions.length) {
onLastAction();
}
return action;
}
};
}

const mockStoreWithMiddleware = applyMiddleware(
...middlewares
)(mockStoreWithoutMiddleware);

return mockStoreWithMiddleware();
}

describe('actions', () => {
it('increment should create increment action', () => {
expect(actions.increment()).toEqual({ type: actions.INCREMENT_COUNTER });
});

it('decrement should create decrement action', () => {
expect(actions.decrement()).toEqual({ type: actions.DECREMENT_COUNTER });
});

it('incrementIfOdd should create increment action', (done) => {
const expectedActions = [
{ type: actions.INCREMENT_COUNTER }
];
const store = mockStore({ counter: 1 }, expectedActions, done);
store.dispatch(actions.incrementIfOdd());
});

it('incrementIfOdd shouldnt create increment action if counter is even', (done) => {
const expectedActions = [];
const store = mockStore({ counter: 2 }, expectedActions);
store.dispatch(actions.incrementIfOdd());
done();
});

it('incrementAsync should create increment action', (done) => {
const expectedActions = [
{ type: actions.INCREMENT_COUNTER }
];
const store = mockStore({ counter: 0 }, expectedActions, done);
store.dispatch(actions.incrementAsync(100));
});
});
23 changes: 23 additions & 0 deletions test/app/reducers/counter.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import expect from 'expect';
import counter from '../../../src/app/reducers/counter';
import { INCREMENT_COUNTER, DECREMENT_COUNTER } from '../../../src/app/actions/counter';

describe('reducers', () => {
describe('counter', () => {
it('should handle initial state', () => {
expect(counter(undefined, {})).toBe(0);
});

it('should handle INCREMENT_COUNTER', () => {
expect(counter(1, { type: INCREMENT_COUNTER })).toBe(2);
});

it('should handle DECREMENT_COUNTER', () => {
expect(counter(1, { type: DECREMENT_COUNTER })).toBe(0);
});

it('should handle unknown action type', () => {
expect(counter(1, { type: 'unknown' })).toBe(1);
});
});
});

0 comments on commit 65c52ca

Please sign in to comment.