import { inject , Injector } from '../index' ; describe ( 'Injector' , ( ) => { describe ( '#instantiate' , ( ) => { it ( 'instantiates a Constructor' , async ( ) => { enum ServiceTypes { NameService = 'NameService' } class NameService { getName ( ) { return 'Test' ; } } class MyService { constructor ( @ inject ( ServiceTypes . NameService ) private nameService ) { } sayHello ( ) { return `Hello, ${ this . nameService . getName ( ) }` ; } } const container = { [ ServiceTypes . NameService ] : NameService } ; const injector = new Injector ( container ) ; const myService = injector . instantiate ( MyService ) ; const result = myService . sayHello ( ) ; expect ( myService ) . toBeInstanceOf ( MyService ) ; expect ( result ) . toBe ( 'Hello, Test' ) ; } ) ; it ( 'throws if injected service does not exist.' , async ( ) => { enum ServiceTypes { NameService = 'NameService' } class NameService { getName ( ) { return 'Test' ; } } class MyService { constructor ( @ inject ( ServiceTypes . NameService ) private nameService ) { } sayHello ( ) { return `Hello, ${ this . nameService . getName ( ) }` ; } } const container = { } ; const injector = new Injector ( container ) ; expect . assertions ( 1 ) ; try { injector . instantiate ( MyService ) ; } catch ( error ) { expect ( error ) . toMatchObject ( { message : 'No service is registered for "NameService" key. (While instantiating "MyService")' } ) ; } } ) ; } ) ; describe ( '#inject' , async ( ) => { it ( 'instantiates a constructor by the given key from container' , ( ) => { enum ServiceTypes { MyService = 'MyService' } class MyService { sayHello ( ) { return 'Hello' ; } } const container = { [ ServiceTypes . MyService ] : MyService } ; const injector = new Injector ( container ) ; const myService = injector . inject ( ServiceTypes . MyService ) ; expect ( myService ) . toBeInstanceOf ( MyService ) ; } ) ; it ( 'throws if no service is regisered to container for the given key' , ( ) => { enum ServiceTypes { MyService = 'MyService' , UnregisteredService = 'UnregisteredService' } class MyService { sayHello ( ) { return 'Hello' ; } } const container = { [ ServiceTypes . MyService ] : MyService } ; const injector = new Injector ( container ) ; expect . assertions ( 1 ) ; try { injector . inject ( ServiceTypes . UnregisteredService ) ; } catch ( error ) { expect ( error ) . toMatchObject ( { message : 'No service is registered for "UnregisteredService" key.' } ) ; } } ) ; } ) ; } ) ;