0
votes

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?

1
Testing-library is built around asserting the end dom rather than components. What you want is against the principles of testing-library testing-library.com/docs/guiding-principles - johnny peter
@johnnypeter ahh so I was viewing it for something it is not - thanks for the clarification! - toobs

1 Answers

0
votes

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.