我正在尝试使用 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/