【发布时间】:2018-06-15 16:58:48
【问题描述】:
我已经开始从 redux-form 迁移到 react-final-form 以使我的包更小。我对我的表单进行了一些测试,其中之一是测试表单提交时是否调用了正确的操作。切换到 react-final-form 后,我的测试中的存储操作永远不会被调用。
当表单作为属性传递时,有没有办法测试提交功能。
我的测试:
it('submits the form', () => {
const wrapper = shallowUntilTarget(<LoginFormContainer store={store} />, 'form');
wrapper.find('form').simulate('submit');
expect(store.getActions()).toEqual(expect.arrayContaining([{ formObj: {}, type: 'PATIENT_LOGIN_REQUEST' }]));
});
shallowUntilTarget 通过容器渲染实际表单
测试组件:
class LoginForm extends React.Component<Props> {
submitForm = (values) => {
this.props.dispatch(actions.loginPatient(values));
};
render() {
return (
<Form
onSubmit={this.submitForm}
render={({ handleSubmit }) => (
<form onSubmit={handleSubmit} />
【问题讨论】:
-
快速浏览后,该代码对我来说似乎很好。它不工作?出了什么问题?
-
它不起作用。 submitForm 函数不会被调用。我认为这是因为 submitForm 是作为属性传递的。
-
我已经通过切换到
mount解决了这个问题。 -
我看不出有什么明显的问题。
this.submitForm()根本没有被调用?而在 Redux Form 中模拟提交的那种方式? -
这是因为验证器。我在示例中删除了它以使其更清晰,但这并不好。我会尽快提交答案。