Merge pull request #3565 from owenconti/ft/map-state-to-props-for-components
`mapStateToProps` for container components
This commit is contained in:
@@ -20,8 +20,11 @@ const RootWrapper = (reduxStore, ComponentToWrap) => class extends Component {
|
||||
}
|
||||
|
||||
const makeContainer = (getSystem, component, reduxStore) => {
|
||||
const mapStateToProps = component.prototype.constructor.mapStateToProps || function(state) {
|
||||
return {state}
|
||||
}
|
||||
let wrappedWithSystem = SystemWrapper(getSystem, component, reduxStore)
|
||||
let connected = connect(state => ({state}))(wrappedWithSystem)
|
||||
let connected = connect( mapStateToProps )(wrappedWithSystem)
|
||||
if(reduxStore)
|
||||
return RootWrapper(reduxStore, connected)
|
||||
return connected
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
/* eslint-env mocha */
|
||||
import React, { PureComponent } from "react"
|
||||
import expect from "expect"
|
||||
import System from "core/system"
|
||||
import { fromJS } from "immutable"
|
||||
import { render } from "enzyme"
|
||||
import ViewPlugin from "core/plugins/view/index.js"
|
||||
import { connect, Provider } from "react-redux"
|
||||
|
||||
describe("bound system", function(){
|
||||
|
||||
@@ -444,4 +448,66 @@ describe("bound system", function(){
|
||||
|
||||
})
|
||||
|
||||
describe("getComponent", function() {
|
||||
it("returns a component from the system", function() {
|
||||
const system = new System({
|
||||
plugins: [
|
||||
ViewPlugin,
|
||||
{
|
||||
components: {
|
||||
test: ({ name }) => <div>{name} component</div>
|
||||
}
|
||||
}
|
||||
]
|
||||
})
|
||||
|
||||
// When
|
||||
var Component = system.getSystem().getComponent("test")
|
||||
const renderedComponent = render(<Component name="Test" />)
|
||||
expect(renderedComponent.text()).toEqual("Test component")
|
||||
})
|
||||
|
||||
it("allows container components to provide their own `mapStateToProps` function", function() {
|
||||
// Given
|
||||
class ContainerComponent extends PureComponent {
|
||||
static mapStateToProps(nextState, props) {
|
||||
return {
|
||||
"abc": "This came from mapStateToProps"
|
||||
}
|
||||
}
|
||||
|
||||
static defaultProps = {
|
||||
"abc" : ""
|
||||
}
|
||||
|
||||
render() {
|
||||
return (
|
||||
<div>{ this.props.abc }</div>
|
||||
)
|
||||
}
|
||||
}
|
||||
const system = new System({
|
||||
plugins: [
|
||||
ViewPlugin,
|
||||
{
|
||||
components: {
|
||||
ContainerComponent
|
||||
}
|
||||
}
|
||||
]
|
||||
})
|
||||
|
||||
// When
|
||||
var Component = system.getSystem().getComponent("ContainerComponent", true)
|
||||
const renderedComponent = render(
|
||||
<Provider store={system.getStore()}>
|
||||
<Component />
|
||||
</Provider>
|
||||
)
|
||||
|
||||
// Then
|
||||
expect(renderedComponent.text()).toEqual("This came from mapStateToProps")
|
||||
})
|
||||
})
|
||||
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user