反应组件儿童测试

时间:2016-01-29 18:27:57

标签: reactjs integration-testing

我正在尝试测试深层嵌套组件的功能,但我很难为Simulate“选择”它。

我一直在解决这个问题的方法如下:

const component = renderIntoDocument(TestParent);
const dropdown = findRenderedComponentWithType(component, TimespanDropdown);
const buttons = scryRenderedDOMComponentsWithTag(dropdown, 'button');
Simulate.click(buttons[0]);

当我将下拉列表传递给srcyRenderedDOMComponentsWithTag时,这一点就崩溃了。我可以在我的按钮上添加一个类,也许可以使用scryRenderedDOMComponentsWithClass,但我宁愿看看是否有办法以某种方式“链接”这些util查找调用。

1 个答案:

答案 0 :(得分:0)

您可以简化此系列调用的一种方法是在根组件上使用ReactDOM.findDOMNode,然后使用querySelector直接转到您关注的元素:

const component = ReactDOM.findDOMNode(renderIntoDocument(TestParent));
const buttons = component.querySelector('button');
Simulate.click(buttons[0]);
相关问题