import { ClientProxy , ClientProxyFactory , Transport , } from '@nestjs/microservices' ; import { RabbitmqModule } from './rabbitmq.module' ; describe ( 'RabbitmqModule' , ( ) => { const configServiceMock = { get : jest . fn ( ) , } ; const configServiceReturnValue = Symbol ( 'configServiceReturnValue' ) ; beforeEach ( ( ) => { jest . resetAllMocks ( ) ; configServiceMock . get . mockReturnValue ( configServiceReturnValue ) ; } ) ; describe ( 'registerFor' , ( ) => { it ( 'should return a module declaration' , ( ) => { const moduleNameMock = 'Foobar' ; const result = RabbitmqModule . registerFor ( moduleNameMock ) ; expect ( result ) . toBeInstanceOf ( Object ) ; expect ( result ) . toHaveProperty ( 'module' ) ; expect ( result ) . toHaveProperty ( 'providers' ) ; expect ( result ) . toHaveProperty ( 'exports' ) ; } ) ; it ( 'should return a module with a provider for "<moduleName>Broker"' , ( ) => { const moduleNameMock = 'Foobar' ; const module = RabbitmqModule . registerFor ( moduleNameMock ) ; const result = module . providers [ 0 ] as any ; expect ( result ) . toHaveProperty ( 'provide' ) ; expect ( result . provide ) . toBe ( 'FoobarBroker' ) ; } ) ; it ( 'should return a module with a provider having a useFactory method' , ( ) => { const moduleNameMock = 'Foobar' ; const module = RabbitmqModule . registerFor ( moduleNameMock ) ; const result = module . providers [ 0 ] as any ; expect ( result ) . toHaveProperty ( 'useFactory' ) ; expect ( result . useFactory ) . toBeInstanceOf ( Function ) ; } ) ; it ( 'should return a module with a provider having a useFactory calling ConfigService & ClientProxyFactory.Create' , ( ) => { const moduleNameMock = 'Foobar' ; jest . spyOn ( ClientProxyFactory , 'create' ) . mockImplementation ( ( ) => { return { } as ClientProxy ; } ) ; const module = RabbitmqModule . registerFor ( moduleNameMock ) ; const provider = module . providers [ 0 ] as any ; provider . useFactory ( configServiceMock ) ; expect ( configServiceMock . get ) . toHaveBeenCalledTimes ( 1 ) ; expect ( configServiceMock . get ) . toHaveBeenCalledWith ( 'FoobarBroker' ) ; expect ( ClientProxyFactory . create ) . toHaveBeenCalledTimes ( 1 ) ; expect ( ClientProxyFactory . create ) . toHaveBeenCalledWith ( { transport : Transport . RMQ , options : configServiceReturnValue , } ) ; } ) ; } ) ; } ) ;