使用 react-testing-library 选择子组件
Selecting child component with react-testing-library
我可以使用 Enzyme 轻松 select 子组件,但我想使用 react-testing-library。假设我有一个 returns 以下组件:
return (
<DropdownButton>
<Dropdown.Item data-testid='x' key={id} />
</DropdownButton>
)
截至目前,在测试中我可以轻松地 select Dropdown.Item
和 const item = wrapper.find(Dropdown.Item)
但是为什么我不能 select 通过 const { getByTestId } = render(<MyComponent />)
和 const item = getByTestId('x')
使用 react-testing-library 和 data-testid?
我应该使用不同的查询吗?
根据上面的评论,react-testing-library“...should deal with DOM nodes rather than component instances, and it should not encourage dealing with component instances”。我以错误的观点来处理这个问题。
感谢 johnny peter 的澄清。
我可以使用 Enzyme 轻松 select 子组件,但我想使用 react-testing-library。假设我有一个 returns 以下组件:
return (
<DropdownButton>
<Dropdown.Item data-testid='x' key={id} />
</DropdownButton>
)
截至目前,在测试中我可以轻松地 select Dropdown.Item
和 const item = wrapper.find(Dropdown.Item)
但是为什么我不能 select 通过 const { getByTestId } = render(<MyComponent />)
和 const item = getByTestId('x')
使用 react-testing-library 和 data-testid?
我应该使用不同的查询吗?
根据上面的评论,react-testing-library“...should deal with DOM nodes rather than component instances, and it should not encourage dealing with component instances”。我以错误的观点来处理这个问题。
感谢 johnny peter 的澄清。