[英]What is the difference “getByText” and “getByTestId” ? In testing-library/react
What is the difference between getByText
and getByTestId
? getByText
和getByTestId
什么区别?
When I had tested a React component, there were some gaps between these two functions.当我测试一个 React 组件时,这两个函数之间存在一些差距。
The test failed in getByText
code, but it succeeded in getByTestId
.测试在
getByText
代码中失败,但在getByTestId
中成功。
I have code that when an element was clicked, the color of the title changed to red.我有代码,当单击一个元素时,标题的颜色变为红色。
Why this is the difference?为什么会有这样的区别?
I omitted styled-components of Container
and Content
.我省略了
Container
和Content
的 styled-components 。 This has props 'toggled' to change the color to red.这使道具“切换”以将颜色更改为红色。
Here is the getByText
code:这是
getByText
代码:
const { getByText } = render(<ListPresenter content={ListText} />);
const colorChangedText = getByText(/the party/);
fireEvent.click(colorChangedText);
screen.debug(); // The result of render I want !
expect(colorChangedText).toHaveStyle("color: red"); * failed
Here is the getByTestId
code:这是
getByTestId
代码:
const { getByText } = render(<ListPresenter content={ListText} />);
fireEvent.click(getAllByTestId("list-element-toggle")[0]);
screen.debug(); // The result of render I want !
const colorChangedText = getAllByTestId("list-element-content")[0];
expect(colorChangedText).toHaveStyle("color: red"); * success
Here is the rendered component:这是渲染的组件:
const Component = (props) => {
return (
<Container
className="container"
data-testid={"list-element-toggle"}
toggled={state[data.id - 1]}
>
<Content className="content" data-testid={"list-element-content"} toggled={state[data.id - 1]}>
{data.titleChild}. // This text had been changed to red color when i was clicked.
</div>
</div>
)
}
react-testing-library cheetsheet react-testing-library 小贴士
I've found that selecting by text can be a bit finicky and usually end up adding a testid
attribute to query/target the exact element I want.我发现按文本选择可能有点挑剔,通常最终会添加一个
testid
属性来查询/定位我想要的确切元素。
My guess is the getByText
may not be returning the correct element/wrapper.我的猜测是
getByText
可能没有返回正确的元素/包装器。
From your test code从您的测试代码
render(<ListPresenter content={ListText} />);
It's not clear to me what the text in the test may be.我不清楚测试中的文字可能是什么。 It's not even clear to me if
ListPresenter
is the Component
in the last snippet.我什至不清楚
ListPresenter
是否是最后一个片段中的Component
。 For example, what is data.titleChild
?例如,什么是
data.titleChild
?
See also Which query should I use?另请参阅我应该使用哪个查询?
Queries Accessible to Everyone每个人都可以访问的查询
getByText
: Not useful for forms, but this is the number 1 method a user finds most non-interactive elements (like divs and spans).getByText
:对 forms 没有用,但这是用户找到大多数非交互式元素(如 div 和 span)的第 1 种方法。
Test IDs测试 ID
getByTestId
: The user cannot see (or hear) these, so this is only recommended for cases where you can't match by role or text or it doesn't make sense (eg the text is dynamic).getByTestId
:用户无法看到(或听到)这些内容,因此仅建议在无法按角色或文本匹配或没有意义(例如文本是动态的)的情况下使用。
As a fallback there also Manual Queries .作为后备,还有Manual Queries 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.