Unit Test: React-Bootstrap Modal

Writing unit test always help in longer run. Now a days almost all frameworks and libraries are supporting unit testing. In React Bootstrap, there is documentation for using the unit test cases but still writing unit test cases for React-Bootstrap Modal can get confusing.

After spending hours on it finally I am able to write test cases for it. These code snippets can be used for other React-Bootstrap components as well (by changing the corresponding names).

Modal Source code:

 import React from 'react';  
 import Container from 'react-bootstrap/Container';  
 import './uv-modal.css';  

 function UVModal(props) {  

  return (  
   <Modal {...props}  
       aria-labelledby="contained-modal-title-vcenter">  

    <Modal.Header closeButton>  
     <Modal.Title id="contained-modal-title-vcenter">  
      Modal Title  
     </Modal.Title>  
    </Modal.Header>  

    <Modal.Body>  
     <Container>  
      Modal Body Contents  
     </Container>  
    </Modal.Body>  

    <Modal.Footer>  
     <Button onClick={props.onHide}>Close</Button>  
    </Modal.Footer>  
   </Modal>  
  );  
 }  
 export default UVModal;

Modal Test cases:

 import React from 'react';  
 import { mount } from 'enzyme';  
 import Modal from 'react-bootstrap/Modal';  
 import UVModal from './UVModal';  

 let modalShow = true;  
 let componentToTest = {  
  title: 'UVModal: ',  
  html: <UVModal show={modalShow}/>  
 };  

 let wrapper = mount(componentToTest.html),  
   elementToSearch;  

 it(componentToTest.title + 'renders without crashing', () => {  
  mount(componentToTest.html);  
 });
  
 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);  
 });


Packages used:

 "react": "^16.9.0",  
 "react-bootstrap": "^1.0.0-beta.9",  
 "react-dom": "^16.9.0",  
 "enzyme": "^3.10.0",  
 "enzyme-adapter-react-16": "^1.14.0",
Happy Unit Testing.


Comments

Popular posts from this blog

Cheat Sheet - Git

Stack implementation using VanillaJS

JavaScript Best Practices