import { APIClient } from '@wireapp/api-client' ; import UUID from 'uuidjs' ; import { AssetService } from './AssetService' ; describe ( 'AssetService' , ( ) => { describe ( '"uploadAsset"' , ( ) => { it ( 'builds an encrypted asset payload' , async ( ) => { const apiClient = new APIClient ( ) ; const assetService = new AssetService ( apiClient ) ; const assetServerData = { key : `3-2-${ UUID . genV4 ( ) . toString ( ) }` , token : UUID . genV4 ( ) . toString ( ) , expires : '' , } ; spyOn ( apiClient . asset . api , 'postAsset' ) . and . returnValue ( { cancel : ( ) => { } , response : Promise . resolve ( assetServerData ) , } ) ; const asset = await ( await assetService . uploadAsset ( Buffer . from ( [ 1 , 2 , 3 ] ) ) ) . response ; expect ( asset ) . toEqual ( jasmine . objectContaining ( { key : assetServerData . key , keyBytes : jasmine . any ( Buffer ) , sha256 : jasmine . any ( Buffer ) , token : assetServerData . token , } ) ) ; } ) ; it ( 'allows cancelling asset upload' , async ( ) => { const apiClient = new APIClient ( ) ; const assetService = new AssetService ( apiClient ) ; const apiUpload = { cancel : jasmine . createSpy ( ) , response : Promise . resolve ( { } as any ) , } ; spyOn ( apiClient . asset . api , 'postAsset' ) . and . returnValue ( apiUpload ) ; const asset = await assetService . uploadAsset ( Buffer . from ( [ 1 , 2 , 3 ] ) ) ; asset . cancel ( ) ; expect ( apiUpload . cancel ) . toHaveBeenCalled ( ) ; } ) ; it ( 'exposes upload progress' , async ( ) => { const apiClient = new APIClient ( ) ; const assetService = new AssetService ( apiClient ) ; const apiUpload = { cancel : jasmine . createSpy ( ) , response : Promise . resolve ( { } as any ) , } ; spyOn ( apiClient . asset . api , 'postAsset' ) . and . callFake ( ( _asset , _options , progressCallback ) => { progressCallback ?. ( 1 ) ; return apiUpload ; } ) ; const onProgress = jasmine . createSpy ( ) ; await assetService . uploadAsset ( Buffer . from ( [ 1 , 2 , 3 ] ) , { } , onProgress ) ; expect ( onProgress ) . toHaveBeenCalledWith ( 1 ) ; } ) ; } ) ; } ) ;