import { HttpModule } from '@angular/http'; import { AppConfig } from './../app-config.interface'; import { APP_CONFIG } from './../app-config'; import { TestBed, inject, async } from '@angular/core/testing'; import { UserService } from './user.service'; describe('UserService', () => { const user = 'test@test.com'; const password = 'test'; beforeEach(() => { const TEST_CONFIG: AppConfig = { apiEndpoint: 'http://192.168.0.172:5000/api', title: 'Byteflies' }; TestBed.configureTestingModule({ providers: [ UserService, { provide: APP_CONFIG, useValue: TEST_CONFIG }, ], imports: [ HttpModule ] }); }); it('should login and return a token', async(inject([UserService], (service: UserService) => { return service.login(user, password) .then(result => expect(typeof result).toBe('string')); }))); it('should have a token after login', async(inject([UserService], (service: UserService) => { return service.login(user, password) .then(_ => service.token); }))); it('should not be logged in after logout', async(inject([UserService], (service: UserService) => { return service.login(user, password) .then(_ => { expect(service.loggedIn()); expect(typeof service.token).toBe('string'); expect(service.token.length).toBeGreaterThan(0, 'service.token.length'); expect(String(service.id).length).toBeGreaterThan(0, 'service.id.length'); service.logout(); expect(!!service.token).toBe(false); expect(service.loggedIn()).toBe(false); }); }))); });