testing-library / dom-testing-library

🐙 Simple and complete DOM testing utilities that encourage good testing practices.
https://testing-library.com/dom
MIT License
3.26k stars 467 forks source link

aria-label or test-id for for elements with no role ? #1151

Closed mo-alaa closed 2 years ago

mo-alaa commented 2 years ago

Hi, I have a hidden input inside a button to upload files.

<button>
  <p>upload</p>
  <input hidden type='file'/>
</button>

since the input 'file' doesn't have a role by default, should I add an "aria-label" or a "data-testid" for the input element?

Because as said here : 'The first rule of aria is don't use aria'.

Also testing library says this about using data-testid: 'Using data-testid attributes do not resemble how your software is used and should be avoided if possible'

so what should I pick ?

eps1lon commented 2 years ago

How is the hidden input used? If it's in a form whose data you use, test submitting it and check for data.

It's generally hard to give good advise without knowing the full UI.

alexkrolick commented 2 years ago

since the input 'file' doesn't have a role by default

the default role of a file input is button

image
MatanBobi commented 2 years ago

@alexkrolick that's probably a chrome decision and not a spec definition so I guess it might not work in JSDOM. Based on the spec, as @mo-alaa said, input type file has no implicit role: image

eps1lon commented 2 years ago

Closing in favor of https://testing-library.com/discord. The documentation still applies. But concrete advise depends on the actual UI.