Skip to content

Commit 818a3ec

Browse files
committed
Create more explicit error message
1 parent 357ee46 commit 818a3ec

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/components/createConnect.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,12 @@ export default function createConnect(React) {
8888
this.version = version;
8989
this.store = props.store || context.store;
9090

91-
invariant(this.store, '`store` must be passed in via the context or props');
91+
invariant(this.store,
92+
`Could not find "store" in either the context or ` +
93+
`props of "${this.constructor.displayName}". ` +
94+
`Either wrap the root component in a <Provider>, ` +
95+
`or explicitly pass "store" as a prop to "${this.constructor.displayName}".`
96+
)
9297

9398
this.stateProps = computeStateProps(this.store);
9499
this.dispatchProps = computeDispatchProps(this.store);

test/components/connect.spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,26 @@ describe('React', () => {
739739
expect(actualState).toEqual(expectedState);
740740
});
741741

742+
it('should throw an error if the store is not in the props or context', () => {
743+
class Container extends Component {
744+
render() {
745+
return <div />;
746+
}
747+
}
748+
749+
const decorator = connect(state => {});
750+
const Decorated = decorator(Container);
751+
const expectedError =
752+
`Invariant Violation: Could not find "store" in either the context ` +
753+
`or props of "Connect(Container)". Either wrap the root component in a `+
754+
`<Provider>, or explicitly pass "store" as a prop to "Connect(Container)".`;
755+
756+
expect(() => TestUtils.renderIntoDocument(<Decorated />)).toThrow(e => {
757+
expect(e.message).toEqual(expectedError)
758+
return true
759+
})
760+
})
761+
742762
it('should return the instance of the wrapped component for use in calling child methods', () => {
743763
const store = createStore(() => ({}));
744764

0 commit comments

Comments
 (0)