Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 544
Hoffmann Avatar asked Sep 13 '26 15:09

Hoffmann


1 Answers

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...

like image 156
Sébastien Avatar answered Sep 16 '26 05:09

Sébastien