Skip to content

Instantly share code, notes, and snippets.

@oieduardorabelo
Forked from stipsan/Input-test.jsx
Last active June 16, 2017 09:50
Show Gist options
  • Save oieduardorabelo/1e00512170837e69454e598735fe7121 to your computer and use it in GitHub Desktop.
Save oieduardorabelo/1e00512170837e69454e598735fe7121 to your computer and use it in GitHub Desktop.
Testando com Jest: Dica #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 está retornando o objeto `this` que você tem no seu componente
// significando que todos os métodos que você tem no seu componente, podem ser
// acesso aqui, incluindo 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