Giá trị trong onSubmit xử lý luôn là một đối tượng trống. Làm thế nào để vượt qua một số giá trị cho nó để tôi có thể kiểm tra phụ thêm?Redux form onSubmit giá trị rỗng đối tượng trong thử nghiệm
kiểm tra:
const store = createStore(combineReducers({ form: formReducer }));
const setup = (newProps) => {
const props = {
...newProps,
};
expect.spyOn(store, 'dispatch');
const wrapper = mount(
<Provider store={store}>
<RegisterFormContainer {...props} />
</Provider>,
);
return {
wrapper,
props,
};
};
describe('RegisterFormContainer.integration',() => {
let wrapper;
it('should append form data',() => {
({ wrapper } = setup());
const values = {
userName: 'TestUser',
password: 'TestPassword',
};
expect.spyOn(FormData.prototype, 'append');
// Passing values as second argument DOESN'T work, it's just an empty object
wrapper.find('form').simulate('submit', values);
Object.keys(values).forEach((key) => {
expect(FormData.prototype.append).toHaveBeenCalledWith(key, values[key]));
});
expect(store.dispatch).toHaveBeenCalledWith(submit());
});
});
container:
const mapDispatchToProps = dispatch => ({
// values empty object
onSubmit: (values) => {
const formData = new FormData();
Object.keys(values).forEach((key) => {
formData.append(key, values[key]);
});
return dispatch(submit(formData));
},
});
export default compose(
connect(null, mapDispatchToProps),
reduxForm({
form: 'register',
fields: ['__RequestVerificationToken'],
validate: userValidation,
}),
)(RegisterForm);
Component:
const Form = ({ error, handleSubmit }) => (
<form onSubmit={handleSubmit} action="">
<Field className={styles.input} name="username" component={FormInput} placeholder="Username" />
<button type="submit">
Register
</button>
</form>
);
Bạn có thể hiển thị mệnh đề kỳ vọng thực tế bạn đã sử dụng để kiểm tra nếu 'submit' được gọi với các giá trị biểu mẫu đúng không? – jakee
@jakee Xong. Tôi sử dụng 'formdata-polyfill' trong jsdom của tôi cho FormData nếu không nó sẽ không được xác định. –