Skip to content

Instantly share code, notes, and snippets.

@stipsan
Last active June 16, 2017 09:49
Show Gist options
  • Save stipsan/f56b5fe2981c0a28e170d6a69a1407f6 to your computer and use it in GitHub Desktop.
Save stipsan/f56b5fe2981c0a28e170d6a69a1407f6 to your computer and use it in GitHub Desktop.
Testing with Jest: Tip #10
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')
})
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