-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: criado mais testes unitários para o login
- Loading branch information
1 parent
348f016
commit 8856ffb
Showing
3 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,4 +13,5 @@ export class UserServiceMock { | |
getProjectsForUser() { | ||
return of({}); | ||
} | ||
saveUser() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,9 +2,15 @@ import { ComponentFixture, TestBed } from '@angular/core/testing'; | |
|
||
import { FormBuilder } from '@angular/forms'; | ||
import { Router } from '@angular/router'; | ||
import { | ||
LoginPayload, | ||
LoginResponse | ||
} from '@core/interfaces/authentication.interface'; | ||
import { User } from '@core/interfaces/user.interface'; | ||
import { AuthService } from '@core/services/auth/auth.service'; | ||
import { ToastAlertService } from '@core/services/toast-alert/toast-alert.service'; | ||
import { UserService } from '@core/services/user/user.service'; | ||
import { of, throwError } from 'rxjs'; | ||
import { AuthServiceMock } from 'src/__mocks__/services/auth.service.mock'; | ||
import { ToastAlertServiceMock } from 'src/__mocks__/services/toast-alert.service.mock'; | ||
import { UserServiceMock } from 'src/__mocks__/services/user.service.mock'; | ||
|
@@ -14,6 +20,12 @@ describe('LoginComponent', () => { | |
let component: LoginComponent; | ||
let fixture: ComponentFixture<LoginComponent>; | ||
|
||
const mockLoginResponse: LoginResponse = { | ||
accessToken: 'accessToken', | ||
refreshToken: 'refreshToken', | ||
user: { id: 'id', name: 'name' } as User | ||
}; | ||
|
||
beforeEach(() => { | ||
TestBed.overrideComponent(LoginComponent, { | ||
set: { | ||
|
@@ -35,4 +47,78 @@ describe('LoginComponent', () => { | |
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
|
||
it('should call login method with success', () => { | ||
component.loginForm.setValue({ | ||
email: '[email protected]', | ||
password: 'password' | ||
}); | ||
fixture.detectChanges(); | ||
console.log('valid', component.loginForm.valid); | ||
const registerFormValue = component.loginForm.getRawValue(); | ||
const payload: LoginPayload = { | ||
email: registerFormValue.email!, | ||
password: registerFormValue.password! | ||
}; | ||
|
||
const spyAuthServiceLogin = spyOn( | ||
component['authService'], | ||
'login' | ||
).and.returnValue(of(mockLoginResponse)); | ||
const spyAuthServiceSetTokens = spyOn( | ||
component['authService'], | ||
'setTokens' | ||
); | ||
const spyUserServiceSaveUser = spyOn(component['userService'], 'saveUser'); | ||
const spyToastAlertServiceAddSuccessMessage = spyOn( | ||
component['toastAlertService'], | ||
'addSuccessMessage' | ||
); | ||
const spyRouterNavigate = spyOn(component['router'], 'navigate'); | ||
|
||
component.loginUser(); | ||
expect(spyAuthServiceLogin).toHaveBeenCalledWith(payload); | ||
component['authService'].login(payload).subscribe(() => { | ||
expect(spyAuthServiceSetTokens).toHaveBeenCalledWith(mockLoginResponse); | ||
expect(spyUserServiceSaveUser).toHaveBeenCalledWith( | ||
mockLoginResponse.user | ||
); | ||
expect(spyRouterNavigate).toHaveBeenCalledWith(['/dashboard']); | ||
expect(spyToastAlertServiceAddSuccessMessage).toHaveBeenCalledWith({ | ||
title: 'Success', | ||
description: 'User logged in' | ||
}); | ||
}); | ||
}); | ||
|
||
it('should call login method with error', () => { | ||
component.loginForm.setValue({ | ||
email: '[email protected]', | ||
password: 'password' | ||
}); | ||
fixture.detectChanges(); | ||
const registerFormValue = component.loginForm.getRawValue(); | ||
const payload: LoginPayload = { | ||
email: registerFormValue.email!, | ||
password: registerFormValue.password! | ||
}; | ||
|
||
const spyAuthServiceLogin = spyOn( | ||
component['authService'], | ||
'login' | ||
).and.returnValue(throwError(() => new Error('Invalid credentials'))); | ||
const spyToastAlertServiceAddDangerMessage = spyOn( | ||
component['toastAlertService'], | ||
'addDangerMessage' | ||
); | ||
|
||
component.loginUser(); | ||
expect(spyAuthServiceLogin).toHaveBeenCalledWith(payload); | ||
component['authService'].login(payload).subscribe(() => { | ||
expect(spyToastAlertServiceAddDangerMessage).toHaveBeenCalledWith({ | ||
title: 'Error', | ||
description: 'Invalid credentials' | ||
}); | ||
}); | ||
}); | ||
}); |