Skip to content

Commit 9d16da2

Browse files
committed
Close #309, Improve invariant validation error message
1 parent 2d5ee79 commit 9d16da2

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

src/components/connect.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ function getDisplayName(WrappedComponent) {
1818
return WrappedComponent.displayName || WrappedComponent.name || 'Component'
1919
}
2020

21-
function checkStateShape(stateProps, dispatch) {
21+
function checkStateShape(componentDisplayName, stateProps, dispatch) {
2222
invariant(
2323
isPlainObject(stateProps),
24-
'`%sToProps` must return an object. Instead received %s.',
24+
'`%s %sToProps` must return an object. Instead received %s.',
25+
componentDisplayName,
2526
dispatch ? 'mapDispatch' : 'mapState',
2627
stateProps
2728
)
@@ -45,11 +46,12 @@ export default function connect(mapStateToProps, mapDispatchToProps, mergeProps,
4546
// Helps track hot reloading.
4647
const version = nextVersion++
4748

48-
function computeMergedProps(stateProps, dispatchProps, parentProps) {
49+
function computeMergedProps(componentDisplayName, stateProps, dispatchProps, parentProps) {
4950
const mergedProps = finalMergeProps(stateProps, dispatchProps, parentProps)
5051
invariant(
5152
isPlainObject(mergedProps),
52-
'`mergeProps` must return an object. Instead received %s.',
53+
'`%s mergeProps` must return an object. Instead received %s.',
54+
componentDisplayName,
5355
mergedProps
5456
)
5557
return mergedProps
@@ -88,7 +90,7 @@ export default function connect(mapStateToProps, mapDispatchToProps, mergeProps,
8890
this.finalMapStateToProps(state, props) :
8991
this.finalMapStateToProps(state)
9092

91-
return checkStateShape(stateProps)
93+
return checkStateShape(this.constructor.displayName, stateProps)
9294
}
9395

9496
configureFinalMapState(store, props) {
@@ -100,7 +102,7 @@ export default function connect(mapStateToProps, mapDispatchToProps, mergeProps,
100102

101103
return isFactory ?
102104
this.computeStateProps(store, props) :
103-
checkStateShape(mappedState)
105+
checkStateShape(this.constructor.displayName, mappedState)
104106
}
105107

106108
computeDispatchProps(store, props) {
@@ -113,7 +115,7 @@ export default function connect(mapStateToProps, mapDispatchToProps, mergeProps,
113115
this.finalMapDispatchToProps(dispatch, props) :
114116
this.finalMapDispatchToProps(dispatch)
115117

116-
return checkStateShape(dispatchProps, true)
118+
return checkStateShape(this.constructor.displayName, dispatchProps, true)
117119
}
118120

119121
configureFinalMapDispatch(store, props) {
@@ -125,7 +127,7 @@ export default function connect(mapStateToProps, mapDispatchToProps, mergeProps,
125127

126128
return isFactory ?
127129
this.computeDispatchProps(store, props) :
128-
checkStateShape(mappedDispatch, true)
130+
checkStateShape(this.constructor.displayName, mappedDispatch, true)
129131
}
130132

131133
updateStatePropsIfNeeded() {
@@ -149,7 +151,7 @@ export default function connect(mapStateToProps, mapDispatchToProps, mergeProps,
149151
}
150152

151153
updateMergedPropsIfNeeded() {
152-
const nextMergedProps = computeMergedProps(this.stateProps, this.dispatchProps, this.props)
154+
const nextMergedProps = computeMergedProps(this.constructor.displayName, this.stateProps, this.dispatchProps, this.props)
153155
if (this.mergedProps && checkMergedEquals && shallowEqual(nextMergedProps, this.mergedProps)) {
154156
return false
155157
}

test/components/connect.spec.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,71 +1004,71 @@ describe('React', () => {
10041004
{makeContainer(() => 1, () => ({}), () => ({}))}
10051005
</ProviderMock>
10061006
)
1007-
}).toThrow(/mapState/)
1007+
}).toThrow(/Connect\(Container\) mapState/)
10081008

10091009
expect(() => {
10101010
TestUtils.renderIntoDocument(
10111011
<ProviderMock store={store}>
10121012
{makeContainer(() => 'hey', () => ({}), () => ({}))}
10131013
</ProviderMock>
10141014
)
1015-
}).toThrow(/mapState/)
1015+
}).toThrow(/Connect\(Container\) mapState/)
10161016

10171017
expect(() => {
10181018
TestUtils.renderIntoDocument(
10191019
<ProviderMock store={store}>
10201020
{makeContainer(() => new AwesomeMap(), () => ({}), () => ({}))}
10211021
</ProviderMock>
10221022
)
1023-
}).toThrow(/mapState/)
1023+
}).toThrow(/Connect\(Container\) mapState/)
10241024

10251025
expect(() => {
10261026
TestUtils.renderIntoDocument(
10271027
<ProviderMock store={store}>
10281028
{makeContainer(() => ({}), () => 1, () => ({}))}
10291029
</ProviderMock>
10301030
)
1031-
}).toThrow(/mapDispatch/)
1031+
}).toThrow(/Connect\(Container\) mapDispatch/)
10321032

10331033
expect(() => {
10341034
TestUtils.renderIntoDocument(
10351035
<ProviderMock store={store}>
10361036
{makeContainer(() => ({}), () => 'hey', () => ({}))}
10371037
</ProviderMock>
10381038
)
1039-
}).toThrow(/mapDispatch/)
1039+
}).toThrow(/Connect\(Container\) mapDispatch/)
10401040

10411041
expect(() => {
10421042
TestUtils.renderIntoDocument(
10431043
<ProviderMock store={store}>
10441044
{makeContainer(() => ({}), () => new AwesomeMap(), () => ({}))}
10451045
</ProviderMock>
10461046
)
1047-
}).toThrow(/mapDispatch/)
1047+
}).toThrow(/Connect\(Container\) mapDispatch/)
10481048

10491049
expect(() => {
10501050
TestUtils.renderIntoDocument(
10511051
<ProviderMock store={store}>
10521052
{makeContainer(() => ({}), () => ({}), () => 1)}
10531053
</ProviderMock>
10541054
)
1055-
}).toThrow(/mergeProps/)
1055+
}).toThrow(/Connect\(Container\) mergeProps/)
10561056

10571057
expect(() => {
10581058
TestUtils.renderIntoDocument(
10591059
<ProviderMock store={store}>
10601060
{makeContainer(() => ({}), () => ({}), () => 'hey')}
10611061
</ProviderMock>
10621062
)
1063-
}).toThrow(/mergeProps/)
1063+
}).toThrow(/Connect\(Container\) mergeProps/)
10641064

10651065
expect(() => {
10661066
TestUtils.renderIntoDocument(
10671067
<ProviderMock store={store}>
10681068
{makeContainer(() => ({}), () => ({}), () => new AwesomeMap())}
10691069
</ProviderMock>
10701070
)
1071-
}).toThrow(/mergeProps/)
1071+
}).toThrow(/Connect\(Container\) mergeProps/)
10721072
})
10731073

10741074
it('should recalculate the state and rebind the actions on hot update', () => {

0 commit comments

Comments
 (0)