Skip to content

Instantly share code, notes, and snippets.

@juliobguedes
Last active March 18, 2020 18:13
Show Gist options
  • Save juliobguedes/ea5ed14828ed91d13e797ed55e8ff555 to your computer and use it in GitHub Desktop.
Save juliobguedes/ea5ed14828ed91d13e797ed55e8ff555 to your computer and use it in GitHub Desktop.
React Native Formik testing
import React from 'react';
import { View } from 'react-native';
import { Input } from 'react-native-elements';
type Props = {
placeholder: string,
value: string,
onChangeText: () => void,
errors: string,
password: boolean
};
const styles = {
viewBox: {
alignItems: 'center',
justifyContent: 'center',
width: '90%',
padding: 10,
marginLeft: 10,
},
inputContainer: {
alignItems: 'center',
backgroundColor: '#FAFAFA',
borderRadius: 8,
borderWidth: 0.6,
borderColor: '#0F0F0F',
},
};
const InputField = ({
placeholder, value, errors, password, onChangeText,
}: Props) => (
<View style={styles.viewBox}>
<Input
inputContainerStyle={styles.inputContainer}
placeholder={placeholder}
value={value}
onChangeText={onChangeText}
errorMessage={errors}
secureTextEntry={password}
/>
</View>
);
export default InputField;
import React from 'react';
import { View } from 'react-native';
import { Button } from 'react-native-elements';
import { withFormik } from 'formik';
import * as yup from 'yup';
import {
NameValidator,
EmailValidator,
PasswordValidator,
ConfirmPasswordValidator,
} from '../common/validation/UserValidation';
import InputField from '../common/InputField';
type Props = {
values: {
name: string,
email: string,
password: string,
confirmPassword: string,
},
errors: {
name: string,
email: string,
confirmPassword: string,
},
setFieldValue: (string, any) => void,
handleSubmit: () => void,
}
export const SignUpInnerForm = ({
values, errors, setFieldValue, handleSubmit,
}: Props) => (
<View>
<InputField
placeholder="Nome"
value={values.name}
password={false}
errors={errors.name}
onChangeText={text => setFieldValue('name', text)}
/>
<InputField
placeholder="Email"
value={values.email}
password={false}
errors={errors.email}
onChangeText={text => setFieldValue('email', text)}
/>
<InputField
placeholder="Senha"
value={values.password}
password
onChangeText={text => setFieldValue('password', text)}
/>
<InputField
placeholder="Repita a Senha"
value={values.confirmPassword}
password
errors={errors.confirmPassword}
onChangeText={text => setFieldValue('confirmPassword', text)}
/>
<Button title="Cadastrar" onPress={handleSubmit} />
</View>
);
export default withFormik({
mapPropsToValues: (props) => {
if (props.initialValues) {
return props.initialValues;
}
return {
name: '',
email: '',
password: '',
confirmPassword: '',
};
},
validationSchema: yup.object().shape({
name: NameValidator(),
email: EmailValidator(),
password: PasswordValidator(),
confirmPassword: ConfirmPasswordValidator(yup.ref('password')),
}),
validateOnChange: false,
handleSubmit: (values, bag) => bag.props.handleSubmit(values),
})(SignUpInnerForm);
import React from 'react';
import { shallow } from 'enzyme';
import SignUpForm from '../../src/components/signup/SignUpForm';
import InputField from '../../src/components/common/InputField';
describe('SignUpForm tests', () => {
describe('should render', () => {
it('properly', () => {
const spy = jest.fn();
const tree = shallow(<SignUpForm
submitForm={spy}
/>);
tree.props().submitForm();
expect(spy).toHaveBeenCalled();
expect(tree).toMatchSnapshot();
});
it('name error with less than 3', () => {
const spy = jest.fn();
const tree = shallow(<SignUpForm
initialValues={{
email: '',
name: 'aa',
password: '',
confirmPassword: '',
}}
submitForm={spy}
/>);
tree.props().submitForm();
expect(spy).toHaveBeenCalled();
expect(tree).toMatchSnapshot();
});
it('no errors', () => {
const spy = jest.fn();
const tree = shallow(<SignUpForm
submitForm={spy}
/>);
tree.find('Formik').dive().dive().dive()
.find(InputField)
.first()
.simulate('change', {
target: {
values: {
name: 'bl',
},
},
});
tree.submitForm(); // I have tried to do this in all sorts of ways... only 'worked' as it is in line 13 and 28
expect(spy).toHaveBeenCalled();
expect(tree.instance()).toMatchSnapshot();
});
});
});
import * as yup from 'yup';
function equalTo(ref: any, msg: string) {
return yup.mixed().test({
name: 'equalTo',
exclusive: false,
message: msg,
params: {
reference: ref.path,
},
test(value: any) {
return value === this.resolve(ref);
},
});
}
yup.addMethod(yup.string, 'equalTo', equalTo);
export const NameValidator = () => yup.string()
.required('Você precisa inserir um nome')
.min(3, 'Seu nome precisa ter mais de 3 caracteres')
.max(255, 'Seu nome não pode ter mais de 255 caracteres');
export const EmailValidator = () => yup.string()
.email('Você precisa inserir um email válido')
.required('Você precisa inserir um email');
export const PasswordValidator = () => yup.string()
.required('É necessário cadastrar uma senha')
.min(6, 'Sua senha precisa ter pelo menos 6 caracteres')
.max(30, 'Sua senha não pode ter mais que 30 caracteres');
export const ConfirmPasswordValidator = password => yup.string()
.equalTo(password, 'As senhas devem ser iguais');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment