我正在尝试使用via和测试我的validate
功能。react-test-renderer
onChange
onBlur
这是一个简化的测试:
import renderer, { act } from 'react-test-renderer';
const byTypeAndName = (type, name) => elem =>
elem.type === type && elem.props.name === name;
const onSubmit = jest.fn();
const onValidate = jest.fn();
const instance = renderer.create(
<Formik
onSubmit={onSubmit}
initialValues={{
password: '',
password2: '',
}}
validate={onValidate}
>
<Form>
<Field
type="password"
name="password"
autoComplete="new-password"
required
/>
<ErrorMessage name="password" />
</Form>
</Formik>
).root;
await act(async () => {
const password = instance.find(byTypeAndName('input', 'password'));
password.props.onChange('123');
});
expect(onValidate).toBeCalled(); // fails