简体   繁体   中英

Selecting child component with react-testing-library

I am able to select the child component easily with Enzyme but I would like to use react-testing-library. Suppose I have a component that returns the following:

return (
  <DropdownButton>
    <Dropdown.Item data-testid='x' key={id} />
  </DropdownButton>
)

As of now in a test I can easily select the Dropdown.Item with const item = wrapper.find(Dropdown.Item) but how come I cannot select via const { getByTestId } = render(<MyComponent />) and const item = getByTestId('x') using react-testing-library and data-testid?

Should I be using a different query?

As per the comment above, react-testing-library " ...should deal with DOM nodes rather than component instances, and it should not encourage dealing with component instances ". I was approaching this with the wrong view.

Thanks to johnny peter for the clarification.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM