TestUtils获取在" root"中呈现的React元素。

时间:2015-06-21 22:44:11

标签: javascript reactjs

我有一个直接在" root"上呈现的React组件。 (document.body),我想使用TestUtils获取此元素,但我不希望保留对此元素的引用。有没有办法做到这一点?

基本上我想要这样的东西:

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

但这不起作用(传递nullundefined,因为第一个参数也不起作用)。我想知道是否有任何" root"反应组件树,我可以获得参考。

1 个答案:

答案 0 :(得分:1)

如果是用于单元测试目的,则不需要这样做,如果您正在测试组件MyReactClass,只需执行以下操作:

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

元素将包含元素。

希望我正确理解你的问题......

相关问题