【问题标题】:How to get Enzyme type() of null instead of function?如何获得 null 而不是函数的 Enzyme type()?
【发布时间】:2018-06-18 19:56:06
【问题描述】:

我是 unit-testing 的新手,遇到了问题。我无法让 Enzyme 为具有 if 语句 !item => null 的元素返回 null。 如果没有给出'item prop',如何检查元素是否为空?

 it("should not render without `props.item`", () => {
    wrapper.setProps({
      item: null,
    })

    console.log(wrapper.find('ThemeTile').childAt(0).name()) // === 'Link'
    expect(wrapper.find('ThemeTile').childAt(0).type()).toEqual(null);

    // returns expect(received).toEqual(expected)
    // Expected value to equal:
    //    null
    //Received:
    //   [Function Link]
  })

这就是 ThemeTile 的相关渲染方法。

render() {
    const { classes, item, href } = this.props;

    if(!item) {
      return null;
    }

    const image                   = buildImageUrl(item, 'square', 300, 300);

    return (
      <Link to={href} className={classes.root}>
        <div className={classes.imageContainer}>
          <div className={classes.image} style={{backgroundImage: `url(${image})`}} />
        </div>
        <Typography className={classes.title}>
          {this.props.item.title}
        </Typography>
      </Link>
    );
  }

【问题讨论】:

    标签: javascript reactjs unit-testing jestjs enzyme


    【解决方案1】:

    有几种可能性可以检查酶包装是否不呈现任何内容。以下对我来说看起来很干净:

    expect(wrapper.getElement()).toBe(null);
    

    请参阅this thread 了解有关此问题的讨论。

    【讨论】:

      猜你喜欢
      • 2014-04-15
      • 1970-01-01
      • 1970-01-01
      • 2019-09-26
      • 2017-06-25
      • 2019-10-16
      • 1970-01-01
      • 2017-06-04
      • 2022-01-16
      相关资源
      最近更新 更多