Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to check disabled status of input field react-test-library

I’m using @testing-library/react for react unit test.

I wanna do unit test of input field’s disabled status and value.

Below is dom element from chrome inspect.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<input type="text" placeholder="Enter text..." aria-disabled="false" class="styles__TextInput-sc-1vdcacp-2 grIDkE" value="test">

I tried like below, but it does not work.

expect(screen.getByText('Enter text...')).toBeVisible();
expect(screen.getByRole('input')).toBeVisible();

Please let me know how to test input field and it’s disabled status and value.

>Solution :

You can use getByRole, but parameter should be textbox, not input.

And for input field’s value, you can test it like this:

expect(screen.getByRole('textbox').value).toBe('test');

For check disabled status, you can use toHaveAttribute like this:

expect(screen.getByRole('textbox')).toHaveAttribute(
      'aria-disabled',
      'true'
    );
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading