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

Allow 2nd method to get ownProps if factory doesn't require it. Fixes #604 #616

Merged
merged 1 commit into from Feb 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/connect/wrapMapToProps.js
Expand Up @@ -43,10 +43,12 @@ export function wrapMapToPropsFunc(mapToProps, methodName) {
: proxy.mapToProps(stateOrDispatch)
}

proxy.dependsOnOwnProps = getDependsOnOwnProps(mapToProps)
// allow detectFactoryAndVerify to get ownProps
proxy.dependsOnOwnProps = true

proxy.mapToProps = function detectFactoryAndVerify(stateOrDispatch, ownProps) {
proxy.mapToProps = mapToProps
proxy.dependsOnOwnProps = getDependsOnOwnProps(mapToProps)
let props = proxy(stateOrDispatch, ownProps)

if (typeof props === 'function') {
Expand Down
41 changes: 39 additions & 2 deletions test/components/connect.spec.js
Expand Up @@ -1881,6 +1881,43 @@ describe('React', () => {
expect(memoizedReturnCount).toBe(2)
})

it('should allow a mapStateToProps factory consuming just state to return a function that gets ownProps', () => {
const store = createStore(() => ({ value: 1 }))

let initialState
let initialOwnProps
let secondaryOwnProps
const mapStateFactory = function (factoryInitialState) {
initialState = factoryInitialState
initialOwnProps = arguments[1];
return (state, props) => {
secondaryOwnProps = props
return { }
}
}

@connect(mapStateFactory)
class Container extends Component {
render() {
return <Passthrough {...this.props} />
}
}

TestUtils.renderIntoDocument(
<ProviderMock store={store}>
<div>
<Container name="a" />
</div>
</ProviderMock>
)

store.dispatch({ type: 'test' })
expect(initialOwnProps).toBe(undefined)
expect(initialState).toNotBe(undefined)
expect(secondaryOwnProps).toNotBe(undefined)
expect(secondaryOwnProps.name).toBe("a")
})

it('should allow providing a factory function to mapDispatchToProps', () => {
let updatedCount = 0
let memoizedReturnCount = 0
Expand Down Expand Up @@ -2134,7 +2171,7 @@ describe('React', () => {
class BlockUpdates extends Component {
shouldComponentUpdate() { return false; }
render() { return this.props.children; }
}
}

const mapStateToProps = expect.createSpy().andCall(state => ({ count: state }))
@connect(mapStateToProps)
Expand Down Expand Up @@ -2169,6 +2206,6 @@ describe('React', () => {

store.dispatch({ type: 'INC' })
})

})
})