Liondancer
Liondancer

Reputation: 16479

Find all components with type in reactjs

I am having trouble finding components by type using scryRenderedComponentsWithType

code:

describe('Layout', function() {
    it('test', function(done) {
        var Wrapper = React.createClass({
            render: function() {
                return <div className="testWrapper">Hello <span>Jim<div>hi<ul><li><div><span></span></div></li></ul></div></span></div>;
            }
        });

        var TestWrap = React.createClass({
            render() {
                return (
                    <div>
                        <p>Test this </p>
                        <Wrapper />
                    </div>
                );
            }
        })

        var renderedTree = TestUtils.renderIntoDocument(<TestWrap />);
        var renderedMyComponent = TestUtils.scryRenderedComponentsWithType(renderedTree, 'Wrapper');
        console.log(renderedMyComponent.length);
        done();
    });
});

the output of this test returns an array length of 0. I think I maybe using the function incorrectly but I am not sure where I went wrong.

Upvotes: 2

Views: 1741

Answers (1)

Taylor Allred
Taylor Allred

Reputation: 4380

TestUtils.scryRenderedComponentsWithType(renderedTree, {function})

This requires that the second argument be a function, not a string.

Hence, your variable Wrapper will work, but not the string "Wrapper".

Upvotes: 2

Related Questions