如何测试提供上下文变量的HOC组件

时间:2018-09-25 03:37:29

标签: javascript reactjs enzyme

我为我的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
  };
}

我想在单元测试中对addFieldsetFieldValue进行单元测试。我尝试了一些尝试,但无法解决。有人可以向我指出正确的测试方法。

0 个答案:

没有答案