-
-
Save oieduardorabelo/64fe0f570ff4c16ab6a9f9a19635c5a2 to your computer and use it in GitHub Desktop.
Testando com Jest: Dica #12
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 Form, { validate } from '../Form' | |
jest.mock('redux-form', () => ({ | |
Field: 'Field', | |
reduxForm: options => { | |
// envolve o componente e retorna um novo componente, exatamente como a verdade HOC faz | |
return Form => props => { | |
// chama a função "validate" para detectar erros | |
options.validate({}, props) | |
return <Form {...props} /> | |
} | |
} | |
})) | |
it('should render correctly', () => { | |
const translate = jest.fn() | |
const component = renderer.create(<Form translate={translate} />) | |
expect(component.toJSON()).toMatchSnapshot() | |
}) | |
it('should validate correctly', () => { | |
// mudando o comportamento padrão do mock, providenciando uma string como resposta | |
const translate = jest.fn(str => str) | |
validate({}, { translate }) | |
expect(translate).toHaveBeenLastCalledWith('Required') | |
validate( | |
{ username: 'much much longer than 15 characters buddy' }, | |
{ translate } | |
) | |
expect(translate).toHaveBeenLastCalledWith('Must be 15 characters or less') | |
validate({ username: 'ironman' }, { translate }) | |
expect(translate).not.toMatch({ | |
username: expect.any(String) | |
}) | |
}) |
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 { Field, reduxForm } from 'redux-form' | |
export const validate = (values, { translate }) => { | |
const errors = {} | |
if (!values.username) { | |
errors.username = translate('Required') | |
} else if (values.username.length > 15) { | |
errors.username = translate('Must be 15 characters or less') | |
} | |
return errors | |
} | |
const renderField = ({ | |
input, | |
label, | |
type, | |
meta: { touched, error, warning } | |
}) => | |
<div> | |
<label>{label}</label> | |
<div> | |
<input {...input} placeholder={label} type={type} /> | |
{touched && (error && <span>{error}</span>)} | |
</div> | |
</div> | |
const Form = props => { | |
const { handleSubmit, pristine, reset, submitting } = props | |
return ( | |
<form onSubmit={handleSubmit}> | |
<Field | |
name="username" | |
type="text" | |
component={renderField} | |
label="Username" | |
/> | |
<div> | |
<button type="submit" disabled={submitting}>Submit</button> | |
<button type="button" disabled={pristine || submitting} onClick={reset}> | |
Clear Values | |
</button> | |
</div> | |
</form> | |
) | |
} | |
export default reduxForm({ | |
form: 'example', | |
validate | |
})(Form) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment