Skip to content

Commit

Permalink
Merge pull request #189 from planetcohen/fix-xxxToProps-length-check
Browse files Browse the repository at this point in the history
fix length comparisons on xxxToProps functions
  • Loading branch information
gaearon committed Dec 12, 2015
2 parents e33310e + e964931 commit 34ee9f0
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/components/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
103 changes: 101 additions & 2 deletions test/components/connect.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <Passthrough {...this.props}/>
Expand Down Expand Up @@ -467,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 <Passthrough {...this.props}/>
}
}

class OuterComponent extends Component {
constructor() {
super()
this.state = { foo: 'FOO' }
}

setFoo(foo) {
this.setState({ foo })
}

render() {
return (
<div>
<WithoutProps {...this.state} />
</div>
)
}
}

let outerComponent
TestUtils.renderIntoDocument(
<ProviderMock store={store}>
<OuterComponent ref={c => outerComponent = c} />
</ProviderMock>
)
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)

Expand Down Expand Up @@ -524,10 +573,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 <Passthrough {...this.props}/>
Expand Down Expand Up @@ -566,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 <Passthrough {...this.props}/>
}
}

class OuterComponent extends Component {
constructor() {
super()
this.state = { foo: 'FOO' }
}

setFoo(foo) {
this.setState({ foo })
}

render() {
return (
<div>
<WithoutProps {...this.state} />
</div>
)
}
}

let outerComponent
TestUtils.renderIntoDocument(
<ProviderMock store={store}>
<OuterComponent ref={c => outerComponent = c} />
</ProviderMock>
)

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)

Expand Down

0 comments on commit 34ee9f0

Please sign in to comment.