UNPKG

@sigi/di

Version:

Dependencies injection library for sigi framework

49 lines 1.59 kB
import { rootInjector } from './root-injector'; export class Test { static createTestingModule(overrideConfig) { return new Test(overrideConfig?.providers ? overrideConfig.providers : [], overrideConfig?.TestModule ? overrideConfig.TestModule : TestModule); } constructor(providers, TestModule) { this.TestModule = TestModule; this.providersMap = new Map(); for (const provider of providers) { this.providersMap.set(provider.provide ?? provider, provider); } } overrideProvider(token) { return new MockProvider(this, token); } compile() { const childInjector = rootInjector.createChild(Array.from(this.providersMap.values())); return new this.TestModule(childInjector); } } export class MockProvider { constructor(test, token) { this.test = test; this.token = token; } useClass(value) { this.test.providersMap.set(this.token, { provide: this.token, useClass: value }); return this.test; } useValue(value) { this.test.providersMap.set(this.token, { provide: this.token, useValue: value }); return this.test; } useFactory(value) { this.test.providersMap.set(this.token, { provide: this.token, useFactory: value }); return this.test; } } export class AbstractTestModule { } export class TestModule { constructor(injector) { this.injector = injector; } getInstance(token) { return this.injector.getInstance(token); } } //# sourceMappingURL=testbed.js.map