Created
November 30, 2016 05:31
-
-
Save DonnieWest/10a50587c6944759f2f7caaceb6d8826 to your computer and use it in GitHub Desktop.
enzyme-stolen-sample-code
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
// __tests__/CheckboxWithLabel-test.js | |
import React from 'react'; | |
import {shallow} from 'enzyme'; | |
import CheckboxWithLabel from '../CheckboxWithLabel'; | |
it('CheckboxWithLabel changes the text after click', () => { | |
// Render a checkbox with label in the document | |
const checkbox = shallow( | |
<CheckboxWithLabel labelOn="On" labelOff="Off" /> | |
); | |
expect(checkbox.text()).toEqual('Off'); | |
checkbox.find('input').simulate('change'); | |
expect(checkbox.text()).toEqual('On'); |
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
// CheckboxWithLabel.js | |
import React from 'react'; | |
export default class CheckboxWithLabel extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = {isChecked: false}; | |
} | |
onChange = () => { | |
this.setState({isChecked: !this.state.isChecked}); | |
} | |
render() { | |
return ( | |
<label> | |
<input | |
type="checkbox" | |
checked={this.state.isChecked} | |
onChange={this.onChange} | |
/> | |
{this.state.isChecked ? this.props.labelOn : this.props.labelOff} | |
</label> | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment