• [React Testing] Test componentDidCatch handler Error Boundaries


    Error boundary:

    import React from 'react'
    import { reportError } from './components/extra/api'
    
    export default class ErrorBoundary extends React.Component {
      constructor(props) {
        super(props)
        this.state = { hasError: false }
      }
    
      static defaultProps = {
        fallback: <h1>Something went wrong.</h1>,
      }
    
      static getDerivedStateFromError(error) {
        return { hasError: true }
      }
    
      componentDidCatch(error, errorInfo) {
        console.log(error, errorInfo)
        reportError(error, errorInfo)
      }
    
      render() {
        if (this.state.hasError) {
          return this.props.fallback
        }
    
        return this.props.children
      }
    }

    What we want to test is 'reportError' was called when error happens

    Test:

    import React from 'react'
    import { render, fireEvent } from '@testing-library/react'
    import { ErrorBoundary } from './error-boundary'
    import { reportError as mockReportError } from './components/extra/api'
    
    function Bomb(shouldThrow) {
      if (shouldThrow) {
        throw new Error('Bomb')
      } else {
        return null
      }
    }
    
    jest.mock('./components/extra/api')
    
    test('calls reportError and renders that there was a problem', () => {
      mockReportError.mockResolvedValueOnce({ success: true })
      const { rerender } = render(
        <ErrorBoundary>
          <Bomb />
        </ErrorBoundary>,
      )
    
      rerender(
        <ErrorBoundary>
          <Bomb shouldThrow={true} />
        </ErrorBoundary>,
      )
    
      const error = expect.any(Error)
      const errorInfo = { componentStack: expect.stringContaining('Bomb') }
      expect(mockReportError).toHaveBeenCalledWith(error, errorInfo)
      expect(mockReportError).toHaveBeenCalledTimes(1)
    })
    
    // Clearn the mock impl afterEach(()
    => { jest.clearAllMocks() })

    Notice:

      const error = expect.any(Error)
      const errorInfo = { componentStack: expect.stringContaining('Bomb') }

    Both uses 'expect' static methods.

    expect.any(): https://jestjs.io/docs/en/expect#expectanyconstructor

    expect.stirngContiaining(): https://jestjs.io/docs/en/expect#expectstringcontainingstring

    In the testin, we mock the whole 'api' module with jest.fn(), just provide the mock implementation for 'reportError':

    mockReportError.mockResolvedValueOnce({ success: true })

    Remember to claer the mock Implmentation after each test:

    afterEach(() => {
      jest.clearAllMocks()
    })
  • 相关阅读:
    VOC2012数据集
    flask 图像接口测试
    PyQt5 学习指南
    超分辨中的注意力机制:Attention in Super Resolution
    OpenCV:图像增亮与直方图均衡
    SOA 架构与微服务架构的区别
    Docker 部署 vue 项目
    element ui 中的 resetFields() 报错'resetFields' of undefined
    Mybatis分页方法
    Serializable的作用
  • 原文地址:https://www.cnblogs.com/Answer1215/p/12814559.html
Copyright © 2020-2023  润新知