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)
    })
  • 相关阅读:
    redis数据结构-list
    reids数据结构1-string
    jedis工具类
    静态资源放行
    SpringMVC拦截器
    xinetd
    Linux-open函数
    Linux简单的文件读取
    复习ssm02
    springMVC文件上传
  • 原文地址:https://www.cnblogs.com/Answer1215/p/12824960.html
Copyright © 2011-2022 走看看