我为我的form component创建了一个HOC,它只将少量函数公开为context
变量。这是代码:
// React 15
import React, { Component } from "react";
import PropTypes from "prop-types";
export default class FormContainer extends Component {
constructor(props) {
super(props);
this.state = {
shouldValidateForm: false
};
}
getChildContext() {
return {
setFieldValue: (event, field) => {
const fieldName = field.id;
this.setState(prevState => ({
[fieldName]: {
...prevState[fieldName],
value: event.currentTarget.value
}
}));
},
addField: data => {
this.setState({
[data.id]: data
});
},
formData: this.state
};
}
/**
* React Lifecycle Method: Renders the data
*
* @return {DOM} Main container DOM.
*/
render() {
return <form action="">{this.props.children}</form>;
}
static childContextTypes = {
addField: PropTypes.func,
setFieldValue: PropTypes.func,
formData: PropTypes.object
};
}
我想在单元测试中对addField
,setFieldValue
进行单元测试。我尝试了一些尝试,但无法解决。有人可以向我指出正确的测试方法。