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

copies static methods to HOC #19

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,8 @@
"react-dom": "0.14.0",
"rimraf": "2.4.3",
"sinon": "1.17.1"
},
"dependencies": {
"hoist-non-react-statics": "^1.2.0"
}
}
7 changes: 7 additions & 0 deletions src/connectToStores.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
*/

import React from 'react'
import hoistNonReactStatic from 'hoist-non-react-statics'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"statics" probably

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure. was just copying that from the modules repo. update en route.

import { assign, isFunction } from './functions'

function connectToStores(Spec, Component = Spec) {
Expand Down Expand Up @@ -107,6 +108,12 @@ function connectToStores(Spec, Component = Spec) {
StoreConnection.contextTypes = Component.contextTypes
}

hoistNonReactStatic(StoreConnection, Spec, {
getStores: true,
getPropsFromStores: true
})


return StoreConnection
}

Expand Down
32 changes: 32 additions & 0 deletions test/connect-to-stores-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,38 @@ export default {
assert.throws(() => connectToStores(BadComponentOne), 'expects the wrapped component to have a static getStores() method')
},

'static methods on wrapped component are copied to StoreConnection component'() {

let outsideFunction = sinon.spy();

const ComponentWithStatics = React.createClass({
statics: {
getStores() {
return [testStore]
},
getPropsFromStores(props) {
return testStore.getState()
},
foo() {
outsideFunction()
}
},
render() {
return React.createElement('div', null, 'statics')
}
})

const wrappedComponent = connectToStores(ComponentWithStatics)


assert.isFunction(wrappedComponent.foo, 'expects foo to also be a function on the wrapped component')
assert.isNotFunction(wrappedComponent.getPropsFromStores, 'expects getPropsFromStores to not be copied')
assert.isNotFunction(wrappedComponent.getStores, 'expects getStores to not be copied')

wrappedComponent.foo()
assert.strictEqual(outsideFunction.called, true, 'expects the function outside to have been called')
},

'element mounts and unmounts'() {
const div = document.createElement('div')

Expand Down