首页 > 其他 > 详细

[React Testing] Assert That Something is NOT Rendered with React Testing Library (with rerender & query)

时间:2020-04-30 19:33:41      阅读:66      评论:0      收藏:0      [点我收藏+]

You can use ‘rerender‘ for a component when its props changed.

 

Then if you wnat to check the alert message has gone when we rerender, you need to use ‘queryByRole‘ instead of ‘getByRole‘, because ‘getByRole‘ will throw when the element is not there

test(rerender the component when the prop changes, () => {
  const { getByLabelText, getByRole, queryByRole, rerender } = render(
    <FavoriteNumber />,
  )
  const input = getByLabelText(/favorite number/i)
  user.type(input, 10)
  expect(getByRole(alert)).toHaveTextContent(/the number is invalid/i)

  // rerender with a different prop
  rerender(<FavoriteNumber max={10} />)
  expect(queryByRole(‘alert‘)).not.toBeInTheDocument()
})

 

[React Testing] Assert That Something is NOT Rendered with React Testing Library (with rerender & query)

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

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