Error: Invariant Violation: findAllInRenderedTree(...): instance must be a composite component
Asked Answered
G

2

11

While writing the test case in JEST for React file I am getting this error. Following is my sample code:

search_basr_test.js

jest.autoMockOff();
global.React = require('react/addons');
jest.setMock('../stores/browser_store.jsx');
beforeEach(function() {
    var search_bar = require('../components/search_bar.jsx');
});
var TestUtils = React.addons.TestUtils;

describe("Test", function() {
    it("should render Test", function() {
            var test = TestUtils.renderIntoDocument(<search_bar/>);
            expect(test).toBeDefined();
    });
    it("renders a list in a box with proper CSS classes", function() {
            var test = TestUtils.renderIntoDocument(<search_bar/>);
            let box = TestUtils.findRenderedDOMComponentWithTag(test, "div");
            expect(box.className).toEqual("sidebar__collapse");
    });
});

search_bar.jsx

return (
        <div>
            <div
                className='sidebar__collapse'
                onClick={this.handleClose}
            >
                <span className='fa fa-angle-left'></span>
            </div>
            <span
                className='search__clear'
                onClick={this.clearFocus}
            >
                {'Cancel'}
            </span>
}

Anyone out there to help me out from this??

Goren answered 23/12, 2015 at 10:38 Comment(1)
Same problem, did you fix it?Pyro
S
5

A composite component is a component which contains React Component (not div, span, ...) The method 'findRenderedDOMComponentWithTag' takes in parameter a composite component.

Try to parse the component directly in your case (jquery, js, ...) because it is not a composite one

Stretcherbearer answered 30/12, 2015 at 10:11 Comment(0)
S
5

This is late, but I just ran into this, and I haven't found a great answer for it.

My solution was to make a wrapper component in the test file

import { Component } from "react";
class Wrapper extends Component {
  render() {
    return <YourComponent {...this.props} />
  }
}

and instead of calling

TestUtils.renderIntoDocument(
    <YourComponent />
);

call

TestUtils.renderIntoDocument(
    <Wrapper />
);

Doing this ensures that your component is a composite component and isn't a stateless function.

Hope this helps anyone else in the future!

Stig answered 24/10, 2016 at 3:16 Comment(1)
So much googling, and I finally arrived at the solution. Thank you!!Slapstick

© 2022 - 2024 — McMap. All rights reserved.