@sigi/di
Version:
Dependencies injection library for sigi framework
50 lines • 1.77 kB
JavaScript
import { rootInjector } from './root-injector';
export class Test {
static createTestingModule(overrideConfig) {
return new Test((overrideConfig === null || overrideConfig === void 0 ? void 0 : overrideConfig.providers) ? overrideConfig.providers : [], (overrideConfig === null || overrideConfig === void 0 ? void 0 : overrideConfig.TestModule) ? overrideConfig.TestModule : TestModule);
}
constructor(providers, TestModule) {
var _a;
this.TestModule = TestModule;
this.providersMap = new Map();
for (const provider of providers) {
this.providersMap.set((_a = provider.provide) !== null && _a !== void 0 ? _a : 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