@sigi/di
Version:
Dependencies injection library for sigi framework
139 lines (107 loc) • 3.53 kB
text/typescript
import '@abraham/reflection'
import { Inject, Test, Injectable, InjectionToken, AbstractTestModule } from '../index'
import { Injector } from '../injector'
import { rootInjector } from '../root-injector'
describe('testbed spec', () => {
it('should resolve dep instance', () => {
()
class Dep {}
()
class Service {
constructor(public dep: Dep) {}
}
const testModule = Test.createTestingModule().compile()
const service = testModule.getInstance(Service)
expect(service instanceof Service).toBeTruthy()
expect(service.dep instanceof Dep).toBeTruthy()
})
it('should override when createTestingModule', () => {
function whatever() {
return true
}
function replacement() {
return false
}
const token = new InjectionToken<typeof whatever>('replaceable')
rootInjector.addProvider({
useValue: replacement,
provide: token,
})
()
class Service {
constructor((token) public dep: typeof whatever) {}
}
const testModule = Test.createTestingModule({ providers: [{ provide: token, useValue: replacement }] }).compile()
const service = testModule.getInstance(Service)
expect(service instanceof Service).toBeTruthy()
expect(service.dep).toBe(replacement)
expect(service.dep()).toBe(false)
})
it('should override by overrideProvider method', () => {
function whatever() {
return true
}
function replacement() {
return false
}
const token = new InjectionToken<typeof whatever>('replaceable')
rootInjector.addProvider({
useValue: replacement,
provide: token,
})
()
class Service {
constructor((token) public dep: typeof whatever) {}
}
const testModule = Test.createTestingModule().overrideProvider(token).useValue(replacement).compile()
const service = testModule.getInstance(Service)
expect(service instanceof Service).toBeTruthy()
expect(service.dep).toBe(replacement)
expect(service.dep()).toBe(false)
})
it('should override class', () => {
()
class Dep {}
()
class Service {
constructor(public dep: Dep) {}
}
()
class BetterDep {}
const testModule = Test.createTestingModule().overrideProvider(Dep).useClass(BetterDep).compile()
const service = testModule.getInstance(Service)
expect(service instanceof Service).toBeTruthy()
expect(service.dep instanceof BetterDep).toBeTruthy()
})
it('should override factory', () => {
const token = new InjectionToken<string>('whatever')
rootInjector.addProvider({
provide: token,
useFactory: () => {
return '1'
},
})
()
class Service {
constructor((token) public fun: string) {}
}
const testModule = Test.createTestingModule()
.overrideProvider(token)
.useFactory(() => '2')
.compile()
const service = testModule.getInstance(Service)
expect(service.fun).toBe('2')
})
it('should override TestModule', () => {
class BetterTestModule implements AbstractTestModule {
private readonly injector!: Injector
getInstance<T>(target: any): T {
return this.injector.getInstance(target)
}
}
const testModule = Test.createTestingModule({
TestModule: BetterTestModule,
}).compile()
expect(testModule instanceof BetterTestModule).toBeTruthy()
})
})