|
import _ from 'lodash'; |
|
import React from 'react'; |
|
import TestUtils from 'react-addons-test-utils'; |
|
import createStoreStub from '__tests__/createStoreStub'; |
|
|
|
// Tests a smart component that is bound to stores and actions directly. |
|
// Tests if the props are passed correctly to the dumb component. |
|
// Expects |
|
// - a smart component, |
|
// - an object with state properties that are expected on the props, |
|
// - an array of action creator names. |
|
// Returns the props of the rendered dumb component. |
|
// |
|
// Usage: |
|
// |
|
// Implementation: |
|
// class DumbComponent extends React.Component { |
|
// render() { |
|
// return <button onClick={this.props.login}> |
|
// {this.props.i18n.loginButtonLabel} |
|
// </button>; |
|
// } |
|
// } |
|
// DumbComponent.propTypes = { |
|
// i18n: PropTypes.object.isRequired, |
|
// login: PropTypes.func.isRequired, |
|
// }; |
|
// const SmartComponent = connect( |
|
// // mapStateToProps |
|
// (state) => ({ i18n: state.i18n }), |
|
// // mapDispatchToProps |
|
// { login: userActions.login } |
|
// )(DumbComponent); |
|
// |
|
// Test: |
|
// const state = { i18n: { loginButtonLabel: 'Login' } }; |
|
// const actionCreatorNames = [ 'login' ]; |
|
// testSmartComponent(SmartComponent, state, actionCreatorNames); |
|
export default (SmartComponent, state, actionCreatorNames) => { |
|
// Create a store, pass it to the SmartComponent as context |
|
const store = createStoreStub(state); |
|
const context = { store }; |
|
|
|
const shallowRenderer = TestUtils.createRenderer(); |
|
shallowRenderer.render(<SmartComponent/>, context); |
|
const tree = shallowRenderer.getRenderOutput(); |
|
|
|
const { props } = tree; |
|
// mapStateToProps test |
|
_.forEach(state, (value, key) => { |
|
expect(props[key]).toBe(value); |
|
}); |
|
|
|
// mapDispatchToProps test |
|
_.forEach(actionCreatorNames, (actionCreatorName) => { |
|
// Call the bound action creator. This is the only way to check that the |
|
// the wrapped value is a function. This calls the original action creator, |
|
// but since we’re using redux-thunk, it only returns a function. |
|
props[actionCreatorName](); |
|
}); |
|
|
|
return props; |
|
}; |