【问题标题】:How to check if component has child component如何检查组件是否有子组件
【发布时间】:2015-04-18 01:01:38
【问题描述】:

我正在使用 Jest 测试一个 ReactJS 应用程序。如何测试视图控制器是否存在必要的子组件。

例如,这里是视图控制器的渲染函数示例:

// App.js
render: function() {
  var table;
  if (this.state.showResults) {
    table = <Table {...this.state} />
  }

  return (
    <div>
      <Form />
      {table}
  )
}

对于 HTML 元素,类似的东西会做:

button = TestUtils.findRenderedDOMComponentWithTag AppElement, 'button'

我怎样才能对表单或表格组件做同样的事情?

谢谢!

【问题讨论】:

    标签: reactjs jestjs reactjs-testutils


    【解决方案1】:

    Doh ...所以答案是 findRenderedComponentWithType。

    首先,我需要组件类:

    Form = require('Form.react.js');
    

    然后我将其作为参数传递,例如:

    form = TestUtils.findRenderedComponentWithType(AppElement, Form);
    

    当然是在哪里

    AppElement = TestUtils.renderIntoDocument(<App />)
    

    【讨论】:

      猜你喜欢
      • 2017-10-03
      • 2018-07-26
      • 1970-01-01
      • 1970-01-01
      • 2018-12-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-04
      • 2019-12-28
      相关资源
      最近更新 更多