import { HttpTestingController , HttpClientTestingModule , } from '@angular/common/http/testing' ; import { TestBed } from '@angular/core/testing' ; import { DaffAuthToken , DaffAccountRegistration , } from '@daffodil/auth' ; import { DaffAccountRegistrationFactory , DaffAuthTokenFactory , } from '@daffodil/auth/testing' ; import { DaffInMemoryLoginService } from './login.service' ; describe ( 'Driver | InMemory | Auth | LoginService' , ( ) => { let loginService ; let httpMock ; const registrationFactory = new DaffAccountRegistrationFactory ( ) ; const authFactory = new DaffAuthTokenFactory ( ) ; let token ; let email ; let password ; let firstName ; let lastName ; let mockRegistration ; let mockAuth ; beforeEach ( ( ) => { TestBed . configureTestingModule ( { imports : [ HttpClientTestingModule , ] , providers : [ DaffInMemoryLoginService , ] , } ) ; httpMock = TestBed . inject ( HttpTestingController ) ; loginService = TestBed . inject ( DaffInMemoryLoginService ) ; mockRegistration = registrationFactory . create ( ) ; mockAuth = authFactory . create ( ) ; token = mockAuth . token ; firstName = mockRegistration . customer . firstName ; lastName = mockRegistration . customer . lastName ; email = mockRegistration . customer . email ; password = mockRegistration . password ; } ) ; it ( 'should be created' , ( ) => { expect ( loginService ) . toBeTruthy ( ) ; } ) ; describe ( 'login | getting a token' , ( ) => { afterEach ( ( ) => { httpMock . verify ( ) ; } ) ; it ( 'should send a post request and return an AuthToken' , done => { loginService . login ( { email , password } ) . subscribe ( auth => { expect ( auth ) . toEqual ( mockAuth ) ; done ( ) ; } ) ; const req = httpMock . expectOne ( `${ loginService . url }login` ) ; expect ( req . request . method ) . toBe ( 'POST' ) ; expect ( req . request . body ) . toEqual ( { email , password } ) ; req . flush ( mockAuth ) ; } ) ; } ) ; describe ( 'logout | getting an empty Observable' , ( ) => { afterEach ( ( ) => { httpMock . verify ( ) ; } ) ; it ( 'should send a post request and return an empty Observable' , done => { loginService . logout ( ) . subscribe ( resp => { expect ( resp ) . toBeUndefined ( ) ; done ( ) ; } ) ; const req = httpMock . expectOne ( `${ loginService . url }logout` ) ; expect ( req . request . method ) . toBe ( 'POST' ) ; req . flush ( { success : true } ) ; } ) ; } ) ; } ) ;