使用Reaction测试库检查出现在元素内的文本出现在、元素、文本、测试

2023-09-03 14:43:00 作者:没了对象省了流量

我正在使用Testing Library为Reaction应用程序编写一些测试。我想检查某些文本是否出现,但我需要检查它是否出现在特定位置,因为我知道它已经出现在其他位置。

Testing Library documentation for queries表示getByText查询接受container参数,我猜该参数允许您在该容器内进行搜索。我尝试这样做,按照文档中指定的顺序使用containertext参数:

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