Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ### auth.service.spec.ts
- import { LocalStorageService } from './local-storage.service';
- import { HttpModule } from '@angular/http';
- import { TestBed } from '@angular/core/testing';
- import { AuthService } from './auth.service';
- describe( 'AuthService', () => {
- let auth: AuthService;
- const Email = 'test@example.com';
- const Password = 'password';
- beforeEach(() => {
- TestBed.configureTestingModule( {
- imports: [ HttpModule ],
- providers: [ AuthService, LocalStorageService ]
- } );
- auth = TestBed.get( AuthService );
- } );
- it('isAuthenticated() should return true after login', () => {
- auth.login(Email, Password);
- expect(auth.isAuthenticated()).toBeTruthy();
- });
- });
- ### auth.service.ts
- @Injectable()
- export class AuthService implements OnInit {
- private user = null;
- private token = null;
- isAuthenticated(): boolean {
- return this.token;
- }
- login(email, password) {
- return this.http
- .post(this.BaseURL + this.LoginEndpoint, {
- email: email,
- password: password
- })
- .map(response => {
- const resp = response.json();
- const user = resp.data;
- const token = resp.meta.token;
- this.user = user;
- this.token = token;
- this.storageService.save('token', this.token);
- this.loginNotifier.next();
- return user;
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement