import { FcException , HttpException , RpcException } from './exceptions' ; import { ExceptionsService } from './exceptions.service' ; describe ( 'ExceptionsService' , ( ) => { describe ( 'addLeadingZeros' , ( ) => { it ( 'Should return a string' , ( ) => { const value = 1 ; const length = 4 ; const result = ExceptionsService [ 'addLeadingZeros' ] ( value , length ) ; expect ( typeof result ) . toBe ( 'string' ) ; } ) ; it ( 'should return the correct length' , ( ) => { const value = 1 ; const length = 4 ; const result = ExceptionsService [ 'addLeadingZeros' ] ( value , length ) ; expect ( result ) . toHaveLength ( length ) ; } ) ; it ( 'should return the correct value' , ( ) => { const value = 1 ; const length = 4 ; const result = ExceptionsService [ 'addLeadingZeros' ] ( value , length ) ; expect ( result ) . toBe ( '0001' ) ; } ) ; } ) ; describe ( 'getCode' , ( ) => { it ( 'Should return a string' , ( ) => { const scope = 1 ; const code = 2 ; const result = ExceptionsService [ 'getCode' ] ( scope , code ) ; expect ( typeof result ) . toBe ( 'string' ) ; } ) ; it ( 'Should return the correct value for 1 digit code and 1 digit scope' , ( ) => { const scope = 1 ; const code = 2 ; const result = ExceptionsService [ 'getCode' ] ( scope , code ) ; expect ( result ) . toBe ( 'Y010002' ) ; } ) ; it ( 'Should return the correct value for 2 digits code and 2 digits scope' , ( ) => { const scope = 23 ; const code = 12 ; const result = ExceptionsService [ 'getCode' ] ( scope , code ) ; expect ( result ) . toBe ( 'Y230012' ) ; } ) ; it ( 'Should return the correct value for 3 digits code and 1 digits scope' , ( ) => { const scope = 3 ; const code = 421 ; const result = ExceptionsService [ 'getCode' ] ( scope , code ) ; expect ( result ) . toBe ( 'Y030421' ) ; } ) ; } ) ; describe ( 'getExceptionCodeFor' , ( ) => { it ( 'should return the correct value from exception FcException' , ( ) => { const exception = new FcException ( ) ; exception . scope = 2 ; exception . code = 23 ; const result = ExceptionsService . getExceptionCodeFor < FcException > ( exception ) ; expect ( result ) . toBe ( 'Y020023' ) ; } ) ; it ( 'should return the correct value from exception HttpException' , ( ) => { const exception = new HttpException ( 'Not found' , 404 ) ; const result = ExceptionsService . getExceptionCodeFor < HttpException > ( exception ) ; expect ( result ) . toBe ( 'Y000404' ) ; } ) ; it ( 'should return the correct value from exception RpcException' , ( ) => { const exception = new RpcException ( 'Foo' ) ; const result = ExceptionsService . getExceptionCodeFor < RpcException > ( exception ) ; expect ( result ) . toBe ( 'Y000000' ) ; } ) ; it ( 'should return the correct value from exception unknown' , ( ) => { const result = ExceptionsService . getExceptionCodeFor < Error > ( ) ; expect ( result ) . toBe ( 'Y000000' ) ; } ) ; it ( 'Should call private method getCode' , ( ) => { const getCodeMock = jest . spyOn ( ExceptionsService as any , 'getCode' ) . mockReturnValueOnce ( 'getCode return value' ) ; const result = ExceptionsService . getExceptionCodeFor < Error > ( ) ; expect ( getCodeMock ) . toHaveBeenCalledTimes ( 1 ) ; expect ( getCodeMock ) . toHaveBeenCalledWith ( 0 , 0 ) ; expect ( result ) . toBe ( 'getCode return value' ) ; } ) ; } ) ; } ) ;