I am trying to write some tests for a React Native component built using Formik. It's a simple form asking for username and password, and I want to use a validation schema built using Yup.
When I use the emulator and test the form manually, the form behaves as expected, error messages show up only when the input values are invalid.
However, when I try to write some automated tests with @testing-library/react-native, the behavior is not what I am expecting. The error messages show up in tests even if the provided values are valid. Below are the code:
// App.test.js
import React from 'react';
import { render, act, fireEvent } from '@testing-library/react-native';
import App from '../App';
it('does not show error messages when input values are valid', async () => {
const {
findByPlaceholderText,
getByPlaceholderText,
getByText,
queryAllByText,
} = render(<App />);
const usernameInput = await findByPlaceholderText('Username');
const passwordInput = getByPlaceholderText('Password');
const submitButton = getByText('Submit');
await act(async () => {
fireEvent.changeText(usernameInput, 'testUser');
fireEvent.changeText(passwordInput, 'password');
fireEvent.press(submitButton);
});
expect(queryAllByText('This field is required')).toHaveLength(0);
});
// App.js
import React from 'react';
import { TextInput, Button, Text, View } from 'react-native';
import { Formik } from 'formik';
import * as Yup from 'yup';
const Schema = Yup.object().shape({
username: Yup.string().required('This field is required'),
password: Yup.string().required('This field is required'),
});
export default function App() {
return (
<View>
<Formik
initialValues={{ username: '', password: '' }}
validationSchema={Schema}
onSubmit={(values) => console.log(values)}>
{({
handleChange,
handleBlur,
handleSubmit,
values,
errors,
touched,
validateForm,
}) => {
return (
<>
<View>
<TextInput
onChangeText={handleChange('username')}
onBlur={handleBlur('username')}
value={values.username}
placeholder="Username"
/>
{errors.username && touched.username && (
<Text>{errors.username}</Text>
)}
</View>
<View>
<TextInput
onChangeText={handleChange('password')}
onBlur={handleBlur('password')}
value={values.password}
placeholder="Password"
/>
{errors.password && touched.password && (
<Text>{errors.password}</Text>
)}
</View>
<View>
<Button
onPress={handleSubmit}
// If I explicitly call validateForm(), the test will pass
// onPress={async () => {
// await validateForm();
// handleSubmit();
// }}
title="Submit"
/>
</View>
</>
);
}}
</Formik>
</View>
);
}
I am not sure whether I am writing the test correctly. I think Formik will automatically validate the form when the handleSubmit function is called.
Within the App.js, if I explicitly call the validateForm, the test will pass. However, it's not feeling right to change the implementation of the onPress handler just to cater for the test. Maybe I am missing some fundamental concepts around this issue. Any insights would be helpful, thank you.
Package versions:
"@testing-library/react-native": "^7.1.0",
"formik": "^2.2.6",
"react": "16.13.1",
"react-native": "0.63.4",
"yup": "^0.32.8"