首页 > 其他 > 详细

[React Testing] Test a Custom React Hook with renderHook from React Hooks Testing Library

时间:2020-05-04 09:04:04      阅读:50      评论:0      收藏:0      [点我收藏+]

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)
})

 

[React Testing] Test a Custom React Hook with renderHook from React Hooks Testing Library

原文:https://www.cnblogs.com/Answer1215/p/12824960.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!