@sigi/di
Version:
Dependencies injection library for sigi framework
144 lines • 4.93 kB
JavaScript
import { __decorate, __metadata, __param } from "tslib";
import '@abraham/reflection';
import { Inject, Test, Injectable, InjectionToken } from '../index';
import { rootInjector } from '../root-injector';
describe('testbed spec', () => {
it('should resolve dep instance', () => {
let Dep = class Dep {
};
Dep = __decorate([
Injectable()
], Dep);
let Service = class Service {
constructor(dep) {
this.dep = dep;
}
};
Service = __decorate([
Injectable(),
__metadata("design:paramtypes", [Dep])
], Service);
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('replaceable');
rootInjector.addProvider({
useValue: replacement,
provide: token,
});
let Service = class Service {
constructor(dep) {
this.dep = dep;
}
};
Service = __decorate([
Injectable(),
__param(0, Inject(token)),
__metadata("design:paramtypes", [Object])
], Service);
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('replaceable');
rootInjector.addProvider({
useValue: replacement,
provide: token,
});
let Service = class Service {
constructor(dep) {
this.dep = dep;
}
};
Service = __decorate([
Injectable(),
__param(0, Inject(token)),
__metadata("design:paramtypes", [Object])
], Service);
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', () => {
let Dep = class Dep {
};
Dep = __decorate([
Injectable()
], Dep);
let Service = class Service {
constructor(dep) {
this.dep = dep;
}
};
Service = __decorate([
Injectable(),
__metadata("design:paramtypes", [Dep])
], Service);
let BetterDep = class BetterDep {
};
BetterDep = __decorate([
Injectable()
], 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('whatever');
rootInjector.addProvider({
provide: token,
useFactory: () => {
return '1';
},
});
let Service = class Service {
constructor(fun) {
this.fun = fun;
}
};
Service = __decorate([
Injectable(),
__param(0, Inject(token)),
__metadata("design:paramtypes", [String])
], Service);
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 {
getInstance(target) {
return this.injector.getInstance(target);
}
}
const testModule = Test.createTestingModule({
TestModule: BetterTestModule,
}).compile();
expect(testModule instanceof BetterTestModule).toBeTruthy();
});
});
//# sourceMappingURL=testbed.spec.js.map