【发布时间】:2021-09-29 09:01:19
【问题描述】:
在反应中,我从数组中映射出一些元素。例如
{options.map((option)=>{
return <div data-testid="option">{option}</div>
})
我有多个测试要选择一个选项(不知道选项的 textContent 是什么),所以我使用data-testid="option" 并使用screenGetAllByTestId('option')[0] 选择第一个选项...
但是,有时我知道我想要什么特定的选项,我想getByTestId,但因为它们都共享相同的 data-testid 有点困难。
我正在尝试做的是类似于这个伪代码的东西:
screen.getAllByTestId('option').getByText("Apples")
这将获得所有选项,然后获得以 Apples 作为文本的特定选项
【问题讨论】:
标签: javascript typescript jestjs react-testing-library