2

I am using Jest to test a ReactJS app. How can one test if the view controller has the necessary child components present.

For example, here is an example of the view controllers' render function:

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

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

For HTML elements, something like will do:

button = TestUtils.findRenderedDOMComponentWithTag AppElement, 'button'

How can I do the same for the Form or Table components ?

Thank you!

medowlock
  • 1,469
  • 1
  • 19
  • 25

1 Answers1

3

Doh ... so the answer is findRenderedComponentWithType.

First, I needed the component class:

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

Then I passed this as argument, like:

form = TestUtils.findRenderedComponentWithType(AppElement, Form);

where, of course

AppElement = TestUtils.renderIntoDocument(<App />)
medowlock
  • 1,469
  • 1
  • 19
  • 25