Here are some test cases I've encountered:
import { loginPagePresenter } from './LoginPagePresenter'
import { apiGateway } from 'config/gatewayConfig'
import { authRepository } from './AuthRepository'
it('should modify the user in the auth repository with the token, email, and set authenticated observable when a successful API call is made', async () => {
const authenticatedStub = {
'success': true,
'message': 'successful login',
'email': '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a6c7e6c488c5c9cb">[email protected]</a>',
'token': '123'
}
apiGateway.post = jest.fn().mockResolvedValue(authenticatedStub)
loginPagePresenter.email = '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0f6e4f6d216c6062">[email protected]</a>'
loginPagePresenter.password = 'aaabbbcom'
await loginPagePresenter.submit()
expect(authRepository.user.token).toBe('123')
expect(authRepository.user.email).toBe('<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="721332105c111d1f">[email protected]</a>')
expect(authRepository.authenticated).toBe(true)
})
it('should not make changes to the user model during an unsuccessful API call', async () => {
const notAutenticatedStub = {
'success': false,
'message': 'bad login',
'email': '',
'token': ''
}
apiGateway.post = jest.fn().mockResolvedValue(notAutenticatedStub)
loginPagePresenter.email = '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7a1b3a1854191517">[email protected]</a>'
loginPagePresenter.password = 'aaabbbcom'
await loginPagePresenter.submit()
expect(authRepository.user.token).toBe(null)
expect(authRepository.user.email).toBe(null)
expect(authRepository.authenticated).toEqual(false)
})
The behavior of the first test is interfering with the second test. Disabling the first test seems to resolve the issue with the second test. The production code appears to be functioning correctly. However, there seems to be a side effect caused by the mocking function in the first test that prevents the second test from running properly (resetting the returned resolved function may be causing this).
If anyone has insights on how to address this problem, please share.