Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use React Test Utilities with Jasmine

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.

like image 913
hiphapis Avatar asked Nov 01 '22 04:11

hiphapis


1 Answers

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');
    });
});
like image 65
hiphapis Avatar answered Nov 07 '22 21:11

hiphapis