【发布时间】:2020-07-19 23:18:07
【问题描述】:
我正在测试我的登录表单的提交事件是否被拒绝。如果用户只是提交了表单而没有填写用户名和密码,则必须显示两条错误消息并且它应该通过测试。但测试结果却相反:显示用户名和密码错误信息为null。我尝试使用setTimeout(),因为onSubmit 事件由于axios 是异步的,但它仍然没有通过测试。我将waitFor() 实用程序用于异步提交事件的方式有什么问题吗?
it('Should render username and password error messages when both inputs are blank', () => {
const { getByTestId, queryByTestId } = render(<Index />)
fireEvent.submit(getByTestId('form'))
expect(getByTestId('submit-button').disabled).toBeTruthy()
setTimeout(async () => {
expect(getByTestId('submit-button').disabled).toBeFalsy()
const usernameError = await waitFor(() => queryByTestId('username-error'))
expect(usernameError).toBeInTheDocument()
const passwordError = await waitFor(() => queryByTestId('password-error'))
expect(passwordError).toBeInTheDocument()
}, 0)
})
【问题讨论】:
-
这可能意味着该元素不存在。尝试打印 dom 以确保
标签: reactjs unit-testing jestjs react-testing-library