Search code examples
react-testing-library

React Testing Library: Get child input inside of TestId element


I have a structure like this:

<div data-test-locator="MyId">
    <input value="some" type="checkbox" checked />
</div>

I need to test if the checkbox is checked. But how can I get it in the first place? I can get an element with a test id like this: getByTestId('MyId') But how can I get its child input?


Solution

  • If you know the structure would not change, you can use firstChild

    expect((getByTestId('MyId').firstChild).checked).toEqual(true)