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

Throw error on undefined value from store function #193

Closed
wants to merge 1 commit 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
17 changes: 12 additions & 5 deletions src/utils/composeStores.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import mapValues from '../utils/mapValues';
import pick from '../utils/pick';
import mapValues from './mapValues';
import pick from './pick';

// UNDEF = undefined;
const UNDEF = void 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like a hack. Can you please check typeof instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a pretty common pattern. I will make the change. How should I fix the other UNDEF use in the test?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can replace return UNDEF with just return, and use typeof x === 'undefined' as a check.
Or are you referring to something else?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, there is an ESLint rule that forbids return; when there is another type returned elsewhere. I think we can just use undefined though. We don't have to worry about users overwriting undefined in tests.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can also invert the condition.
Instead of returning undefined in some cases, return something in other cases.

function bad() {
  if (action.type !== 'something') {
    return something;
  }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about that. I was trying to make the test as obvious as possible. I will change it to above


export default function composeStores(stores) {
const finalStores = pick(stores, (val) => typeof val === 'function');
return function Composition(atom = {}, action) {
return mapValues(finalStores, (store, key) =>
store(atom[key], action)
);
return mapValues(finalStores, (store, key) => {
const state = store(atom[key], action);
if (state === UNDEF) {
throw new Error(`Store ${key} returns undefined. By default store should return original state.`);
}
return state;
});
};
}
27 changes: 27 additions & 0 deletions test/composeStores.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import expect from 'expect';
import { composeStores } from '../src';

// UNDEF = undefined;
const UNDEF = void 0;

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

expect(Object.keys(store({}, {type: 'push'}))).toEqual(['stack']);
});
it('should throw an error if undefined return from store', () => {
const store = composeStores({
stack: (state = []) => state,
bad: (state = [], action) => {
if (action.type === 'something') {
return state;
}
}
});
expect(() => store({}, {type: '@@testType'})).toThrow();
});
it('should throw an error if undefined return not by default', () => {
const store = composeStores({
stack: (state = []) => state,
bad: (state = 1, action) => {
if (action.type === 'something') {
return UNDEF;
}
return state;
}
});
expect(store({}, {type: '@@testType'})).toEqual({stack: [], bad: 1});
expect(() => store({}, {type: 'something'})).toThrow();
});
});
});