单元测试React Bootstrap模态对话框

时间:2015-11-11 16:44:25

标签: 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作为第一个参数传递。

有人可以告诉我出了什么问题吗?

3 个答案:

答案 0 :(得分:5)

结帐react-bootstrap本身权限如何测试。模态被渲染成一个不同的子树,它是如何呈现给文档主体而不是直接作为其父项的子项。换句话说,您的srcying失败,因为该组件不在该Component树中。

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

答案 1 :(得分:0)

React-Bootstrap模态可以使用mount中的enzyme

进行单元测试
it(componentToTest.title + 'renders Modal component', () => {  
  expect(wrapper.find(UVModal).length).toEqual(1);  
 });

 it(componentToTest.title + 'renders major html elements', () => {  
  // Test whether modal-content element has 3 html children elements.  
  expect(wrapper.find('.modal-content').length).toEqual(1);  
  expect(wrapper.find('.modal-content').children()).toHaveLength(3);  

  // Test whether modal-header element has 2 html children elements.  
  expect(wrapper.find('.modal-header').length).toEqual(1);  
  expect(wrapper.find('.modal-header').children()).toHaveLength(2);  

  // Test whether modal-body element has 1 html child element.  
  expect(wrapper.find('.modal-body').length).toEqual(1);  
  expect(wrapper.find('.modal-body').children()).toHaveLength(1);  

  // Test whether modal-footer element has 1 html child element.  
  expect(wrapper.find('.modal-footer').length).toEqual(1);  
  expect(wrapper.find('.modal-footer').children()).toHaveLength(1);  

  elementToSearch = <p>Lannisters always pay their debt</p>;  
  expect(wrapper.contains(elementToSearch)).toEqual(false);  
 });

查看以下博客以获取详细信息:

https://yuvi1422.blogspot.com/2019/08/unit-test-react-bootstrap-modal.html

答案 2 :(得分:0)

如果您使用的是旧版本的 Enzyme,您可以将容器元素传递到您希望渲染 Modal 的位置,如下所示:

Actual Code:
------------
import React from 'react'
import { Modal } from 'reactstrap'

export default MyModal = () => {
    return (
        <Modal isOpen={props.isOpen}>
            <ModalHeader>Header</ModalHeader>
            <ModalBody>Body</ModalBody>
        </Modal>
     );
} 

Unit Test:
----------
import React from 'react'
import MyModal  from './MyModal'
import { mount } from 'enzyme'

describe(() => {
    let wrapper;
    beforeEach(() => {
        const container = document.createElement("div");
        document.body.appendChild(container);
        wrapper = mount( <MyModal isOpen={true}/> , {attachTo: container});
    });

    it('renders correctly', () => {
        expect(wrapper).toMatchSnapshot();

        expect(wrapper.find('ModalHeader')).toHaveLength(1);

        expect(wrapper.find('ModalBody')).toHaveLength(1);
    });

})