简体   繁体   English

“getByText”和“getByTestId”有什么区别? 在测试库/反应

[英]What is the difference “getByText” and “getByTestId” ? In testing-library/react

What is the difference between getByText and getByTestId ? getByTextgetByTestId什么区别?

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 .我省略了ContainerContent的 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 小贴士

  • ByText find by element text content ByText按元素文本内容查找
  • ByTestId find by data-testid attribute ByTestId按 data-testid 属性查找

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.

相关问题 testing-library/react 中 toJSON 的等价物是什么? - What is the equivalent of toJSON in testing-library/react? 如何期望 getByText 在反应测试库中为假? - How to expect a getByText to be false in react testing library? 如何在反应测试库中将参数传递给 getByText? - How to pass a an argument to getByText in react Testing Library? @testing-library/react 中的 toBeInTheDocument 和 getBy* 有什么区别 - Whats the difference between toBeInTheDocument and getBy* in @testing-library/react 对于异步挂钩,@testing-library/react 中的 function waitForNextUpdate (@testing-library/react-hooks) 有什么替代方案? - What is the alternative the function waitForNextUpdate (@testing-library/react-hooks) in @testing-library/react for async hooks? 在嵌套组件中反应原生测试库 getByText - react native testing library getByText in nested component React testing-library 模拟触摸和移动,指针的键是什么? - React testing-library to simulate touch and move, what is the key for a pointer? react 16.8.6 对应的测试库/反应版本是什么? - What are the corresponding testing-library/react versions for react 16.8.6? 将变量作为参数从反应测试库传递给 getByText 不起作用 - Passing variable as argument to getByText from react testing library does not work 使用测试库测试弹出组件以进行反应 - Testing popover components with testing-library for react
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM