reactjs - 如何在启用异步验证的情况下测试 redux 表单
问题描述
我为我的 redux 表单中的一个字段启用了异步验证。我使用 jest 和酵素来测试表单提交。
我尝试用一个简单的已解决承诺模拟异步验证功能,但仍然无法提交表单。但是我删除了异步验证,表单可以毫无问题地提交。
...
jest.mock('../../../../../../utilities/validators');
it('should set registration info and set current step with correct values when registration form is successfully submitted', () => {
const store = createStore(
combineReducers({
form: formReducer,
}),
);
validateEmailUnique.mockImplementation(() => Promise.resolve());
const mockOnSetRegistrationInfo = jest.fn();
const mockOnSetRegistrationCurrentStep = jest.fn();
const updatedProps = {
...defaultProps,
onSetRegistrationInfo: mockOnSetRegistrationInfo,
onSetRegistrationCurrentStep: mockOnSetRegistrationCurrentStep,
};
const wrapper = mount(
<Provider store={store}>
<StepOne {...updatedProps} />
</Provider>,
);
const form = wrapper.find('form');
const businessEmailTextField = wrapper.find(
'input#business-email-text-field',
);
businessEmailTextField.simulate('change', {
target: {
value: 'business@email.com',
},
});
form.simulate('submit');
expect(mockOnSetRegistrationInfo).toHaveBeenCalled();
我希望提交表单,然后调用表单提交回调函数内部的“onSetRegistrationInfo”函数。但是,由于异步验证没有通过,所以在测试期间无法提交表单。
解决方案
expect
问题是异步验证在运行和失败时尚未完成。
从我所看到的代码来看,您似乎无法Promise
从异步验证步骤直接访问,因此您将无法直接访问await
它...
...但是如果您模拟了任何async
要立即解决的操作,那么它们应该都在Promise
微任务队列的一个周期内完成。
如果是这种情况,那么您可以将您的断言移动到类似setImmediate
orsetTimeout
并用于done
让Jest
知道测试何时完成:
it('should set registration info...', done => { // <= use done
// ...
form.simulate('submit');
setImmediate(() => {
expect(mockOnSetRegistrationInfo).toHaveBeenCalled(); // Success!
done(); // <= now call done
});
});
推荐阅读
- node.js - 如何将 npm config 用于依赖模块?
- php - 通过单击 php 设置一个值并使用该新值重新加载页面
- git - github流版本控制(它是如何工作的)
- visual-studio-code - 如何停止quokka在vs代码中观看文件?
- r - 基于 grepl 复制行并在 dplyr 中作为分级列表加入
- c++ - 在给定的时间后调用另一个无效
- python - 在熊猫数据框的字典行中创建元素计数的新列
- angular - 从 ngOnit 的函数 istead 以角度获取 http 数据
- firefox - 在 opensearch.xml 中设置关键字条目
- symfony - API 平台:使用 PUT 替换 OneToMany 关系