TestUtils gets a React element mapped to "root"
I have a React component that maps directly to "root" (document.body), I want to get this element using TestUtils, but I don't want to keep a link to this element. Is there a way to do this?
Basically, I want something like this:
React.addons.TestUtils.findRenderedComponentWithType(document.body, MyReactClass);
But that doesn't work (passing null
or undefined
as the first parameter doesn't work either). I am left to wonder if there is any "root" of the React Component Tree that I can get a reference to.
+3
source to share
1 answer
If it's for Unit test purpose, you don't need to do that, if you're testing a component MyReactClass
, just do:
const props = {//some mocked props}
const element = TestUtils.renderIntoDocument(<MyReactClass {...props}/>);
Element
contains the element.
I hope I understood your question correctly ...
+1
source to share