Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test input without label with React Testing Library

I use react-testing-library in my application. Normally, to check if the input is in the form, we use getByLabelText. For example:

getByLabelText(/name/i)

But what if my input does not have any label? This is because of the design (UI) so I cannot have a label for the input. How can I check if that input exists in the form?

like image 721
coinhndp Avatar asked Oct 15 '25 01:10

coinhndp


1 Answers

You can use the following to target a text input field.

getByRole('textbox', {name: /name/i})

More info https://testing-library.com/docs/queries/about#priority

like image 186
CWSites Avatar answered Oct 17 '25 21:10

CWSites



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!