diff --git a/src/app/pages/login/login.component.spec.ts b/src/app/pages/login/login.component.spec.ts index e03216cb..381ddc62 100644 --- a/src/app/pages/login/login.component.spec.ts +++ b/src/app/pages/login/login.component.spec.ts @@ -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'); }); }); diff --git a/src/app/services/auth.guard.spec.ts b/src/app/services/auth.guard.spec.ts index 81d1c094..b09a90ef 100644 --- a/src/app/services/auth.guard.spec.ts +++ b/src/app/services/auth.guard.spec.ts @@ -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); + } + ); + }); diff --git a/src/app/services/user.service.spec.ts b/src/app/services/user.service.spec.ts index bd802828..cc2d4167 100644 --- a/src/app/services/user.service.spec.ts +++ b/src/app/services/user.service.spec.ts @@ -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": "mario@gmail.com", + "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": "mario@example.com", + "role": "USER", + "is_active": true + }, + { + "id": 2, + "name": "Luigi", + "connection": "ALUNO", + "email": "luigi@example.com", + "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": "mario@gmail.com", + "role": "USER", + "is_active": true + } + service.updateUser(1, { "name": "Mario", "connection": "PROFESSOR", "email": "mario@gmail.com" }).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": "mario@gmail.com", + "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(); + } + ); + }); diff --git a/src/app/services/user.service.ts b/src/app/services/user.service.ts index 3ffdd629..bb9279f9 100644 --- a/src/app/services/user.service.ts +++ b/src/app/services/user.service.ts @@ -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) { }