Skip to content

Commit 527497f

Browse files
committed
Throws an error when select not returns an object
On Connector, verify if `select` prop, which is a function, returns an object, and if not, throws an error. Issue #78
1 parent ca935a1 commit 527497f

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/components/createConnector.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import identity from 'lodash/utility/identity';
22
import shallowEqual from '../utils/shallowEqual';
3+
import isPlainObject from 'lodash/lang/isPlainObject';
34

45
export default function createConnector(React) {
56
const { Component, PropTypes } = React;
@@ -59,6 +60,11 @@ export default function createConnector(React) {
5960
selectState({ context, props } = this) {
6061
const state = context.redux.getState();
6162
const slice = props.select(state);
63+
64+
if (!isPlainObject(slice)) {
65+
throw new Error('prop `select` should always return an object');
66+
}
67+
6268
return { slice };
6369
}
6470

test/components/Connector.spec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,5 +144,21 @@ describe('React', () => {
144144
const div = TestUtils.findRenderedDOMComponentWithTag(tree, 'div');
145145
expect(div.props.dispatch).toBe(redux.dispatch);
146146
});
147+
148+
it('should throw an error if `state` returns anything but a plain object', () => {
149+
const redux = createRedux(() => {});
150+
151+
expect(() => {
152+
TestUtils.renderIntoDocument(
153+
<Provider redux={redux}>
154+
{() => (
155+
<Connector state={() => 1}>
156+
{() => <div />}
157+
</Connector>
158+
)}
159+
</Provider>
160+
);
161+
}).toThrow(/select/);
162+
});
147163
});
148164
});

0 commit comments

Comments
 (0)