Skip to content

Commit

Permalink
More tests and license
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakob Nilsson-Ehle committed Feb 9, 2017
1 parent c7d1b3a commit 4f9fc4b
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 6 deletions.
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Jakob Nilsson-Ehle

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,7 @@ const reducer = chain([

reducer(1, {value: 1, type: "SUBTRACT_VALUE"}) //1
reducer(1, {value: 2, type: "ADD_VALUE"}) //3


## license
See [LICENSE.md]
10 changes: 6 additions & 4 deletions src/merge-states.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import isPlainObject from 'is-plain-object';
import assign from 'object-assign';

const merge = (reducer) => {
const mergeStates = (reducer) => {
if (typeof reducer !== 'function') {
throw new Error('Argument supplied to `merge` is not a reducer');
}

return (state, action) => {
if (!isPlainObject(state)) {
throw new Error('`merge` only works with plain object states');
const type = typeof state;
throw new Error(`"mergeStates" only works with plain object states, but got a "${type}"`);
}
const nextState = reducer(state, action);
if (!isPlainObject(nextState)) {
throw new Error('The reducer in `merge` did not return a plain object');
const type = typeof nextState;
throw new Error(`The reducer in "mergeStates" did not return a plain object. Instead it was a "${type}"`);
}
return assign({}, state, nextState);
};
};

export default merge;
export default mergeStates;
2 changes: 1 addition & 1 deletion test/always.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sinon from 'sinon';
import chai, { expect } from 'chai';
import sinonChai from 'sinon-chai';
import always from '../lib/always';
import { always } from '../lib';

chai.use(sinonChai);

Expand Down
2 changes: 1 addition & 1 deletion test/chain.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sinon from 'sinon';
import chai, { expect } from 'chai';
import sinonChai from 'sinon-chai';
import chain from '../lib/chain';
import { chain } from '../lib';

chai.use(sinonChai);

Expand Down
41 changes: 41 additions & 0 deletions test/merge-states.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import sinon from 'sinon';
import chai, { expect } from 'chai';
import sinonChai from 'sinon-chai';
import { mergeStates } from '../lib';

chai.use(sinonChai);

describe('mergeStates', () => {
it('throws an error if the inner reducer is not an object', () => {
expect(() => mergeStates()).to.throw('not a reducer');
expect(() => mergeStates(null)).to.throw('not a reducer');
expect(() => mergeStates(1)).to.throw('not a reducer');
expect(() => mergeStates(new Date())).to.throw('not a reducer');
expect(() => mergeStates({})).to.throw('not a reducer');
});

it('does not throw if the inner reducer is a function', () => {
expect(() => mergeStates(() => {})).to.not.throw;
});

it('throws an error if the supplied state is not an object', () => {
const reducer = mergeStates(() => {});
expect(() => reducer()).to.throw('"mergeStates" only works with plain object states');
expect(() => reducer(null)).to.throw('"mergeStates" only works with plain object states');
expect(() => reducer(1)).to.throw('"mergeStates" only works with plain object states');
expect(() => reducer(() => {})).to.throw('"mergeStates" only works with plain object states');
expect(() => reducer(new Date())).to.throw('"mergeStates" only works with plain object states');
});

it('does not throw errors it the state is an object', () => {
const reducer = mergeStates(() => {});
expect(() => reducer({})).to.not.throw('"mergeStates" only works with plain object states');
});

it('calls the inner reducer', () => {
const innerReducer = sinon.stub().returns({});
const reducer = mergeStates(innerReducer);
reducer({});
expect(innerReducer).to.have.been.calledOnce;
});
});

0 comments on commit 4f9fc4b

Please sign in to comment.