diff --git a/.eslintrc b/.eslintrc index fb96f47..7db73a3 100644 --- a/.eslintrc +++ b/.eslintrc @@ -42,7 +42,6 @@ "no-invalid-regexp": 2, "no-irregular-whitespace": 2, "no-obj-calls": 2, - "no-reserved-keys": 2, "no-sparse-arrays": 2, "no-unreachable": 2, "use-isnan": 2, @@ -118,7 +117,7 @@ "no-new-object": 2, "no-spaced-func": 2, "no-trailing-spaces": 2, - "no-wrap-func": 2, + "no-extra-parens": 2, "no-underscore-dangle": 0, "one-var": [2, "never"], "padded-blocks": [2, "never"], @@ -126,11 +125,10 @@ "before": false, "after": true }], - "space-after-keywords": 2, + "keyword-spacing": 2, "space-before-blocks": 2, "space-infix-ops": 2, - "space-return-throw-case": 2, - "spaced-line-comment": 2, + "spaced-comment": 2, /* custom rules */ "no-console": 0, @@ -142,7 +140,7 @@ "func-names": 0, "consistent-this": [2, "this"], "func-style": [0, "expression"], - "generator-star": [2, "end"], + "generator-star-spacing": [2, "after"], "max-nested-callbacks": [2, 3], "new-parens": 2, "no-array-constructor": 2, @@ -150,9 +148,6 @@ "no-inline-comments": 2, "no-lonely-if": 2, "no-mixed-spaces-and-tabs": 2, - "no-multiple-empty-lines": [1, { - "max": 2 - }], "no-ternary": 0, "operator-assignment": 0, "quote-props": [2, "as-needed"], diff --git a/package.json b/package.json index 230ecff..2ba57c4 100644 --- a/package.json +++ b/package.json @@ -3,20 +3,22 @@ "version": "0.0.1", "description": "", "main": "lib/connectToStores", - "dependencies": {}, + "dependencies": { + "estraverse": "^4.2.0", + "estraverse-fb": "^1.3.1" + }, "devDependencies": { "alt": "0.17.1", "babel": "5.6.14", "babel-core": "5.6.15", - "babel-eslint": "3.1.18", "babel-loader": "^5.1.4", "babelify": "6.1.2", "browserify": "^10.2.6", "chai": "^2.3.0", "coveralls": "^2.11.2", "envify": "^3.4.0", + "eslint": "^2.4.0", "es6-promise": "^2.1.1", - "eslint": "^0.24.0", "eslint-plugin-react": "2.5.2", "istanbul": "^0.3.14", "jsdom": "^3.1.2", diff --git a/src/connectToStores.js b/src/connectToStores.js index 1564155..ba83b89 100644 --- a/src/connectToStores.js +++ b/src/connectToStores.js @@ -10,7 +10,9 @@ const eachObject = (f, o) => { }) } const assign = (target, ...source) => { - eachObject((key, value) => target[key] = value, source) + eachObject((key, value) => { + target[key] = value + }, source) return target } @@ -24,6 +26,7 @@ function connectToStores(Spec, Component = Spec) { } const StoreConnection = React.createClass({ + getInitialState() { return Spec.getPropsFromStores(this.props, this.context) }, @@ -58,6 +61,14 @@ function connectToStores(Spec, Component = Spec) { } }) + // copy static methods of component + Object.getOwnPropertyNames(Spec).forEach(function (prop) { + if (prop === 'getPropsFromStores' || prop === 'getStores') return + if (typeof Spec[prop] === 'function' && !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 2ef9eaa..8731032 100644 --- a/test/connect-to-stores-test.js +++ b/test/connect-to-stores-test.js @@ -92,6 +92,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')