admin管理员组文章数量:1257740
The best possible selector we can use in React Testing Library is getByRole()
. This is good because we can select our node by it's role and accessibility name.
If we have a rendered component where we have multiple buttons - so I can't just use only getByRole('button')
- and the button doesn't have text, meaning it doesn't have an accessible name, instead we have an image, how would I go about selecting that?
I know the image's accessible name derives from the alt
attribute, and I am also aware I could use alternatives such as data-testid
, however I would prefer to go with getByRole
if there is a chance, so my tests resembles the way my software is used.
<Button onClick{() => void}>
<img
src={"foo"}
alt={"alt text"}
/>
<Button/>
getByRole('button')
will throw me an error as there will be multiple buttons on the wrapping component
getAllByRole('button')
will return an array with all the buttons, however I only want the one with the image
I tried getByRole('button', {name: "alt text"})
but this doesn't work, it kinda makes sense as it's a different node.
Any ideas if there is a logical way to get this using getByRole
or I have to give up and resort to something like data-testid?
Thank you!
The best possible selector we can use in React Testing Library is getByRole()
. This is good because we can select our node by it's role and accessibility name.
If we have a rendered component where we have multiple buttons - so I can't just use only getByRole('button')
- and the button doesn't have text, meaning it doesn't have an accessible name, instead we have an image, how would I go about selecting that?
I know the image's accessible name derives from the alt
attribute, and I am also aware I could use alternatives such as data-testid
, however I would prefer to go with getByRole
if there is a chance, so my tests resembles the way my software is used.
<Button onClick{() => void}>
<img
src={"foo"}
alt={"alt text"}
/>
<Button/>
getByRole('button')
will throw me an error as there will be multiple buttons on the wrapping component
getAllByRole('button')
will return an array with all the buttons, however I only want the one with the image
I tried getByRole('button', {name: "alt text"})
but this doesn't work, it kinda makes sense as it's a different node.
Any ideas if there is a logical way to get this using getByRole
or I have to give up and resort to something like data-testid?
Thank you!
Share Improve this question asked Jul 20, 2020 at 17:08 anddakanddak 6611 gold badge6 silver badges17 bronze badges2 Answers
Reset to default 25The best way that I know of to do this is by adding an aria-label
to the button element. RTL will be able to query that as an accessible name with:
getByRole('button', { name: /button-name/i })
<Button aria-label="button-name" onClick{() => void}>
<img
src={"foo"}
alt={"alt text"}
/>
<Button/>
You could select the corresponding button by screen.getByAltText('alt text').parentNode
.
If it's only about firing a click event, you could directly use the img node -
userEvent.click(screen.getByAltText('alt text'))
.
Hope this works for your case.
本文标签: javascriptHow to getByRole a button with image in React Testing LibraryStack Overflow
版权声明:本文标题:javascript - How to getByRole a button with image in React Testing Library - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738345801a2078038.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论