|
|
|
@ -13,10 +13,13 @@ describe('the inversion of control container', function() {
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should accept definitions as parameters on construction', function() {
|
|
|
|
|
const example_defs = {}
|
|
|
|
|
class Test {}
|
|
|
|
|
const example_defs = {
|
|
|
|
|
foo: Test
|
|
|
|
|
}
|
|
|
|
|
const container = new Container(example_defs)
|
|
|
|
|
|
|
|
|
|
expect(container.definitions).to.be.equal(example_defs)
|
|
|
|
|
expect(container.definitions.foo).to.exist
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should set an empty object of definitions by default', function() {
|
|
|
|
@ -66,21 +69,42 @@ describe('the inversion of control container', function() {
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('service function', function() {
|
|
|
|
|
describe('[DEPRECATED] service function', function() {
|
|
|
|
|
it('should be a function', function() {
|
|
|
|
|
const container = new Container()
|
|
|
|
|
expect(container.service).to.be.a('function')
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should require no arguments', function() {
|
|
|
|
|
it('should require one argument', function() {
|
|
|
|
|
const container = new Container()
|
|
|
|
|
expect(container.service.length).to.be.equal(0)
|
|
|
|
|
expect(container.service.length).to.be.equal(1)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should call the non-deprecated get method', function() {
|
|
|
|
|
const container = new Container()
|
|
|
|
|
container.get = sinon.spy()
|
|
|
|
|
const sym = Symbol()
|
|
|
|
|
container.service(sym)
|
|
|
|
|
expect(container.get.calledOnce).to.be.true
|
|
|
|
|
expect(container.get.getCall(0).args[0]).to.be.equal(sym)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('get function', function() {
|
|
|
|
|
it('should be a function', function() {
|
|
|
|
|
const container = new Container()
|
|
|
|
|
expect(container.get).to.be.a('function')
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should require one argument', function() {
|
|
|
|
|
const container = new Container()
|
|
|
|
|
expect(container.get.length).to.be.equal(1)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should throw an error with a helpful message if the passed in service has no definition', function() {
|
|
|
|
|
const container = new Container()
|
|
|
|
|
const name = Math.random().toString(36).substring(7)
|
|
|
|
|
expect(container.service.bind(container, name)).to.throw('No such service registered with this container: '+name)
|
|
|
|
|
expect(container.get.bind(container, name)).to.throw(`${name}`)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should instantiate service classes by name', function() {
|
|
|
|
@ -89,7 +113,7 @@ describe('the inversion of control container', function() {
|
|
|
|
|
const defs = {}
|
|
|
|
|
defs[name] = TestService
|
|
|
|
|
const container = new Container(defs)
|
|
|
|
|
expect(container.service(name)).to.be.an.instanceof(TestService)
|
|
|
|
|
expect(container.get(name)).to.be.an.instanceof(TestService)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should re-use instantiated services', function() {
|
|
|
|
@ -98,8 +122,8 @@ describe('the inversion of control container', function() {
|
|
|
|
|
const defs = {}
|
|
|
|
|
defs[name] = TestService
|
|
|
|
|
const container = new Container(defs)
|
|
|
|
|
const test_service_1 = container.service(name)
|
|
|
|
|
const test_service_2 = container.service(name)
|
|
|
|
|
const test_service_1 = container.get(name)
|
|
|
|
|
const test_service_2 = container.get(name)
|
|
|
|
|
expect(test_service_1).to.be.equal(test_service_2)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
@ -109,24 +133,12 @@ describe('the inversion of control container', function() {
|
|
|
|
|
const defs = { test: TestService }
|
|
|
|
|
const container = new Container(defs)
|
|
|
|
|
container.di = fake_di
|
|
|
|
|
container.service('test')
|
|
|
|
|
container.get('test')
|
|
|
|
|
|
|
|
|
|
expect(fake_di.make.calledOnce).to.be.true
|
|
|
|
|
expect(fake_di.make.getCall(0).args.length).to.be.equal(1)
|
|
|
|
|
expect(fake_di.make.getCall(0).args[0]).to.be.equal(TestService)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should return a proxy container if called with no arguments', function() {
|
|
|
|
|
class TestService {}
|
|
|
|
|
const name = Math.random().toString(36).substring(7)
|
|
|
|
|
const defs = {}
|
|
|
|
|
defs[name] = TestService
|
|
|
|
|
const container = new Container(defs)
|
|
|
|
|
const services = container.service()
|
|
|
|
|
|
|
|
|
|
expect(util.types.isProxy(services)).to.be.true
|
|
|
|
|
expect(services[name]).to.be.an.instanceof(TestService)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('proxy function', function() {
|
|
|
|
@ -143,15 +155,15 @@ describe('the inversion of control container', function() {
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('register function', function() {
|
|
|
|
|
describe('register_service function', function() {
|
|
|
|
|
it('should be a function', function() {
|
|
|
|
|
const container = new Container()
|
|
|
|
|
expect(container.register).to.be.a('function')
|
|
|
|
|
expect(container.register_service).to.be.a('function')
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should accept 2 arguments', function() {
|
|
|
|
|
const container = new Container()
|
|
|
|
|
expect(container.register.length).to.be.equal(2)
|
|
|
|
|
expect(container.register_service.length).to.be.equal(2)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should register service definitions by name', function() {
|
|
|
|
@ -159,8 +171,8 @@ describe('the inversion of control container', function() {
|
|
|
|
|
const name = Math.random().toString(36).substring(7)
|
|
|
|
|
const container = new Container()
|
|
|
|
|
|
|
|
|
|
container.register(name, TestService)
|
|
|
|
|
expect(container.definitions[name]).to.be.equal(TestService)
|
|
|
|
|
container.register_service(name, TestService)
|
|
|
|
|
expect(container.definitions[name].ref).to.be.equal(TestService)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should trigger deferral processing', function() {
|
|
|
|
@ -168,8 +180,9 @@ describe('the inversion of control container', function() {
|
|
|
|
|
const name = Math.random().toString(36).substring(7)
|
|
|
|
|
const container = new Container()
|
|
|
|
|
|
|
|
|
|
container.deferred_classes = [{_di_deferred_services: [name]}]
|
|
|
|
|
container._process_deferral = sinon.spy()
|
|
|
|
|
container.register(name, TestService)
|
|
|
|
|
container.register_service(name, TestService)
|
|
|
|
|
expect(container._process_deferral.calledOnce).to.be.true
|
|
|
|
|
expect(container._process_deferral.getCall(0).args.length).to.be.equal(2)
|
|
|
|
|
expect(container._process_deferral.getCall(0).args[0]).to.be.equal(name)
|
|
|
|
@ -177,36 +190,25 @@ describe('the inversion of control container', function() {
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('register_as_instance function', function() {
|
|
|
|
|
describe('register_singleton function', function() {
|
|
|
|
|
it('should be a function', function() {
|
|
|
|
|
const container = new Container()
|
|
|
|
|
expect(container.register_as_instance).to.be.a('function')
|
|
|
|
|
expect(container.register_singleton).to.be.a('function')
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should accept 2 arguments', function() {
|
|
|
|
|
const container = new Container()
|
|
|
|
|
expect(container.register_as_instance.length).to.be.equal(2)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should register service constructors as definitions by name', function() {
|
|
|
|
|
class TestService {}
|
|
|
|
|
const name = Math.random().toString(36).substring(7)
|
|
|
|
|
const container = new Container()
|
|
|
|
|
const test = new TestService()
|
|
|
|
|
|
|
|
|
|
container.register_as_instance(name, test)
|
|
|
|
|
expect(container.definitions[name]).to.be.equal(TestService)
|
|
|
|
|
expect(container.register_singleton.length).to.be.equal(2)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should register service instances by name for reuse', function() {
|
|
|
|
|
it('should register singletons as definitions by name', function() {
|
|
|
|
|
class TestService {}
|
|
|
|
|
const name = Math.random().toString(36).substring(7)
|
|
|
|
|
const container = new Container()
|
|
|
|
|
const test = new TestService()
|
|
|
|
|
|
|
|
|
|
container.register_as_instance(name, test)
|
|
|
|
|
expect(container.service(name)).to.be.equal(test)
|
|
|
|
|
expect(container.service(name)).to.be.equal(test)
|
|
|
|
|
container.register_singleton(name, test)
|
|
|
|
|
expect(container.definitions[name].ref).to.be.equal(test)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should trigger deferral processing', function() {
|
|
|
|
@ -215,8 +217,9 @@ describe('the inversion of control container', function() {
|
|
|
|
|
const container = new Container()
|
|
|
|
|
const test = new TestService()
|
|
|
|
|
|
|
|
|
|
container.deferred_classes = [{_di_deferred_services: [name]}]
|
|
|
|
|
container._process_deferral = sinon.spy()
|
|
|
|
|
container.register_as_instance(name, test)
|
|
|
|
|
container.register_singleton(name, test)
|
|
|
|
|
expect(container._process_deferral.calledOnce).to.be.true
|
|
|
|
|
expect(container._process_deferral.getCall(0).args.length).to.be.equal(2)
|
|
|
|
|
expect(container._process_deferral.getCall(0).args[0]).to.be.equal(name)
|
|
|
|
|