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

Query all Input Fields #1162

Closed DarkTrick closed 2 years ago

DarkTrick commented 2 years ago

Not sure, if this is actually possible...

Motivation

I'd like to be able to figure out, if someone changed the UI (added input element to form) without changing the test.

Describe the feature you'd like:

Function call to query all input fields (like queryAllInputFields)

Describe alternatives you've considered:

AFAIK there is none.

Teachability, Documentation, Adoption, Migration Strategy:

  const inputs = screen.queryAllInputs();
  expect(inputs.length).toBe(5);
  expect(inputs[0].value).toBe("...");
timdeschryver commented 2 years ago

You can query the DOM if you want. Depending on your use case, you can use getAll queries or just document.querySelectorAll

const inputs =  screen.getAllByRole('textbox');
const inputs = document.querySelectorAll('input');
// or another variant
DarkTrick commented 2 years ago

Then this is perhaps a documentation issue(?) Eg. The cheat sheet says ByRole find by aria role. Would be very nice if the documentation would be more clear about this :)

MatanBobi commented 1 year ago

@DarkTrick ByRole uses implicit or explicit roles, in this case an input element without a type defaults to role="textbox". More about this can be found in the ARIA spec.