I'm testing the rejection of the submit event of my login form. If the user just submitted the form without filling up the username and password, the two error messages must show up and it should pass the test. But the result of the test shows the opposite: it shows that the username and password error messages are null. I tried using setTimeout() since the onSubmit event is asynchronous because of axios, but it still didn't pass the test. Is there anything wrong about the way I use the waitFor() utility for an asynchronous submit event?
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)
})
