I made unit test code with test utils by React. But encountered a problem
My environment is:
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.
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');
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With