Hoffmann
Hoffmann

Reputation: 14719

TestUtils get React element rendered in the "root"

I have a React component rendered directly on the "root" (document.body), I want to get this element using TestUtils but I do not want to keep a reference to this element. Is there any way to do that?

Basically I want something like this:

React.addons.TestUtils.findRenderedComponentWithType(document.body, MyReactClass);

But this does not work (passing null or undefined as the first parameter does not work either). I am left wondering if there is any "root" React Component Tree that I can get a reference to.

Upvotes: 3

Views: 307

Answers (1)

Sébastien
Sébastien

Reputation: 341

If it is for Unit test purpose, you don't need to do that, if you are testing the component MyReactClass, just do :

const props = {//some mocked props}
const element = TestUtils.renderIntoDocument(<MyReactClass {...props}/>);

element will contains the element.

Hope I correctly understood your question...

Upvotes: 1

Related Questions