unit-testingreactjsjasminereactjs-testutils

React/Jasmine/Karma/Phantom Unit Test: findDOMNode and renderIntoDocument not working as expected


I'm trying to write a simple unit test and can't seem to figure it out. I want to test a bootstrap modal to ensure it displays the correct contents when I pass certain object properties to it. Here's what my modal code looks like:

import React, { Component, PropTypes } from 'react';
import { Button, Modal } from 'react-bootstrap';

class ModalBox extends Component {
  render() {
    const { modalBox } = this.props;
    let content;

    if (modalBox.contentBody) {
      content = modalBox.contentBody;
    } else {
      content = (
        <span>
          <Modal.Header closeButton onHide={this.close.bind(this)}>
            <Modal.Title>{modalBox.title}</Modal.Title>
          </Modal.Header>
          <Modal.Body>
            {modalBox.message}
          </Modal.Body>
          {modalBox.isConfirm &&
            <Modal.Footer>
              <Button onClick={modalBox.onCancel} className="modal-button cancel">{modalBox.cancelText || 'Cancel'}</Button>
              <Button onClick={modalBox.onConfirm} className="modal-button confirm">{modalBox.confirmText || 'Confirm'}</Button>
            </Modal.Footer>
          }
        </span>
      );
    }

    return (
      <Modal show={typeof modalBox != 'undefined'} onHide={this.close.bind(this)} dialogClassName={modalBox.dialogClassName || ''} backdrop={modalBox.backdrop || true}>
        {content}
      </Modal>
    );
  }
}

So for a test, I want to make sure that if I pass the prop modalBox containing the contentBody field that it just returns the contentBody for the modal body. Here's an example of what I'm trying to test:

it("renders only contentBody when provided", () => {
let modalBoxObj = {
  contentBody: <div className="test-content-body">This is a test.</div>
};

let element = React.createElement(ModalBox, {modalBox: modalBoxObj});

let component = TestUtils.renderIntoDocument(element);
let modalWrapper = TestUtils.scryRenderedDOMComponentsWithClass(component, 'modal');

// modalWrapper returns an empty array, so this returns "Expected 0 to be 1"
expect(modalWrapper.length).toBe(1);

let testBody = TestUtils.scryRenderedDOMComponentsWithClass(component, 'test-content-body');


// testBody returns an empty array, so this returns "Expected 0 to be 1"
expect(testBody.length).toBe(1);

// this returns "TypeError: 'undefined' is not an object (evaluating 'testBody[0].innerHTML')"
expect(testBody[0].innerHTML).toEqual("This is a test.");
}

I've also tried doing shallow rendering with TestUtils.createRenderer and trying that approach, but had no luck with it. Based on the examples I've seen online and previous testing experience with react <0.14, I feel this test should work. I just don't know what I'm missing or misunderstanding. In the past, I did something like below and just looked at the componentNode object to find elements and such, but componentNode is returning null.

let component = TestUtils.renderIntoDocument(element);
let componentNode = findDOMNode(component);

Thanks for your help!


Solution

  • The solution ended up being to add a ref to the ModalBox component. Once added, we were able to target the node like this:

    let component = TestUtils.renderIntoDocument(<ModalBox modalBox={modalBoxObj} />);
    let componentNode = findDOMNode(component.refs.modalBox._modal);