From b38a0423a36f0049332156f7107a77cc4fe57b57 Mon Sep 17 00:00:00 2001 From: kellyrmilligan Date: Mon, 8 Aug 2016 10:16:57 -0500 Subject: [PATCH 1/3] copies static methods to HOC --- src/connectToStores.js | 7 +++++++ test/connect-to-stores-test.js | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/connectToStores.js b/src/connectToStores.js index 2a04c37..75028e3 100644 --- a/src/connectToStores.js +++ b/src/connectToStores.js @@ -107,6 +107,13 @@ function connectToStores(Spec, Component = Spec) { StoreConnection.contextTypes = Component.contextTypes } + Object.getOwnPropertyNames(Spec).forEach(function (prop) { + if (prop === 'getPropsFromStores' || prop === 'getStores') return + if (isFunction(Spec[prop]) && !StoreConnection[prop]) { + StoreConnection[prop] = Spec[prop] + } + }) + return StoreConnection } diff --git a/test/connect-to-stores-test.js b/test/connect-to-stores-test.js index 73cd780..3a6a4e8 100644 --- a/test/connect-to-stores-test.js +++ b/test/connect-to-stores-test.js @@ -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 funtion outside to have been called') + }, + 'element mounts and unmounts'() { const div = document.createElement('div') From 6fea79c97ec2e9c639205cdd7236a3677020d67e Mon Sep 17 00:00:00 2001 From: kellyrmilligan Date: Tue, 9 Aug 2016 09:43:28 -0500 Subject: [PATCH 2/3] using hoistNonReactStatic per PR feedback --- package.json | 3 +++ src/connectToStores.js | 10 +++++----- test/connect-to-stores-test.js | 2 +- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 68f173b..9676303 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/src/connectToStores.js b/src/connectToStores.js index 75028e3..0852b33 100644 --- a/src/connectToStores.js +++ b/src/connectToStores.js @@ -47,6 +47,7 @@ */ import React from 'react' +import hoistNonReactStatic from 'hoist-non-react-statics' import { assign, isFunction } from './functions' function connectToStores(Spec, Component = Spec) { @@ -107,13 +108,12 @@ function connectToStores(Spec, Component = Spec) { StoreConnection.contextTypes = Component.contextTypes } - Object.getOwnPropertyNames(Spec).forEach(function (prop) { - if (prop === 'getPropsFromStores' || prop === 'getStores') return - if (isFunction(Spec[prop]) && !StoreConnection[prop]) { - StoreConnection[prop] = Spec[prop] - } + hoistNonReactStatic(StoreConnection, Spec, { + getStores: true, + getPropsFromStores: true }) + return StoreConnection } diff --git a/test/connect-to-stores-test.js b/test/connect-to-stores-test.js index 3a6a4e8..4549e0b 100644 --- a/test/connect-to-stores-test.js +++ b/test/connect-to-stores-test.js @@ -120,7 +120,7 @@ export default { assert.isNotFunction(wrappedComponent.getStores, 'expects getStores to not be copied') wrappedComponent.foo() - assert.strictEqual(outsideFunction.called, true, 'expects the funtion outside to have been called') + assert.strictEqual(outsideFunction.called, true, 'expects the function outside to have been called') }, 'element mounts and unmounts'() { From b8418613791d21f9e664c504163ff0d69fc75658 Mon Sep 17 00:00:00 2001 From: kellyrmilligan Date: Tue, 9 Aug 2016 11:43:39 -0500 Subject: [PATCH 3/3] change per PR feedback --- src/connectToStores.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/connectToStores.js b/src/connectToStores.js index 0852b33..1618401 100644 --- a/src/connectToStores.js +++ b/src/connectToStores.js @@ -47,7 +47,7 @@ */ import React from 'react' -import hoistNonReactStatic from 'hoist-non-react-statics' +import statics from 'hoist-non-react-statics' import { assign, isFunction } from './functions' function connectToStores(Spec, Component = Spec) { @@ -108,7 +108,7 @@ function connectToStores(Spec, Component = Spec) { StoreConnection.contextTypes = Component.contextTypes } - hoistNonReactStatic(StoreConnection, Spec, { + statics(StoreConnection, Spec, { getStores: true, getPropsFromStores: true })