Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amsardesai committed Feb 14, 2016
1 parent 6cfbf4c commit ae66d56
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 8 deletions.
12 changes: 4 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,19 @@ module.exports = function testReducer(reducer) {

// Perform invariant checks
if (typeof from === 'undefined') {
assert(false, 'The `from` was not specified in reducer assertion call.');
assert(false, 'The `from` option was not specified in the reducer assertion call.');
}

if (typeof to === 'undefined') {
assert(false, 'The `to` was not specified in reducer assertion call.');
assert(false, 'The `to` option was not specified in the reducer assertion call.');
}

if (typeof action === 'undefined') {
assert(false, 'The `action` was not specified in reducer assertion call.');
assert(false, 'The `action` option was not specified in the reducer assertion call.');
}

// Get the next state
try {
var result = reducer(from, action);
} catch (e) {
assert(false, 'The reducer threw an exception.');
}
var result = reducer(from, action);

// Assert strict equality
assert.deepStrictEqual(result, to);
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@
"license": "MIT",
"dependencies": {
"core-assert": "^0.1.3"
},
"devDependencies": {
"chai": "^3.5.0",
"mocha": "^2.4.5"
}
}
79 changes: 79 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@

var testReducer = require('./index');
var expect = require('chai').expect;

var goodSample = function(state, action) {
if (action.type === 'INCREMENT') {
return { counter: state.counter + 1 };
} else if (action.type === 'DECREMENT') {
return { counter: state.counter - 1 };
} else {
return { counter: 0 };
}
};

var badSample = function(state, action) {
if (action.type === 'GET_VALUE') {
return 'someOtherValue';
}
};

describe('#testReducer', function() {
var assertReducer;
beforeEach(function() {
assertReducer = testReducer(goodSample);
});

it('can perform deep equality comparisons', function() {
expect(function() {
assertReducer({
from: { counter: 0 },
to: { counter: 1 },
action: { type: 'INCREMENT' },
});
assertReducer({
from: { counter: 1 },
to: { counter: 0 },
action: { type: 'DECREMENT' },
});
}).to.not.throw(Error);
});

it('throws errors for bad reducers', function() {
badAssertReducer = testReducer(badSample);
expect(function() {
badAssertReducer({
from: '',
to: 'someValue',
action: { type: 'GET_VALUE' },
});
}).to.throw(Error);
});

it('throws an error if `from` is not specified', function() {
expect(function() {
assertReducer({
to: { counter: 1 },
action: { type: 'INCREMENT' },
});
}).to.throw('The `from` option was not specified in the reducer assertion call.');
});

it('throws an error if `to` is not specified', function() {
expect(function() {
assertReducer({
from: { counter: 0 },
action: { type: 'INCREMENT' },
});
}).to.throw('The `to` option was not specified in the reducer assertion call.');
});

it('throws an error if `action` is not specified', function() {
expect(function() {
assertReducer({
from: { counter: 0 },
to: { counter: 1 },
});
}).to.throw('The `action` option was not specified in the reducer assertion call.');
});
});

0 comments on commit ae66d56

Please sign in to comment.