From 6c504b966839be0e739d5a566c8fb844c27a6c44 Mon Sep 17 00:00:00 2001 From: Michael Cohen Date: Fri, 13 Nov 2015 08:47:58 -0800 Subject: [PATCH 1/2] fix length comparisons on xxxToProps functions Testing finalMap{State|Dispatch}ToProps.length > 1 breaks when these functions are wrapped in generic decorator functions. Instead, test for !== 1. --- src/components/connect.js | 4 ++-- test/components/connect.spec.js | 8 ++++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/components/connect.js b/src/components/connect.js index 5b31632d1..3e01744f1 100644 --- a/src/components/connect.js +++ b/src/components/connect.js @@ -28,8 +28,8 @@ export default function connect(mapStateToProps, mapDispatchToProps, mergeProps, wrapActionCreators(mapDispatchToProps) : mapDispatchToProps || defaultMapDispatchToProps const finalMergeProps = mergeProps || defaultMergeProps - const shouldUpdateStateProps = finalMapStateToProps.length > 1 - const shouldUpdateDispatchProps = finalMapDispatchToProps.length > 1 + const shouldUpdateStateProps = finalMapStateToProps.length !== 1 + const shouldUpdateDispatchProps = finalMapDispatchToProps.length !== 1 const { pure = true, withRef = false } = options // Helps track hot reloading. diff --git a/test/components/connect.spec.js b/test/components/connect.spec.js index bae556e0a..89ca687b7 100644 --- a/test/components/connect.spec.js +++ b/test/components/connect.spec.js @@ -426,10 +426,12 @@ describe('React', () => { let invocationCount = 0 - @connect(() => { + /*eslint-disable no-unused-vars */ + @connect((arg1) => { invocationCount++ return {} }) + /*eslint-enable no-unused-vars */ class WithoutProps extends Component { render() { return @@ -524,10 +526,12 @@ describe('React', () => { let invocationCount = 0 - @connect(null, () => { + /*eslint-disable no-unused-vars */ + @connect(null, (arg1) => { invocationCount++ return {} }) + /*eslint-enable no-unused-vars */ class WithoutProps extends Component { render() { return From e964931e138ad21a1746398b493ee93b1b2c14c3 Mon Sep 17 00:00:00 2001 From: Michael Cohen Date: Fri, 13 Nov 2015 09:15:32 -0800 Subject: [PATCH 2/2] add tests for zero arity --- test/components/connect.spec.js | 95 +++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/test/components/connect.spec.js b/test/components/connect.spec.js index 89ca687b7..82edb12c0 100644 --- a/test/components/connect.spec.js +++ b/test/components/connect.spec.js @@ -469,6 +469,53 @@ describe('React', () => { expect(invocationCount).toEqual(2) }) + it('should invoke mapState every time props are changed if it has zero arguments', () => { + const store = createStore(stringBuilder) + + let invocationCount = 0 + + @connect(() => { + invocationCount++ + return {} + }) + + class WithoutProps extends Component { + render() { + return + } + } + + class OuterComponent extends Component { + constructor() { + super() + this.state = { foo: 'FOO' } + } + + setFoo(foo) { + this.setState({ foo }) + } + + render() { + return ( +
+ +
+ ) + } + } + + let outerComponent + TestUtils.renderIntoDocument( + + outerComponent = c} /> + + ) + outerComponent.setFoo('BAR') + outerComponent.setFoo('DID') + + expect(invocationCount).toEqual(4) + }) + it('should invoke mapState every time props are changed if it has a second argument', () => { const store = createStore(stringBuilder) @@ -570,6 +617,54 @@ describe('React', () => { expect(invocationCount).toEqual(1) }) + it('should invoke mapDispatch every time props are changed if it has zero arguments', () => { + const store = createStore(stringBuilder) + + let invocationCount = 0 + + @connect(null, () => { + invocationCount++ + return {} + }) + + class WithoutProps extends Component { + render() { + return + } + } + + class OuterComponent extends Component { + constructor() { + super() + this.state = { foo: 'FOO' } + } + + setFoo(foo) { + this.setState({ foo }) + } + + render() { + return ( +
+ +
+ ) + } + } + + let outerComponent + TestUtils.renderIntoDocument( + + outerComponent = c} /> + + ) + + outerComponent.setFoo('BAR') + outerComponent.setFoo('DID') + + expect(invocationCount).toEqual(3) + }) + it('should invoke mapDispatch every time props are changed if it has a second argument', () => { const store = createStore(stringBuilder)