Last active
June 16, 2017 09:49
-
-
Save stipsan/f56b5fe2981c0a28e170d6a69a1407f6 to your computer and use it in GitHub Desktop.
Testing with Jest: Tip #10
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 renderer from 'react-test-renderer' | |
| import Input from '../Input' | |
| it('should render correctly', () => { | |
| const component = renderer.create(<Input />) | |
| expect(component.toJSON()).toMatchSnapshot() | |
| // getInstance is returning the `this` object you have in your component | |
| // meaning anything accessible on `this` inside your component | |
| // can be accessed on getInstance, including props! | |
| const instance = component.getInstance() | |
| expect(instance.state).toMatchSnapshot('initial state') | |
| instance.handleChange({ target: { value: '[email protected]' } }) | |
| expect(instance.state).toMatchSnapshot('updated state') | |
| }) |
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 { Component } from 'react' | |
| export default class Input extends Component { | |
| state = { value: '' } | |
| handleChange = event => this.setState({ value: event.target.value }) | |
| render() { | |
| return ( | |
| <div> | |
| <label>Email</label> | |
| <input | |
| type="email" | |
| onChange={this.handleChange} | |
| value={this.state.value} | |
| /> | |
| </div> | |
| ) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment