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 b6108c5 commit 5e655e2
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 12 deletions.
17 changes: 9 additions & 8 deletions src/app/pages/login/login.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,14 @@ describe('LoginComponent', () => {

expect(component.navigator).toHaveBeenCalledWith('/sendCodeResetPassword');

it('should call navigator method when "Cadastre-se" is clicked', () => {
spyOn(component, 'navigator');
const registerLink =
fixture.nativeElement.querySelector('.text-blue-brand');
registerLink.click();

expect(component.navigator).toHaveBeenCalledWith('/register');
});
});

it('should call navigator method when "Cadastre-se" is clicked', () => {
spyOn(component, 'navigator');
const registerLink =
fixture.nativeElement.querySelector('.text-blue-brand');
registerLink.click();

expect(component.navigator).toHaveBeenCalledWith('/register');
});
});
21 changes: 20 additions & 1 deletion src/app/services/auth.guard.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,35 @@
import { TestBed } from '@angular/core/testing';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { AuthGuard } from './auth.guard';
import { RouterTestingModule } from '@angular/router/testing';
import { LoginComponent } from '../pages/login/login.component';

describe('AuthGuard', () => {
let guard: AuthGuard;

beforeEach(() => {
TestBed.configureTestingModule({ imports: [HttpClientTestingModule], providers: [AuthGuard] });
TestBed.configureTestingModule({
imports: [HttpClientTestingModule, RouterTestingModule.withRoutes(
[{ path: 'login', component: LoginComponent }]
)], providers: [AuthGuard], declarations: [LoginComponent]
});
guard = TestBed.inject(AuthGuard);
});

it('should be created', () => {
expect(guard).toBeTruthy();
});

it('should return true for a logged in user', () => {
localStorage.setItem('token', 'testtoken');
expect(guard.canActivate()).toBe(true);
}
);

it('should return false for a logged out user', () => {
localStorage.removeItem('token');
expect(guard.canActivate()).toBe(false);
}
);

});
94 changes: 92 additions & 2 deletions src/app/services/user.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,106 @@
import { TestBed } from '@angular/core/testing';
import { UserService } from './user.service';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { HttpClientModule } from '@angular/common/http';

describe('UserService', () => {
let service: UserService;
let httpMock: HttpTestingController;

beforeEach(() => {
TestBed.configureTestingModule({ imports: [HttpClientTestingModule], providers: [UserService] });
TestBed.configureTestingModule({ imports: [HttpClientTestingModule, HttpClientModule], providers: [UserService] });
service = TestBed.inject(UserService);
httpMock = TestBed.inject(HttpTestingController);
});

it('should be created', () => {
expect(service).toBeTruthy();
});

it('should return a single user', () => {
const userResponse: any = {
"id": 1,
"name": "Mario",
"connection": "ALUNO",
"email": "[email protected]",
"role": "USER",
"is_active": true
}
service.getUser(1).subscribe(res => {
expect(res).toEqual(userResponse);

});
const req = httpMock.expectOne(`${service.apiURL}/users/1`);
expect(req.request.method).toBe('GET');
req.flush(userResponse);
});

it('should return a list of users', async () => {
const userResponse: any = [
{
"id": 1,
"name": "Mario",
"connection": "ALUNO",
"email": "[email protected]",
"role": "USER",
"is_active": true
},
{
"id": 2,
"name": "Luigi",
"connection": "ALUNO",
"email": "[email protected]",
"role": "USER",
"is_active": true
}];
service.getAllUsers().subscribe(res => {
expect(res).toEqual(userResponse);
});
const req = httpMock.expectOne(`${service.apiURL}/users`);
expect(req.request.method).toBe('GET');
req.flush(userResponse);
}
);

it('should update a user', () => {
const userResponse: any = {
"id": 1,
"name": "Mario",
"connection": "ALUNO",
"email": "[email protected]",
"role": "USER",
"is_active": true
}
service.updateUser(1, { "name": "Mario", "connection": "PROFESSOR", "email": "[email protected]" }).subscribe(res => {
expect(res).toEqual(userResponse);
});
const req = httpMock.expectOne(`${service.apiURL}/users/1`);
expect(req.request.method).toBe('PATCH');
req.flush(userResponse);
}
);

it('should delete a user', () => {
const userResponse: any = {
"id": 1,
"name": "Mario",
"connection": "ALUNO",
"email": "[email protected]",
"role": "USER",
"is_active": true
}
service.deleteUser(1).subscribe(res => {
expect(res).toEqual(userResponse);
});
const req = httpMock.expectOne(`${service.apiURL}/users/1`);
expect(req.request.method).toBe('DELETE');
req.flush(userResponse);
}
);

afterEach(() => {
httpMock.verify();
}
);

});
2 changes: 1 addition & 1 deletion src/app/services/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Observable } from 'rxjs';
providedIn: 'root'
})
export class UserService {
private apiURL = environment.apiURL;
public apiURL = environment.apiURL;

constructor(private http: HttpClient) { }

Expand Down

0 comments on commit 5e655e2

Please sign in to comment.