zoukankan      html  css  js  c++  java
  • [React Testing] Test a Custom React Hook with renderHook from React Hooks Testing Library

    That setup function is pretty handy. Seems like a good opportunity for an abstraction. Well, we already have one! It’s called React Hooks Testing Library. Let’s swap our setup function for the renderHook function from @testing-library/react-hooks.

    Testing:

    import {renderHook, act} from '@testing-library/react-hooks'
    import {useCounter} from '../use-counter'
    
    test('exposes the count and increment/decrement functions', () => {
      const {result} = renderHook(useCounter)
      expect(result.current.count).toBe(0)
      act(() => result.current.increment())
      expect(result.current.count).toBe(1)
      act(() => result.current.decrement())
      expect(result.current.count).toBe(0)
    })
    
    test('allows customization of the initial count', () => {
      const {result} = renderHook(useCounter, {initialProps: {initialCount: 3}})
      expect(result.current.count).toBe(3)
    })
    
    test('allows customization of the step', () => {
      const {result} = renderHook(useCounter, {initialProps: {step: 2}})
      expect(result.current.count).toBe(0)
      act(() => result.current.increment())
      expect(result.current.count).toBe(2)
      act(() => result.current.decrement())
      expect(result.current.count).toBe(0)
    })
  • 相关阅读:
    面向对象之多态,property
    描述符
    day23 面向对象之继承
    day22面向对象
    os模块
    logging日志模块,四种方式
    Linux 如何测试 IO 性能(磁盘读写速度)
    Vi命令:如何删除全部内容
    cdnbest如何查看站点操作日志(同步日志)
    Linux查找含有某字符串的所有文件
  • 原文地址:https://www.cnblogs.com/Answer1215/p/12824960.html
Copyright © 2011-2022 走看看