【问题标题】:enzyme is not recognizing lodash.flowright酶无法识别 lodash.flowright
【发布时间】:2020-07-23 02:06:25
【问题描述】:

我试图测试一个在另一个类中呈现 D3 元素的 React 组件,下面是组件代码。

import React, {Component} from 'react';
import {graphql} from "react-apollo";
import * as compose from 'lodash.flowright';
import MostConvD3bar from './MostConvD3bar'
import MostConvProdQuery from "../../Query/MostConvProdQuery";
import style from "./MostConvProds.css"

class MostConvProds extends Component {

componentDidMount(){
    if (!this.props.loading)
        new MostConvD3bar(this.refs.mostConChart, this.props.products)
}

componentWillReceiveProps(nextProps){
    if (!nextProps.loading)
        new MostConvD3bar(this.refs.mostConChart, nextProps.products)
}

render() {
    return (
        <div className={style.container} ref="mostConChart"> </div>
          )
      }
 }


const mostConvQuery = {
    name: "mostConvQuery",
    props({ mostConvQuery: { error, loading, mostConverted } }) {
        return {
            loading,
            error,
            products: mostConverted
        }
      }
    }

export default compose(
graphql(MostConvProdQuery, mostConvQuery))(MostConvProds);

如您所见,我已将 lodash.flowright 导入为 compose,但出现如下错误。

TypeError: compose is not a function

任何帮助将不胜感激。

【问题讨论】:

  • 不是flowRight - 大写R,而不是flowright?另外,不应该是带有正斜杠的lodash/flowRight,而不是点吗?
  • 好吧,代码运行正常,问题出在测试上。
  • 如果您遇到错误,则表示代码运行不正常。如果您没有以正确的方式导入 flowRight,则可能会导致您看到的错误。
  • 我采纳了您的建议并将其更改为 'lodash/flowRight' ,我再次遇到相同的错误,但代码运行时好像没有任何变化。

标签: reactjs jestjs lodash enzyme babel-jest


【解决方案1】:

替换:

import * as compose from 'lodash.flowright';

与:

import { flowRight as compose } from 'lodash';

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-11
    • 1970-01-01
    • 1970-01-01
    • 2018-06-18
    • 2013-04-19
    • 2019-01-28
    • 2017-08-10
    • 2019-11-01
    相关资源
    最近更新 更多