【问题标题】:Unit testing React Bootstrap modal dialog单元测试 React Bootstrap 模式对话框
【发布时间】:2016-02-12 20:01:24
【问题描述】:

我正在尝试使用 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 行还有什么问题。如果我取消注释行,我会在 cmets 中显示错误。

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

谁能告诉我怎么了?

【问题讨论】:

    标签: unit-testing reactjs jasmine react-bootstrap


    【解决方案1】:

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

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

    【讨论】:

    【解决方案2】:

    React-Bootstrap modal 可以使用mount of 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://medium.com/@yuvi1422/unit-test-react-bootstrap-modal-a37bf59732ab

    【讨论】:

      【解决方案3】:

      如果您使用的是旧版本的 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);
          });
      
      })
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-04-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-01
        • 2021-11-06
        相关资源
        最近更新 更多