import { TestBed } from '@angular/core/testing' ; import { HttpClientTestingModule , HttpTestingController } from '@angular/common/http/testing' ; import { PhotoService } from '@core/services/photo.service' ; import { Photo } from '@core/models' ; import { API_URL } from '@core/tokens' ; import { HttpErrorResponse } from '@angular/common/http' ; describe ( 'PhotoService' , ( ) => { let photoService ; let httpMock ; const apiUrl = '/api' ; beforeEach ( ( ) => { TestBed . configureTestingModule ( { imports : [ HttpClientTestingModule ] , providers : [ { provide : API_URL , useValue : apiUrl } , PhotoService ] } ) ; photoService = TestBed . inject ( PhotoService ) ; httpMock = TestBed . inject ( HttpTestingController ) ; } ) ; afterEach ( ( ) => httpMock . verify ( ) ) ; it ( 'should be created' , ( ) => { expect ( photoService ) . toBeTruthy ( ) ; } ) ; describe ( '#uploadPhoto' , ( ) => { const testPhoto = { id : 1 , url : 'http://test.com' , description : 'test' , addedAt : new Date ( ) , isMain : true } ; const userId = 1 ; const path = `${ apiUrl }/users/${ userId }/photos` ; it ( 'should return a photo, empty object or null' , ( done ) => { photoService . uploadPhoto ( testPhoto , userId ) . subscribe ( res => { expect ( res ) . toEqual ( testPhoto ) ; } ) ; photoService . uploadPhoto ( { } , userId ) . subscribe ( res => { expect ( res ) . toEqual ( { } ) ; expect ( res . description ) . toBe ( undefined ) ; } ) ; photoService . uploadPhoto ( null , userId ) . subscribe ( res => { expect ( res ) . toEqual ( null ) ; done ( ) ; } ) ; const requests = httpMock . match ( path ) ; expect ( requests [ 0 ] . request . method ) . toEqual ( 'POST' ) ; requests [ 0 ] . flush ( testPhoto ) ; requests [ 1 ] . flush ( { } ) ; requests [ 2 ] . flush ( null ) ; } ) ; it ( 'should return an error message' , ( ) => { const errorMessage = 'Error!!!' ; photoService . uploadPhoto ( { } , userId ) . subscribe ( res => { expect ( res ) . toEqual ( { } ) ; } , ( error ) => { expect ( error . status ) . toEqual ( 404 ) ; expect ( error . statusText ) . toEqual ( 'Not Found' ) ; expect ( error . error ) . toEqual ( errorMessage ) ; } ) ; const req = httpMock . expectOne ( path ) ; req . flush ( errorMessage , { status : 404 , statusText : 'Not Found' } ) ; } ) ; } ) ; describe ( '#setMainPhoto' , ( ) => { const userId = 1 ; const photoId = 1 ; const path = `${ apiUrl }/users/${ userId }/photos/${ photoId }/setMain` ; it ( 'should return empty object' , ( ) => { photoService . setMainPhoto ( userId , photoId ) . subscribe ( res => { expect ( res ) . toEqual ( { } ) ; } ) ; const req = httpMock . expectOne ( path ) ; expect ( req . request . method ) . toEqual ( 'POST' ) ; req . flush ( { } ) ; } ) ; } ) ; describe ( '#deletePhoto' , ( ) => { const path = `${ apiUrl }/users/${ 1 }/photos/${ 1 }` ; it ( 'should return empty object (void)' , ( ) => { photoService . deletePhoto ( 1 , 1 ) . subscribe ( res => expect ( res ) . toEqual ( { } ) ) ; const req = httpMock . expectOne ( path ) ; expect ( req . request . method ) . toEqual ( 'DELETE' ) ; req . flush ( { } ) ; } ) ; } ) ; } ) ;