React-Hook-form' s formstate.Errors在开玩笑测试中的怪异行为
我想进行验证。但是,当我在开玩笑中测试它时,错误消息将不会在第一个测试子句中的第一个Onblur中显示。但是,它在第二个测试条款中显示在第二个Onblur中。
在这里,我从第一个和第二子句中显示DOM,我们可以看到第二个Onblur之后的第二子句中显示了错误消息。
如何在第一个测试子句中的第一个Onblur中显示错误消息?
React文件
import React from 'react';
import { useForm } from 'react-hook-form';
import { string, object } from 'yup';
import { yupResolver } from '@hookform/resolvers/yup';
interface TestingProps {
fullname: string;
}
const TestingPage: React.FC = () => {
const schema = object().shape({
fullname: string().required('fullname is required!')
});
const {
register,
handleSubmit,
formState: { errors }
} = useForm<TestingProps>({
resolver: yupResolver(schema),
mode: 'onBlur'
});
const submit = () => {};
return (
<div>
<form onSubmit={handleSubmit(submit)}>
<input className="testing" {...register('fullname')} />
</form>
{errors?.fullname && <p>{errors.fullname.message}</p>}
<input type="submit" />
</div>
);
};
export default TestingPage;
开玩笑测试文件
import Enzyme, { mount } from 'enzyme';
import Adapter from '@wojtekmaj/enzyme-adapter-react-17';
import TestingPage from './TermsAndConditions';
Enzyme.configure({ adapter: new Adapter() });
describe('Testing page test', () => {
let wrapper: any;
test('first attempt to show error message onblur', () => {
wrapper = mount(<TestingPage />);
wrapper.find('.testing').simulate('focus');
wrapper.find('.testing').simulate('blur');
console.log(wrapper.debug());
});
test('second attempt to show error message onblur', () => {
wrapper.find('.testing').simulate('blur');
console.log(wrapper.debug());
});
});
I would like to do validation onBlur. But when I am testing it in jest, the error message will not show up in first onblur in first test clause. However, it shows up in second onblur in second test clause.
Here I show the DOM from first and second clause and we can see the error message shows up in 2nd clause after second onblur.
How to make the error message showing up in the first onblur in first test clause?
React File
import React from 'react';
import { useForm } from 'react-hook-form';
import { string, object } from 'yup';
import { yupResolver } from '@hookform/resolvers/yup';
interface TestingProps {
fullname: string;
}
const TestingPage: React.FC = () => {
const schema = object().shape({
fullname: string().required('fullname is required!')
});
const {
register,
handleSubmit,
formState: { errors }
} = useForm<TestingProps>({
resolver: yupResolver(schema),
mode: 'onBlur'
});
const submit = () => {};
return (
<div>
<form onSubmit={handleSubmit(submit)}>
<input className="testing" {...register('fullname')} />
</form>
{errors?.fullname && <p>{errors.fullname.message}</p>}
<input type="submit" />
</div>
);
};
export default TestingPage;
Jest Test File
import Enzyme, { mount } from 'enzyme';
import Adapter from '@wojtekmaj/enzyme-adapter-react-17';
import TestingPage from './TermsAndConditions';
Enzyme.configure({ adapter: new Adapter() });
describe('Testing page test', () => {
let wrapper: any;
test('first attempt to show error message onblur', () => {
wrapper = mount(<TestingPage />);
wrapper.find('.testing').simulate('focus');
wrapper.find('.testing').simulate('blur');
console.log(wrapper.debug());
});
test('second attempt to show error message onblur', () => {
wrapper.find('.testing').simulate('blur');
console.log(wrapper.debug());
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只需要在测试断言之前等待,因为React-Hook-form-form验证和提交是异步的。使用齐平宣传,例如
You just need to
await
before test assertion, because react-hook-form validation and submit are async. Use flush-promises e.g.