【发布时间】:2021-05-01 05:37:33
【问题描述】:
我可以使用.getByPlaceholderText 正确渲染我的组件并获取输入标签。问题是在我使用fireEvent 更改输入值后,我无法使用findByText 获得相同的输入。这不是我要使用的测试;我只是想了解为什么这不起作用。
it('displays the clearIcon by default', async () => {
// Render my component
render(<SearchInput {...defaultProps} />)
// find the input html element by placeholder - it works
const inputNode = await screen.findByPlaceholderText(/search/i)
// fire an event where I change the value of the input to "Text"
fireEvent.change(inputNode, { target: { value: 'Text' } })
// find the same element searching by the text I added in it - "Text"
// this fails saying that it's "unable to find an element with the text /Text/i"
const sameInput = await screen.findByText(/Text/i)
expect(sameInput).toBeInTheDocument()
})
【问题讨论】:
标签: javascript reactjs testing jestjs react-testing-library