From 86a26089f777db1e45fc0b37bf8124c15727e5a8 Mon Sep 17 00:00:00 2001 From: DaviMarinho Date: Thu, 16 Nov 2023 11:41:27 -0300 Subject: [PATCH] =?UTF-8?q?Adiciona=20testes=20unit=C3=A1rios=20no=20login?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: DaviMarinho --- src/app/pages/login/login.component.spec.ts | 78 ++++++++++++++------- 1 file changed, 52 insertions(+), 26 deletions(-) diff --git a/src/app/pages/login/login.component.spec.ts b/src/app/pages/login/login.component.spec.ts index ca4a87dd..899796b2 100644 --- a/src/app/pages/login/login.component.spec.ts +++ b/src/app/pages/login/login.component.spec.ts @@ -1,4 +1,4 @@ -import { ComponentFixture, TestBed } from '@angular/core/testing'; +import { ComponentFixture, TestBed, tick, fakeAsync } from '@angular/core/testing'; import { LoginComponent } from './login.component'; import { FormBuilder, ReactiveFormsModule } from '@angular/forms'; import { HttpClientTestingModule } from '@angular/common/http/testing'; @@ -8,16 +8,32 @@ import { ProfileComponent } from '../profile/profile.component'; import { CheckCodeRestPasswordComponent } from '../check-code-rest-password/check-code-rest-password.component'; import { RegisterComponent } from '../register/register.component'; import { of, throwError } from 'rxjs'; +import { AlertService } from '../../services/alert.service'; +import { MessageService } from 'primeng/api'; +import { VideoComponent } from '../video/video.component'; const mockUserReturn = { "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwiZW1haWwiOiJqb2FvMTV2aWN0b3IwOEBnbWFpbC5jb20iLCJleHAiOjE2OTkzMTI5MzV9.1B9qBJt8rErwBKyD5JCdsPozsw86oQ38tdfDuMM2HFI", "token_type": "bearer" -} +}; class AuthServiceMock { constructor() { } + loginUser() { - return of(mockUserReturn); + return throwError({ error: { message: 'Erro' } }); + } +} + +class MessageServiceMock { + messages: any[] = []; + + add(message: any) { + this.messages.push(message); + } + + clear() { + this.messages = []; } } @@ -25,6 +41,8 @@ describe('LoginComponent', () => { let component: LoginComponent; let fixture: ComponentFixture; let authService: AuthService; + let alertService: AlertService; + let messageService: MessageServiceMock; // Use the mock class beforeEach(async () => { TestBed.configureTestingModule({ @@ -32,16 +50,25 @@ describe('LoginComponent', () => { [ { path: 'profile', component: ProfileComponent }, { path: 'sendCodeResetPassword', component: CheckCodeRestPasswordComponent }, - { path: 'register', component: RegisterComponent } + { path: 'register', component: RegisterComponent }, + { path: 'videos', component: VideoComponent } ] )], - providers: [FormBuilder, { provide: AuthService, useValue: new AuthServiceMock() }], + providers: [ + FormBuilder, + AuthService, + AlertService, + { provide: MessageService, useClass: MessageServiceMock }, // Provide the mock class + { provide: AuthService, useClass: AuthServiceMock } + ], declarations: [LoginComponent], }).compileComponents(); fixture = TestBed.createComponent(LoginComponent); component = fixture.componentInstance; authService = TestBed.inject(AuthService); + alertService = TestBed.inject(AlertService); + messageService = TestBed.inject(MessageService) as unknown as MessageServiceMock; // Inject the mock class }); it('should create', () => { @@ -50,61 +77,60 @@ describe('LoginComponent', () => { it('should have a valid form on initialization', () => { fixture.detectChanges(); - expect(component.userForm).toBeTruthy(); + expect(component.userForm.valid).toBeFalse(); }); - it('should call login method when the form is submitted', () => { + it('should call login method when the form is submitted with valid data', fakeAsync(() => { fixture.detectChanges(); spyOn(component, 'login').and.callThrough(); const form = component.userForm; form.setValue({ email: 'test@example.com', password: 'password' }); - const submitButton = fixture.nativeElement.querySelector( - 'button[type="submit"]' - ); + const submitButton = fixture.nativeElement.querySelector('button[type="submit"]'); submitButton.click(); + tick(); expect(component.login).toHaveBeenCalled(); - }); + })); - it('should call alert when form is not valid', () => { + it('should call alert when form is submitted with invalid data', fakeAsync(() => { spyOn(component, 'login').and.callThrough(); - const alertSpy = spyOn(window, 'alert'); + const alertSpy = spyOn(alertService, 'showMessage'); fixture.detectChanges(); - const submitButton = fixture.nativeElement.querySelector( - 'button[type="submit"]' - ); + const submitButton = fixture.nativeElement.querySelector('button[type="submit"]'); submitButton.click(); + tick(); - expect(alertSpy).toHaveBeenCalledWith('Preencha todos os campos corretamente!'); - }); + expect(alertSpy).toHaveBeenCalledWith('info', 'Alerta', 'Preencha todos os campos corretamente!'); + })); it('should call navigator method when "Esqueceu a senha?" is clicked', () => { spyOn(component, 'navigator').and.callThrough(); - const forgotPasswordLink = - fixture.nativeElement.querySelector('.text-gray-400'); + const forgotPasswordLink = fixture.nativeElement.querySelector('.text-gray-400'); forgotPasswordLink.click(); expect(component.navigator).toHaveBeenCalledWith('/sendCodeResetPassword'); - }); it('should call navigator method when "Cadastre-se" is clicked', () => { spyOn(component, 'navigator').and.callThrough(); - const registerLink = - fixture.nativeElement.querySelector('.text-blue-brand'); + const registerLink = fixture.nativeElement.querySelector('.text-blue-brand'); registerLink.click(); expect(component.navigator).toHaveBeenCalledWith('/register'); }); - it('should call login and return an error', () => { + it('should call login and handle error', fakeAsync(() => { fixture.detectChanges(); const form = component.userForm; form.setValue({ email: 'test@example.com', password: 'password' }); - const mySpy = spyOn(authService, 'loginUser').and.returnValue(throwError(() => new Error('Erro'))); + + const mySpy = spyOn(authService, 'loginUser').and.returnValue(throwError({ error: { message: 'Erro' } })); + component.login(); + tick(); + expect(mySpy).toHaveBeenCalled(); - }); + })); });