import { mocked } from 'jest-mock' ; import { lastValueFrom } from 'rxjs' ; import { HttpService } from '@nestjs/axios' ; import { Test , TestingModule } from '@nestjs/testing' ; import { BridgePayload , BridgeResponse } from '@fc/hybridge-http-proxy' ; import { LoggerService } from '@fc/logger' ; import { CsmrHttpProxyService } from './csmr-http-proxy.service' ; jest . mock ( 'rxjs' ) ; describe ( 'CsmrHttpProxyService' , ( ) => { let service ; const loggerService = { setContext : jest . fn ( ) , debug : jest . fn ( ) , trace : jest . fn ( ) , } ; const httpService = { get : jest . fn ( ) , post : jest . fn ( ) , } ; const lastValueMock = mocked ( lastValueFrom ) ; const httpResponseMock = Symbol ( 'httpResponseMock' ) ; beforeEach ( async ( ) => { jest . resetAllMocks ( ) ; jest . restoreAllMocks ( ) ; const module = await Test . createTestingModule ( { providers : [ LoggerService , HttpService , CsmrHttpProxyService ] , } ) . overrideProvider ( LoggerService ) . useValue ( loggerService ) . overrideProvider ( HttpService ) . useValue ( httpService ) . compile ( ) ; service = module . get < CsmrHttpProxyService > ( CsmrHttpProxyService ) ; } ) ; it ( 'should be defined' , ( ) => { expect ( service ) . toBeDefined ( ) ; } ) ; it ( 'should call logger at init' , ( ) => { expect ( loggerService . setContext ) . toHaveBeenCalledTimes ( 1 ) ; expect ( loggerService . setContext ) . toHaveBeenCalledWith ( service . constructor . name ) ; } ) ; describe ( 'forwardRequest()' , ( ) => { const baseResponseMock = Object . freeze ( { status : 200 , statusText : 'statusTextValue' , headers : { 'content-type' : 'text/html; charset=UTF-8' , } , } ) ; const resultMock = { headers : { 'content-type' : 'text/html; charset=UTF-8' , } , statusText : 'statusTextValue' , status : 200 , data : undefined , } ; const options = Object . freeze ( { url : 'http://test.com/token?code=33EFGRGRG44556GH6J' , method : 'get' , headers : { hello : 'world' , } , data : null , } ) ; beforeEach ( ( ) => { httpService . get . mockResolvedValueOnce ( httpResponseMock ) ; httpService . post . mockResolvedValueOnce ( httpResponseMock ) ; } ) ; it ( 'should resolve without data for given options' , async ( ) => { const requestMock = [ 'http://test.com/token?code=33EFGRGRG44556GH6J' , { headers : { hello : 'world' } , } , ] ; lastValueMock . mockResolvedValueOnce ( baseResponseMock ) ; const result = await service . forwardRequest ( options ) ; expect ( result ) . toStrictEqual ( resultMock ) ; expect ( httpService . get ) . toHaveBeenCalledTimes ( 1 ) ; expect ( httpService . get ) . toHaveBeenCalledWith ( ... requestMock ) ; } ) ; it ( 'should resolve with data for given options' , async ( ) => { const mockResponse = { ... baseResponseMock , data : { hello : 'world' , } , } ; const mockOptions = { ... options , method : 'post' , data : "{\nexemple: 'example value'\n}" , } ; const requestMock = [ 'http://test.com/token?code=33EFGRGRG44556GH6J' , "{\nexemple: 'example value'\n}" , { headers : { hello : 'world' } , } , ] ; lastValueMock . mockResolvedValueOnce ( mockResponse ) ; const result = await service . forwardRequest ( mockOptions ) ; expect ( result ) . toStrictEqual ( mockResponse ) ; expect ( httpService . post ) . toHaveBeenCalledTimes ( 1 ) ; expect ( httpService . post ) . toHaveBeenCalledWith ( ... requestMock ) ; } ) ; it ( 'should call lastValueFrom when request is called' , async ( ) => { lastValueMock . mockResolvedValueOnce ( baseResponseMock ) ; await service . forwardRequest ( options ) ; expect ( lastValueMock ) . toHaveBeenCalledTimes ( 1 ) ; expect ( lastValueMock ) . toHaveBeenCalledWith ( Promise . resolve ( httpResponseMock ) ) ; } ) ; it ( 'should throw an error if request failed' , async ( ) => { const errorMock = new Error ( 'Unknown Error' ) ; lastValueMock . mockImplementationOnce ( ( ) => { throw errorMock ; } ) ; await expect ( service . forwardRequest ( options ) ) . rejects . toThrow ( errorMock ) ; } ) ; } ) ; } ) ;