Advertisement
Guest User

Untitled

a guest
Aug 27th, 2017
1,027
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. ### auth.service.spec.ts
  2.  
  3. import { LocalStorageService } from './local-storage.service';
  4. import { HttpModule } from '@angular/http';
  5. import { TestBed } from '@angular/core/testing';
  6.  
  7. import { AuthService } from './auth.service';
  8.  
  9. describe( 'AuthService', () => {
  10. let auth: AuthService;
  11. const Email = 'test@example.com';
  12. const Password = 'password';
  13.  
  14. beforeEach(() => {
  15. TestBed.configureTestingModule( {
  16. imports: [ HttpModule ],
  17. providers: [ AuthService, LocalStorageService ]
  18. } );
  19. auth = TestBed.get( AuthService );
  20. } );
  21.  
  22. it('isAuthenticated() should return true after login', () => {
  23. auth.login(Email, Password);
  24. expect(auth.isAuthenticated()).toBeTruthy();
  25. });
  26. });
  27.  
  28.  
  29. ### auth.service.ts
  30.  
  31. @Injectable()
  32. export class AuthService implements OnInit {
  33. private user = null;
  34. private token = null;
  35.  
  36. isAuthenticated(): boolean {
  37. return this.token;
  38. }
  39.  
  40. login(email, password) {
  41. return this.http
  42. .post(this.BaseURL + this.LoginEndpoint, {
  43. email: email,
  44. password: password
  45. })
  46. .map(response => {
  47. const resp = response.json();
  48. const user = resp.data;
  49. const token = resp.meta.token;
  50. this.user = user;
  51. this.token = token;
  52. this.storageService.save('token', this.token);
  53. this.loginNotifier.next();
  54. return user;
  55. });
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement