import { ModuleRef } from '@nestjs/core' ; import { Instantiable } from '@fc/common' ; import { FeatureHandlerEmptyException , FeatureHandlerUnregisteredException , } from '../exceptions' ; import { FeatureHandlerNoHandler } from '../handlers' ; import { IFeatureHandler } from '../interfaces' ; import { FeatureHandler } from './feature-handler.decorator' ; describe ( 'FeatureHandler exception decorator' , ( ) => { let featureHandlerServiceMock ; let ctx ; beforeEach ( ( ) => { FeatureHandler . getInternalMappingForTestingPurposes ( ) . clear ( ) ; featureHandlerServiceMock = { handle : jest . fn ( ) , } as unknown as Instantiable < IFeatureHandler > ; ctx = { moduleRef : { get : jest . fn ( ) , } , } ; } ) ; describe ( 'get()' , ( ) => { it ( 'should retrieve a `FeatureHandlerNoHandler` response for a `null` featureHandler request' , async ( ) => { const resultMock = new FeatureHandlerNoHandler ( ) ; const result = FeatureHandler . get ( null , ctx ) ; expect ( result ) . toStrictEqual ( resultMock ) ; } ) ; it ( 'should throw an `Error` if an empty string as key is requested' , async ( ) => { const emptyFeatureHandlerTopicGetMock = '' ; expect ( ( ) => FeatureHandler . get ( emptyFeatureHandlerTopicGetMock , ctx ) ) . toThrow ( FeatureHandlerEmptyException ) ; } ) ; it ( 'should throw an `Error` if an `undefined` featureHandler is requested' , async ( ) => { const emptyFeatureHandlerMock = undefined ; expect ( ( ) => FeatureHandler . get ( emptyFeatureHandlerMock , ctx ) ) . toThrow ( FeatureHandlerEmptyException ) ; } ) ; it ( 'should retrieve a instantiated class for a given existing featureHandler' , async ( ) => { const featureHandlerServiceMock = class { async handle ( ) { return ; } } ; const featureHandlerTopicMock = 'core-fcp-eidas-verify' ; FeatureHandler . getInternalMappingForTestingPurposes ( ) . set ( featureHandlerTopicMock , featureHandlerServiceMock ) ; ctx . moduleRef . get . mockResolvedValueOnce ( featureHandlerServiceMock ) ; const result = await FeatureHandler . get ( featureHandlerTopicMock , ctx as unknown as { moduleRef : ModuleRef ; } ) ; expect ( result ) . toStrictEqual ( featureHandlerServiceMock ) ; } ) ; it ( 'should failed if a undefined feature handler is requested' , ( ) => { expect . assertions ( 1 ) ; const emptyKey = undefined ; expect ( ( ) => FeatureHandler . get ( emptyKey , ctx as unknown as { moduleRef : ModuleRef ; } ) ) . toThrow ( FeatureHandlerEmptyException ) ; } ) ; it ( 'should failed if a empty feature handler was registered' , ( ) => { expect . assertions ( 1 ) ; const emptyResponseMock = undefined ; const featureHandlerFakeTopicMock = 'doesnt-exists' ; FeatureHandler . getInternalMappingForTestingPurposes ( ) . set ( featureHandlerFakeTopicMock , emptyResponseMock ) ; expect ( ( ) => FeatureHandler . get ( featureHandlerFakeTopicMock , ctx as unknown as { moduleRef : ModuleRef ; } ) ) . toThrow ( FeatureHandlerUnregisteredException ) ; } ) ; } ) ; describe ( 'getAll()' , ( ) => { it ( 'should retrieve the mapping list of all setted FeatureHandler decorator' , async ( ) => { const featureHandlerTopicMock = 'core-fcp-eidas-verify' ; FeatureHandler . getInternalMappingForTestingPurposes ( ) . set ( featureHandlerTopicMock , featureHandlerServiceMock ) ; const resultKeysMock = [ featureHandlerTopicMock ] ; const result = FeatureHandler . getAll ( ) ; expect ( result ) . toStrictEqual ( resultKeysMock ) ; } ) ; } ) ; } ) ;