-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jakob Nilsson-Ehle
committed
Feb 9, 2017
1 parent
c7d1b3a
commit 4f9fc4b
Showing
6 changed files
with
74 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
}); |