unit-testing - 单元测试 React Bootstrap 模态对话框

标签 unit-testing reactjs jasmine react-bootstrap

我正在尝试使用 Jasmine 对 React Bootstrap 模态对话框进行单元测试。但它没有按预期工作。

这是使用最新版本的 React、React Bootstrap、Jasmine 的 jsfiddle 链接。:http://jsfiddle.net/30qmcLyf/3/

失败的测试:

第 27-28 行

// This test fails. Find DOM Node.
var instanceDomNode = ReactDOM.findDOMNode(instance);
expect(instanceDomNode).not.toBe(null);

第 39-40 行

//This test fails. Find modal header.
var headerComponents = TestUtils.scryRenderedComponentsWithType(component, ReactBootstrap.Modal.Header);
expect(headerComponents.length).not.toBe(0);

还有第 35-36 行有什么问题。如果我取消注释行,我会在注释中看到错误。

// Error: Did not find exactly one match for componentType:function ModalHeader()...
//var headerComponent = TestUtils.findRenderedComponentWithType(component, ReactBootstrap.Modal.Header);
//expect(headerComponent).not.toBe(null);

根据测试实用程序的最新官方文档 (link),您应该将 ReactComponent 作为第一个参数传递。

谁能告诉我哪里出了问题?

最佳答案

查看 react-bootstrap 团队如何为此编写测试。模态被渲染到不同的子树中,这就是它如何渲染到文档主体而不是直接作为其父级的子级。换句话说,您的搜索失败是因为该组件不在该组件树中。

您可以在模式上使用 refs 或直接在文档中查找 DOM 节点。

关于unit-testing - 单元测试 React Bootstrap 模态对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33655466/

相关文章:

c++ - gtest - 测试模板类

javascript - React Hot Module Reloader 阻止摩卡测试运行

reactjs - Material-ui 水平菜单

java - 如何为本类(class)编写单元测试

c++ - 交织 EXPECT_CALL() 和对模拟函数的调用

reactjs - reactjs datepicker日期格式

angular - 测试可观察的完成情况

node.js - 如何使用 jasmine 测试是否在 Meteor.startup 中调用函数?

javascript - 滚动网格以显示列

unit-testing - 如何隐藏由 selenium rc 启动的 firefox 插件弹出窗口?