Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix length comparisons on xxxToProps functions #189

Merged
merged 2 commits into from
Dec 12, 2015

Conversation

planetcohen
Copy link
Contributor

Testing finalMap{State|Dispatch}ToProps.length > 1 breaks when these functions are wrapped in generic decorator functions. Instead, test for !== 1.

For example, let's say I wanted to write a decorator function that logs function invocation like so:

function traceExecution(fn) {
  const name = fn.name;
  return function() {
    console.log('ENTER: ', name);
    const result = fn.apply(this, arguments);
    console.log('LEAVE: ', name, ' --> ', result);
    return result;
  }
}

Then, I want to wrap my mapStateToProps and mapDispatchToProps functions with traceExecution and pass it to connect:

function mapStateToProps(state, props) { /* stuff here */}
function mapDispatchToProps(dispatch, props) { /* stuff here */}
const connector = connect(traceExecution(mapStateToProps), traceExecution(mapDispatchToProps))

This would not work without these fixes, since the functions returned by the decorator have arity 0 and would fail the "> 1" length test.

By changing the arity test to be "!== 1" instead of "> 1", you enable this scenario to work as expected.

Testing finalMap{State|Dispatch}ToProps.length > 1 breaks when these functions are wrapped in generic decorator functions. Instead, test for !== 1.
@gaearon
Copy link
Contributor

gaearon commented Nov 13, 2015

Fair enough. Mind adding tests for this?

@planetcohen
Copy link
Contributor Author

Done - I added tests checking for expected behavior when arity is zero.

gaearon added a commit that referenced this pull request Dec 12, 2015
fix length comparisons on xxxToProps functions
@gaearon gaearon merged commit 34ee9f0 into reduxjs:master Dec 12, 2015
@gaearon
Copy link
Contributor

gaearon commented Dec 12, 2015

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants