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

added copying of static methods to resulting storeConnection component #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
13 changes: 4 additions & 9 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -118,19 +117,18 @@
"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"],
"semi-spacing": [2, {
"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,
Expand All @@ -142,17 +140,14 @@
"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,
"no-else-return": 0,
"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"],
Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
13 changes: 12 additions & 1 deletion src/connectToStores.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -24,6 +26,7 @@ function connectToStores(Spec, Component = Spec) {
}

const StoreConnection = React.createClass({

getInitialState() {
return Spec.getPropsFromStores(this.props, this.context)
},
Expand Down Expand Up @@ -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
}

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 @@ -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')

Expand Down