【问题标题】:How to test that a sub component doesn't exist with React+enzyme如何使用 React+enzyme 测试子组件不存在
【发布时间】:2017-01-13 06:09:17
【问题描述】:

假设我有一个组件,它有一个控制按钮是否显示的道具。我正在添加以下测试以确保始终观察到道具。

import React from 'react';
import { expect } from 'chai';
import { mount } from 'enzyme';
import { MyComponent } from '..';

describe( 'MyComponent', () => {
    it( 'should render as expected', () => {
    const wrapper = mount( <MyComponent showButton={ false } /> );
    expect( wrapper.find( '.button' ) ).to.have.length( 0 );
} );

我的问题是:有没有更好的方法来测试组件中的某些东西不存在

我正在寻找更详细的内容。还有像.to.not.exist这样的链吗?

【问题讨论】:

    标签: reactjs chai enzyme chai-enzyme


    【解决方案1】:

    如果您使用 chaiEnzyme (https://github.com/producthunt/chai-enzyme),它将为您提供一个 .to.not.be.present() 或 .to.not.exist (https://github.com/producthunt/chai-enzyme#present) 断言,您可以使用它来帮助清理这些各种断言。

    【讨论】:

      猜你喜欢
      • 2017-01-01
      • 2017-05-03
      • 2017-04-13
      • 2017-02-12
      • 2018-08-20
      • 2018-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多