Question

I made unit test code with test utils by React. But encountered a problem

My environment is:

  • Rails 4
  • Jasmine 2.0.0
  • Backbone 1.1.2
describe("cNotice", function () {
    it("lol", function () {
        console.log(Notice); // present
        console.log(<Notice message="show me the message" />); // return Constructor

        var instance = <Notice message="show me the message" />;
        var component = React.addons.TestUtils.renderIntoDocument(instance);
        expect(component.getDOMNode().childNodes[0].className).toBe('notice');
    });
});

Error message is:

Error: Invariant Violation: addComponentAsRefTo(...): Only a ReactOwner can have refs. This usually means that you're trying to add a ref to a component that doesn't have an owner (that is, was not created inside of another component's render method). Try rendering this component inside of a new top-level component which will hold the ref.


UPDATE

This code is no problem:

describe("cNotice", function () {
    var Notice = null;
    beforeEach(function () { Notice = React.createClass({...}); });

    it("lol", function () {
        var instance = <Notice message="show me the message" />;
        var component = React.addons.TestUtils.renderIntoDocument(instance);
        expect(component.getDOMNode().childNodes[0].className).toBe('notice');
    });
});

But I want to import Notice component from external file.

Was it helpful?

Solution

SOLVED

i used window namespace.
imported Notice component from external file

describe("cNotice", function () {
    it("lol", function () {
        var component = React.addons.TestUtils.renderIntoDocument(window.Notice({ message: "show me the message" }));
        expect(component.getDOMNode().childNodes[0].className).toBe('notice');
    });
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top