Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: Invariant Violation: findAllInRenderedTree(...): instance must be a composite component #4692

Closed
gajus opened this issue Aug 23, 2015 · 15 comments

Comments

@gajus
Copy link
Contributor

gajus commented Aug 23, 2015

I am writing a test case using Mocha and JSDom.

import React from 'react';
import TestUtils from 'react-addons-test-utils';

describe('ReactOutsideEvent', () => {
    it('captures mousedown event', () => {
        let detachedCoponent;

        detachedCoponent = TestUtils.renderIntoDocument(<div>
            <div className='foo'></div>
        </div>);

        TestUtils.scryRenderedDOMComponentsWithClass(detachedCoponent, 'foo');
    });
});

I'd like to obtain a reference to the generated DOMNode with className "foo". scryRenderedDOMComponentsWithClass produces:

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

I have also tried scryRenderedDOMComponentsWithTag and got the same error.

Using 0.14.0-beta3.

@sophiebits
Copy link
Collaborator

Yes, this is a breaking change and you'll need to change your tests to be different, sorry. See 4070c4c. detachedCoponent is actually a DOM node now so the test utils can't look into it to find the React component tree any more. This rarely comes up because you'll almost always be testing your own components (it's our job to test that the DOM components work, not yours :)).

@gajus
Copy link
Contributor Author

gajus commented Aug 23, 2015

Awesome. Thank you.

@benhughes
Copy link

@spicyj
Just discovering numerous versions of this error after updating our codebase.

So how would we now test that say after clicking something the class of a div has been updated to hide inside one of our components? Is that something we shouldn't be testing?

@sophiebits
Copy link
Collaborator

@benhughes Can you paste a sample test so I can see what you mean?

@sheepsteak
Copy link

@benhughes just come across this myself. I've started accessing them as though they were DOM nodes after seeing this SO answer. So for your example div I would now do (Jasmine):

expect(myDiv.className).toContain('hide');

@sophiebits
Copy link
Collaborator

@sheepsteak That sounds like a different warning, but I'm glad you got it to work.

@sheepsteak
Copy link

You're right, I was searching for two problems at once! My answer above relates to this warning:

Warning: ReactDOMComponent: Do not access .props of a DOM node; instead, recreate the props asrenderdid originally or read the DOM properties/attributes directly from this node (e.g., this.refs.box.className).

@iangreenleaf
Copy link

This has caused trouble on some of my tests (and I can see people who have mentioned this ticket with the same problem). I was using nested calls to the selector functions to find the components I was interested in:

    const sections = TestUtils.scryRenderedDOMComponentsWithTag(myPage, 'section');
    expect(sections.length).to.eq(3)
    const headers = sections.map(section => {
      return TestUtils.findRenderedDOMComponentWithTag(section, 'h1').props.children;
    });

It looks like now this is no longer possible, and I don't really see an alternative in the test utils that's functionally equivalent.

@vysinsky
Copy link

vysinsky commented Dec 8, 2015

Hi @iangreenleaf, I've just found a solution which works for me:

Previous code which was failing on this error:

var myComponent = TestUtils.renderIntoDocument(
    <div>
        <MyComponent page={page1} dispatch={dispatch}>Foo</MyComponent>
        <MyComponent page={page2} dispatch={dispatch}>Foo</MyComponent>
    </div>
);

assert.equal(TestUtils.scryRenderedDOMComponentsWithTag(myComponent, 'a').length, 2);

(test checks that component is 'rerendered' after changing its state)

Code which fixed that:

var Wrapper = React.createClass({
    render: function() {
        return (
            <div>{this.props.children}</div>
        );
    }
});

var myComponent = TestUtils.renderIntoDocument(
    <Wrapper>
        <MyComponent page={page1} dispatch={dispatch}>Foo</MyComponent>
        <MyComponent page={page2} dispatch={dispatch}>Foo</MyComponent>
    </Wrapper>
);

assert.equal(TestUtils.scryRenderedDOMComponentsWithTag(myComponent, 'a').length, 2);

As Wrapper is normal React component it does not fail anymore. Hope it will help.

@moroshko
Copy link

@iangreenleaf Did you find a solution to the nested lookup issue? I'm stuck with exactly that.

@spicyj Is there any way to do a nested lookup? More specifically, as @iangreenleaf mentioned, I'd like to do:

var elements = scryRendered...
var nestedElements = scryRendered...(elements[0], ...)

But, TestUtils complains:

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

@idoo
Copy link

idoo commented Dec 20, 2015

I have same issue, with 0.14.3
Error: Invariant Violation: findAllInRenderedTree(...): instance must be a composite component
please give me a worked solution to find element with 0.14.3

@iangreenleaf
Copy link

@moroshko Nope 😿 Our solution is to find a different tool, and abandon TestUtils for all but the most low-level unit tests.

@moroshko
Copy link

@iangreenleaf What tool did you end up with?

@iangreenleaf
Copy link

@moroshko Haven't settled on anything yet, but we'll probably be looking for something more integration-oriented that works with the real DOM and XPath/CSS-style selectors. For the moment we're just sticking with Capybara (since we are already running Ruby). The limited scope of TestUtils is fine for testing individual components, but is unwieldy when actually simulating user interactions.

@sophiebits
Copy link
Collaborator

I have not tried https://github.com/airbnb/enzyme but it looks promising.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants