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)
    })
  • 相关阅读:
    tcp/心跳包
    TCP协议中的三次握手和四次挥手(图解)
    http 中get和post
    xmpp总结
    IOS中http请求使用cookie
    sdwebimage总结
    iOS断言
    Object-C自定义对象NSLog输入信息
    NSTimer你真的会用了吗
    ios中block中的探究
  • 原文地址:https://www.cnblogs.com/Answer1215/p/12824960.html
Copyright © 2011-2022 走看看