0

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?

2
  • 2
    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 Commented Feb 28, 2020 at 5:52
  • @johnnypeter ahh so I was viewing it for something it is not - thanks for the clarification! Commented Feb 28, 2020 at 18:53

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.