Checking text appears inside an element using react testing library(使用Reaction测试库检查出现在元素内的文本)
问题描述
我正在使用Testing Library为Reaction应用程序编写一些测试。我想检查某些文本是否出现,但我需要检查它是否出现在特定位置,因为我知道它已经出现在其他位置。
Testing Library documentation for queries表示getByText
查询接受container
参数,我猜该参数允许您在该容器内进行搜索。我尝试这样做,按照文档中指定的顺序使用container
和text
参数:
const container = getByTestId('my-test-id');
expect(getByText(container, 'some text')).toBeTruthy();
我收到一个错误:matcher.test is not a function
。
如果我将参数反过来:
const container = getByTestId('my-test-id');
expect(getByText('some text', container)).toBeTruthy();
我收到不同的错误:Found multiple elements with the text: some text
这意味着它没有在指定容器内进行搜索。
我想我不理解getByText
是如何工作的。我做错了什么?
推荐答案
这类事情最好使用within
:
const { getByTestId } = render(<MyComponent />)
const { getByText } = within(getByTestId('my-test-id'))
expect(getByText('some text')).toBeInTheDocument()
这篇关于使用Reaction测试库检查出现在元素内的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用Reaction测试库检查出现在元素内的文本


基础教程推荐
- fetch 是否支持原生多文件上传? 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- Bokeh Div文本对齐 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01