由网友(没了对象省了流量)分享简介:我正在使用Testing Library为Reaction应用程序编写一些测试。我想检查某些文本是否出现,但我需要检查它是否出现在特定位置,因为我知道它已经出现在其他位置。Testing Library documentation for queries表示getByText查询接受container参数,我猜该参数...![原神 元素反应改动评测,雷神上线后,雷系角色迎来春天](https://p.xsw88.cn/allimgs/daicuo/20230903/4813.png)
我正在使用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
。
![原神 元素反应改动评测,雷神上线后,雷系角色迎来春天](https://p.xsw88.cn/allimgs/daicuo/20230903/4813.png)
如果我将参数反过来:
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()
相关推荐
最新文章