-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b6108c5
commit 5e655e2
Showing
4 changed files
with
122 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters