Skip to content

Commit

Permalink
chore: criado mais testes unitários para o login
Browse files Browse the repository at this point in the history
  • Loading branch information
BernardoSemiOficial committed Sep 28, 2024
1 parent 348f016 commit 8856ffb
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ import { Injectable } from '@angular/core';
})
export class ToastAlertServiceMock {
addDangerMessage() {}
addSuccessMessage() {}
}
1 change: 1 addition & 0 deletions frontend/src/__mocks__/services/user.service.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ export class UserServiceMock {
getProjectsForUser() {
return of({});
}
saveUser() {}
}
86 changes: 86 additions & 0 deletions frontend/src/app/features/login/login.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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: {
Expand All @@ -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'
});
});
});
});

0 comments on commit 8856ffb

Please sign in to comment.