Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Checking that the return value of every store function inside compose… #174

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,20 @@
"react": "^0.13.0",
"react-hot-loader": "^1.2.7",
"rimraf": "^2.3.4",
"sinon": "^1.15.3",
"webpack": "^1.9.6",
"webpack-dev-server": "^1.8.2"
},
"dependencies": {
"invariant": "^2.0.0"
},
"npmName": "redux",
"npmFileMap": [{
"basePath": "/dist/",
"files": [
"*.js"
]
}]
"npmFileMap": [
{
"basePath": "/dist/",
"files": [
"*.js"
]
}
]
}
12 changes: 12 additions & 0 deletions src/utils/composeStores.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@ import pick from '../utils/pick';

export default function composeStores(stores) {
const finalStores = pick(stores, (val) => typeof val === 'function');

const dummyAction = {};
const dummyStore = {};

if (process.env.NODE_ENV !== 'production') {
Object.keys(finalStores).forEach(key => {
if (finalStores[key](dummyStore, dummyAction) !== dummyStore) {
console.warn(`Your ${key} Store must return the state given to it for any unknown actions.`);
}
});
}

return function Composition(atom = {}, action) {
return mapValues(finalStores, (store, key) =>
store(atom[key], action)
Expand Down
49 changes: 49 additions & 0 deletions test/composeStores.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
import expect from 'expect';
import { composeStores } from '../src';
import sinon from 'sinon';

describe('Utils', () => {

var sandbox;
beforeEach(function() {
sandbox = sinon.sandbox.create();
sandbox.stub(console, 'warn');
});

afterEach(function() {
sandbox.restore();
});

describe('composeStores', () => {
it('should return a store that maps state keys to reducer functions', () => {
const store = composeStores({
Expand All @@ -27,5 +39,42 @@ describe('Utils', () => {

expect(Object.keys(store({}, {type: 'push'}))).toEqual(['stack']);
});

it('should check that return value of every reducer is the state given to it for any unknown actions #1', () => {

const store = composeStores({
counter: (state = 0, action) =>
action.type === 'increment' ? state + 1 : state,
stack: (state = [], action) =>
action.type === 'push' ? [...state, action.value] : state,
some: (state = initialState, action) => {}
});

sinon.assert.calledOnce(console.warn);

});

it('should check that return value of every reducer is the state given to it for any unknown actions #2', () => {

const store = composeStores({
someOther: (state = initialState, action) => 'somevalue'
});
sinon.assert.calledOnce(console.warn);

});

it('should check that return value of every reducer is the state given to it for any unknown actions #3', () => {

const store = composeStores({
counter: (state = 0, action) =>
action.type === 'increment' ? state + 1 : state,
stack: (state = [], action) =>
action.type === 'push' ? [...state, action.value] : state,
});
sinon.assert.notCalled(console.warn);

});


});
});