Skip to content

Commit

Permalink
Adiciona testes
Browse files Browse the repository at this point in the history
  • Loading branch information
joao15victor08 committed Nov 7, 2023
1 parent 29576e7 commit 4b72eec
Show file tree
Hide file tree
Showing 8 changed files with 395 additions and 30 deletions.
67 changes: 64 additions & 3 deletions src/app/pages/active-account/active-account.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,87 @@ import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ActiveAccountComponent } from './active-account.component';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { AuthService } from 'src/app/services/auth.service';
import { ReactiveFormsModule } from '@angular/forms';
import { FormBuilder, ReactiveFormsModule } from '@angular/forms';
import { of, throwError } from 'rxjs';
import { RouterTestingModule } from '@angular/router/testing';
import { LoginComponent } from '../login/login.component';

const mockData: any = {
"email": "[email protected]",
"code": 123456,
}

class AuthServiceMock {
constructor() { }
activeAccount() {
return of({ success: true });
}
resendCode() {
return of({ success: true });
}
}

describe('ActiveAccountComponent', () => {
let component: ActiveAccountComponent;
let fixture: ComponentFixture<ActiveAccountComponent>;
let authService: AuthService;

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [HttpClientTestingModule, ReactiveFormsModule],
providers: [AuthService],
imports: [HttpClientTestingModule, RouterTestingModule.withRoutes(
[
{ path: 'login', component: LoginComponent },
]
), ReactiveFormsModule],
providers: [{ provide: AuthService, useValue: new AuthServiceMock() }, FormBuilder],
declarations: [ActiveAccountComponent]
})
.compileComponents();

fixture = TestBed.createComponent(ActiveAccountComponent);
component = fixture.componentInstance;
fixture.detectChanges();
authService = TestBed.inject(AuthService);
});

it('should create', () => {
expect(component).toBeTruthy();
});

it('should call activeAccount method when the form is submitted', () => {
fixture.detectChanges();
spyOn(component, 'activeAccount').and.callThrough();
const form = component.userForm;
form.setValue(mockData);

const submitButton = fixture.nativeElement.querySelector(
'button[type="submit"]'
);
submitButton.click();

expect(component.activeAccount).toHaveBeenCalled();
});

it('should call alert when form is not valid', () => {
spyOn(component, 'activeAccount').and.callThrough();
const alertSpy = spyOn(window, 'alert');
fixture.detectChanges();

const submitButton = fixture.nativeElement.querySelector(
'button[type="submit"]'
);
submitButton.click();

expect(alertSpy).toHaveBeenCalledWith('Preencha todos os campos corretamente!');
});

it('should call activeAccount and return an error', () => {
fixture.detectChanges();
const form = component.userForm;
form.setValue(mockData);
const mySpy = spyOn(authService, 'activeAccount').and.returnValue(throwError(() => new Error('Erro')));
component.activeAccount();
expect(mySpy).toHaveBeenCalled();
});

});
Original file line number Diff line number Diff line change
@@ -1,13 +1,40 @@

<div class="flex justify-center items-center h-4/5">
<form [formGroup]="userForm" class="text-center">
<div class="mb-4">
<input class="w-64 h-8 border rounded-lg border-gray-300 pl-4 md:w-96 md:h-10" type="email" formControlName="email" name="email" placeholder="E-mail" required>
<input
class="w-64 h-8 border rounded-lg border-gray-300 pl-4 md:w-96 md:h-10"
type="email"
formControlName="email"
name="email"
placeholder="E-mail"
required
/>
</div>
<div *ngIf="activeCode" class="mb-4">
<input class="w-64 h-8 border rounded-lg border-gray-300 pl-4 md:w-96 md:h-10" type="text" formControlName="code" name="code" placeholder="Código" required>
<input
class="w-64 h-8 border rounded-lg border-gray-300 pl-4 md:w-96 md:h-10"
type="text"
formControlName="code"
name="code"
placeholder="Código"
required
/>
</div>
<button *ngIf="!activeCode" class="p-2 bg-blue-brand rounded-lg justify-center mb-4 text-white" (click)="sendEmail()">Enviar código</button>
<button *ngIf="activeCode" class="p-2 bg-blue-brand rounded-lg justify-center mb-4 text-white" (click)="checkCode()">Trocar senha</button>
<button
id="sendEmail"
*ngIf="!activeCode"
class="p-2 bg-blue-brand rounded-lg justify-center mb-4 text-white"
(click)="sendEmail()"
>
Enviar código
</button>
<button
id="checkCode"
*ngIf="activeCode"
class="p-2 bg-blue-brand rounded-lg justify-center mb-4 text-white"
(click)="checkCode()"
>
Trocar senha
</button>
</form>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,128 @@ import { ComponentFixture, TestBed } from '@angular/core/testing';
import { CheckCodeRestPasswordComponent } from './check-code-rest-password.component';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { AuthService } from 'src/app/services/auth.service';
import { ReactiveFormsModule } from '@angular/forms';
import { FormBuilder, ReactiveFormsModule } from '@angular/forms';
import { of, throwError } from 'rxjs';
import { RouterTestingModule } from '@angular/router/testing';
import { ResetPasswordComponent } from '../reset-password/reset-password.component';

const mockData: any = {
"email": "[email protected]",
"code": 123456,
}

class AuthServiceMock {
constructor() { }
sendEmailPassword() {
return of({ success: true });
}
verifyCodePassword() {
return of({ success: true });
}
}

describe('CheckCodeRestPasswordComponent', () => {
let component: CheckCodeRestPasswordComponent;
let fixture: ComponentFixture<CheckCodeRestPasswordComponent>;
let authService: AuthService;

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [HttpClientTestingModule, ReactiveFormsModule],
providers: [AuthService],
imports: [HttpClientTestingModule, RouterTestingModule.withRoutes(
[
{ path: 'changePassword', component: ResetPasswordComponent },
]
), ReactiveFormsModule],
providers: [{ provide: AuthService, useValue: new AuthServiceMock() }, FormBuilder],
declarations: [CheckCodeRestPasswordComponent]
})
.compileComponents();

fixture = TestBed.createComponent(CheckCodeRestPasswordComponent);
component = fixture.componentInstance;
fixture.detectChanges();
authService = TestBed.inject(AuthService);
});

it('should create', () => {
expect(component).toBeTruthy();
});

it('should call sendEmail method when the form is submitted', () => {
fixture.detectChanges();
spyOn(component, 'sendEmail').and.callThrough();
const form = component.userForm;
form.setValue(mockData);

const submitButton = fixture.nativeElement.querySelector(
'#sendEmail'
);
submitButton.click();

expect(component.sendEmail).toHaveBeenCalled();
});

it('should call alert when form is not valid', () => {
fixture.detectChanges();
spyOn(component, 'sendEmail').and.callThrough();
const alertSpy = spyOn(window, 'alert');
const form = component.userForm;
form.setValue({ "email": '', code: '' });

const submitButton = fixture.nativeElement.querySelector(
'#sendEmail'
);
submitButton.click();

expect(alertSpy).toHaveBeenCalledWith('Preencha todos os campos corretamente!');
});

it('should call sendEmail and return an error', () => {
fixture.detectChanges();
const form = component.userForm;
form.setValue(mockData);
const mySpy = spyOn(authService, 'sendEmailPassword').and.returnValue(throwError(() => new Error('Erro')));
component.sendEmail();
expect(mySpy).toHaveBeenCalled();
});

it('should call checkCode method when the form is submitted', () => {
fixture.detectChanges();
spyOn(component, 'checkCode').and.callThrough();
const form = component.userForm;
form.setValue(mockData);
component.activeCode = true;
fixture.detectChanges();

const submitButton = fixture.nativeElement.querySelector(
'#checkCode'
);
submitButton.click();

expect(component.checkCode).toHaveBeenCalled();
});

it('should call alert when form is not valid', () => {
fixture.detectChanges();
spyOn(component, 'checkCode').and.callThrough();
const alertSpy = spyOn(window, 'alert');
component.activeCode = true;
fixture.detectChanges();
const submitButton = fixture.nativeElement.querySelector(
'#checkCode'
);
submitButton.click();

expect(alertSpy).toHaveBeenCalledWith('Preencha todos os campos corretamente!');
});

it('should call checkCode and return an error', () => {
fixture.detectChanges();
const form = component.userForm;
form.setValue(mockData);
const mySpy = spyOn(authService, 'verifyCodePassword').and.returnValue(throwError(() => new Error('Erro')));
component.checkCode();
expect(mySpy).toHaveBeenCalled();
});

});
67 changes: 64 additions & 3 deletions src/app/pages/edit-user/edit-user.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,86 @@ import { ComponentFixture, TestBed } from '@angular/core/testing';
import { EditUserComponent } from './edit-user.component';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { ReactiveFormsModule } from '@angular/forms';
import { FormBuilder, ReactiveFormsModule } from '@angular/forms';
import { of, throwError } from 'rxjs';
import { UserService } from 'src/app/services/user.service';
import { ProfileComponent } from '../profile/profile.component';

const mockData: any = {
"name": "Mario",
"email": "[email protected]",
"connection": "ALUNO",
}

class UserServiceMock {
constructor() { }
updateUser() {
return of({ success: true });
}
}

describe('EditUserComponent', () => {
let component: EditUserComponent;
let fixture: ComponentFixture<EditUserComponent>;
let userService: UserService;

beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [HttpClientTestingModule, RouterTestingModule, ReactiveFormsModule],
declarations: [EditUserComponent]
imports: [HttpClientTestingModule, RouterTestingModule.withRoutes(
[
{ path: 'profile', component: ProfileComponent },
]
), ReactiveFormsModule],
declarations: [EditUserComponent],
providers: [{ provide: UserService, useValue: new UserServiceMock() }, FormBuilder],
})
.compileComponents();

fixture = TestBed.createComponent(EditUserComponent);
component = fixture.componentInstance;
fixture.detectChanges();
userService = TestBed.inject(UserService);
});

it('should create', () => {
expect(component).toBeTruthy();
});

it('should call updateUser method when the form is submitted', () => {
fixture.detectChanges();
spyOn(component, 'updateUser').and.callThrough();
const form = component.userForm;
form.setValue(mockData);
component.userId = mockData.id;

const submitButton = fixture.nativeElement.querySelector(
'button[type="submit"]'
);
submitButton.click();

expect(component.updateUser).toHaveBeenCalled();
});

it('should call alert when form is not valid', () => {
spyOn(component, 'updateUser').and.callThrough();
const alertSpy = spyOn(window, 'alert');
fixture.detectChanges();

const submitButton = fixture.nativeElement.querySelector(
'button[type="submit"]'
);
submitButton.click();

expect(alertSpy).toHaveBeenCalledWith('Preencha todos os campos corretamente!');
});

it('should call updateUser and return an error', () => {
fixture.detectChanges();
const form = component.userForm;
form.setValue(mockData);
const mySpy = spyOn(userService, 'updateUser').and.returnValue(throwError(() => new Error('Erro')));
component.updateUser();
expect(mySpy).toHaveBeenCalled();
});

});
Loading

0 comments on commit 4b72eec

Please sign in to comment.