[英]How to test parent child relationship in react-testing-library?
Lets say this is my HTML, created from using semantic-ui.假设这是我的 HTML,使用语义 ui 创建。
I select number 35 from my dropdown and the result becomes this:我从下拉列表中选择了数字 35,结果变成了这样:
<div name="genre" style="width:400px" role="combobox" aria-expanded="false" class="ui compact fluid multiple search selection dropdown">
<a class="ui label" value="35">Comedy<i aria-hidden="true" class="delete icon"></i></a>
<div role="option" aria-checked="false" aria-selected="true" class="selected item" style="pointer-events: all;"><span class="text">Comedy</span></div>
</div>
Ideally I want to test the parent items children like this:理想情况下,我想像这样测试父项子项:
This doesn't seem possible.这似乎不可能。
I can only test one selector level for example - by getByText('Comedy')
which is too generic since there are other 'Comedy' elements on the same page.例如,我只能测试一个选择器级别 - 通过
getByText('Comedy')
这太通用了,因为同一页面上还有其他 'Comedy' 元素。
So how to select something like this:那么如何选择这样的东西:
getByWhatever('Some parent').getChildByWhatever('Some child').exists()
You can use the within
query to get elements within another specific element.您可以使用
within
查询来获取另一个特定元素中的元素。
const combobox = screen.getByRole('combobox'); // Retrieve the parent
expect(combobox).toHaveAttribute('name', 'genre');
const link = within(combobox).getByRole('link', { name: 'Comedy' }); // Retrieve the link child from within the parent
expect(link).toHaveAttribute('value', '35');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.