vendredi 1 novembre 2019

The Testing Library way of finding "image buttons"

What do people use for finding "image buttons" by their alt text using Testing Library queries?

I have something like this:

<div
  role="button"
  aria-pressed="false"
>
  <img src="some/image.svg" alt="Click me"/>
</div>

The query I ended up using to find this button:

getByAltText('Click me').closest('[role=button]')

It works but is not amazing. It assumes implementation details like "the button contains an image with alt text".

What I actually want is "give me a button that - not matter how - contains this specific text". Some variant of getByText('Click me') which by the way just works if this is a plain <button>. I consider the difference to be an implementation detail...

Aucun commentaire:

Enregistrer un commentaire