Last active
March 25, 2019 12:45
-
-
Save riddla/3d8bc51a3ae2f443313099bbe215288d to your computer and use it in GitHub Desktop.
Suppress render React component conditionally
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, {PropTypes} from 'react'; | |
import {connect} from 'react-redux'; | |
export const Step = ({title, children, id, state}) => { | |
if (state && !(state.present.ui.step === Number(id))) { | |
console.log('step denied'); | |
return null; | |
} | |
return ( | |
<section> | |
<h2>{title}</h2> | |
<div className="step _grid-container__body"> | |
{children} | |
</div> | |
</section>); | |
}; | |
Step.propTypes = { | |
children: PropTypes.node, | |
state: PropTypes.object, | |
title: PropTypes.string, | |
id: PropTypes.string, | |
}; | |
const mapStateToProps = state => ( | |
{ | |
state: state | |
} | |
); | |
export default connect(mapStateToProps)(Step); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-env jest */ | |
import React from 'react'; | |
import {shallow} from 'enzyme'; | |
import {Step} from './step'; | |
const renderComponent = (props) => { | |
const wrapper = shallow( | |
<Step> | |
<p/> | |
</Step> | |
); | |
if (props && props.state) { | |
props = Object.assign(props, { | |
state: { | |
present: { | |
ui: props.state | |
} | |
} | |
}); | |
} | |
if (props) { | |
wrapper.setProps(props); | |
wrapper.update(); | |
} | |
return wrapper; | |
}; | |
describe('<Step />', () => { | |
it('should render when step is active', () => { | |
const renderedComponent = renderComponent({id: '1', state: {step: {number: 1}}}); | |
expect(renderedComponent.containsMatchingElement(<p />)).toBeTruthy(); | |
}); | |
it('should NOT render when step is inactive', () => { | |
const renderedComponent = renderComponent({id: '1', state: {step: {number: 2}}}); | |
expect(renderedComponent.containsMatchingElement(<p/>)).toBeFalsy(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment