Skip to content

Instantly share code, notes, and snippets.

@DonnieWest
Created November 30, 2016 05:31
Show Gist options
  • Save DonnieWest/10a50587c6944759f2f7caaceb6d8826 to your computer and use it in GitHub Desktop.
Save DonnieWest/10a50587c6944759f2f7caaceb6d8826 to your computer and use it in GitHub Desktop.
enzyme-stolen-sample-code
// __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');
// 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