score:4

Accepted answer

The main point of the React Testing Library is that it's very easy to write tests that resembles on how users would use the application. This is what we want to achieve in order to give us full confidence and this is what's advocated in the industry as well. We write tests to raise confidence in our applications rather than to up the coverage.

Considering the above, you should not use toHaveBeenCalledTimes instead, you should test the visual result of triggering the onChange function. Based on the code you shared I don't know what is rendered upon successful login, but for our use case, let's say the text Welcome, User! renders.

You could fire an event as you do now, and instead of testing with haveBeenCalled, use the following:

expect(getByText('Welcome, User!').toBeInTheDocument();

This will give you full confidence as opposed to testing if a function was called which won't give you enough confidence because what if the implementation of the function is not correct?


Related Query

More Query from same tag