Write integration tests

develop
garrettmills 4 years ago
parent d3e3c0675d
commit c459959654

@ -12,6 +12,7 @@
"sinon": "^8.1.1"
},
"scripts": {
"test": "./node_modules/.bin/mocha --reporter spec"
"test_units": "./node_modules/.bin/mocha --reporter spec test",
"test_integration": "./node_modules/.bin/mocha --reporter spec test/integration"
}
}

@ -88,6 +88,8 @@ class Injectable {
instance[service_name] = service_instance
}
this.prototype[service_name] = service_instance
this._di_deferred_services = this._di_deferred_services.filter(x => x !== service_name)
}
}

@ -0,0 +1,82 @@
const { expect } = require('chai')
const Container = require('../../src/Container')
const Service = require('../../src/Service')
const DependencyInjector = require('../../src/DependencyInjector')
const Injectable = require('../../src/Injectable')
describe('[integration] basic service use', function() {
let container
let di
let ServiceClass
let service_name
beforeEach(function() {
class Svc extends Service {}
ServiceClass = Svc
service_name = Math.random().toString(36).substring(7)
const defs = {}
defs[service_name] = ServiceClass
container = new Container(defs)
di = new DependencyInjector(container)
})
it('should make the specified service available', function() {
expect(di.container.service(service_name)).to.be.an.instanceof(ServiceClass)
})
it('should reuse service instances', function() {
expect(di.container.service(service_name)).to.be.equal(di.container.service(service_name))
})
it('should inject injectable classes', function() {
class InjTest extends Injectable {
static get services() {
return [service_name]
}
}
di.make(InjTest)
const inj = new InjTest()
expect(inj[service_name]).to.be.an.instanceof(ServiceClass)
})
it('should inject services themselves', function() {
const s2_name = Math.random().toString(36).substring(7)
class Svc2 extends Service {
static get services() {
return [service_name]
}
}
di.container.register(s2_name, Svc2)
const s2 = di.container.service(s2_name)
expect(s2).to.be.an.instanceof(Svc2)
expect(s2[service_name]).to.be.an.instanceof(ServiceClass)
})
it('should throw an error when services are not found', function() {
const bad_name = Math.random().toString(36).substring(7)
expect(di.container.service.bind(di.container, bad_name)).to.throw(Error)
})
it('should inject deferred services', function() {
const s2_name = Math.random().toString(36).substring(7)
class Svc2 extends Service {}
class Inj2 extends Injectable {
static get services() {
return [service_name, s2_name]
}
}
di.make(Inj2)
const i2 = new Inj2()
expect(i2[service_name]).to.be.an.instanceof(ServiceClass)
expect(i2[s2_name]).to.be.undefined
di.container.register(s2_name, Svc2)
expect(i2[s2_name]).to.be.an.instanceof(Svc2)
const i3 = new Inj2()
expect(i3[s2_name]).to.be.an.instanceof(Svc2)
})
})
Loading…
Cancel
Save