ts.di
Version:
Typescript写的依赖注入库。An dependency-injection library for TypeScript.
233 lines • 8.87 kB
JavaScript
/**
* Created by Weizehua on 2017/1/13.
*/
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
require("reflect-metadata");
const chai_1 = require("chai");
const mocha_typescript_1 = require("mocha-typescript");
const Injector_1 = require("../src/Injector");
const singleton_a_1 = require("./singleton_a");
const singleton_b_1 = require("./singleton_b");
const loop_dependency_singleton_1 = require("./loop_dependency_singleton");
const loop_dependency_a_1 = require("./loop_dependency_a");
const Injector_2 = require("../src/Injector");
let A = class A {
};
A = __decorate([
Injector_1.Injectable()
], A);
let B = class B {
constructor(a) {
this.a = a;
this.val = '1234';
}
};
B = __decorate([
Injector_1.Injectable(),
__metadata("design:paramtypes", [A])
], B);
let SingletonClass = class SingletonClass {
constructor(a) {
this.a = a;
this.val = '1234';
}
};
SingletonClass = __decorate([
Injector_1.Singleton(),
__metadata("design:paramtypes", [A])
], SingletonClass);
let SingletonClassForSet = class SingletonClassForSet {
};
SingletonClassForSet = __decorate([
Injector_1.Singleton()
], SingletonClassForSet);
let InjectPropertyClass = class InjectPropertyClass {
constructor() {
this.val = '1234';
}
};
__decorate([
Injector_1.Inject(),
__metadata("design:type", B)
], InjectPropertyClass.prototype, "b", void 0);
InjectPropertyClass = __decorate([
Injector_1.Injectable()
], InjectPropertyClass);
let TypedInjectClass = class TypedInjectClass {
constructor() {
this.val = '1234';
}
};
__decorate([
Injector_1.Inject(() => B),
__metadata("design:type", B)
], TypedInjectClass.prototype, "b", void 0);
TypedInjectClass = __decorate([
Injector_1.Injectable()
], TypedInjectClass);
let FA = class FA {
constructor() {
this.val = '1234';
}
};
FA = __decorate([
Injector_1.Injectable()
], FA);
class FactorFA {
static factorOfA() {
return { val: 'factor!' };
}
}
__decorate([
Injector_2.FactorOf(FA),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", FA)
], FactorFA, "factorOfA", null);
let InjectorTest = class InjectorTest {
'get @Injectable()'() {
let b = Injector_1.Injector.get(B);
chai_1.assert.equal(b.val, '1234');
}
'get @Singleton()'() {
let b = Injector_1.Injector.get(SingletonClass);
chai_1.assert.equal(b.val, '1234');
}
'get @Inject()'() {
let b = Injector_1.Injector.get(InjectPropertyClass);
chai_1.assert.equal(b.val, '1234');
}
'get @Inject(()=>Type)'() {
let b = Injector_1.Injector.get(TypedInjectClass);
chai_1.assert.equal(b.val, '1234');
}
'Injector.setSingleton()'() {
let s1 = new SingletonClassForSet();
Injector_1.Injector.setSingleton(SingletonClassForSet, s1);
let s2 = Injector_1.Injector.get(SingletonClassForSet);
chai_1.assert.equal(s1, s2);
}
'@FactorOf(Class)'() {
let normal1 = new FA();
let normal2 = new FA();
let factor1 = Injector_1.Injector.get(FA);
let factor2 = Injector_1.Injector.get(FA);
chai_1.assert.equal(normal1.val, normal2.val);
chai_1.assert.equal(normal1.val, '1234');
chai_1.assert.notEqual(normal1.val, factor1.val);
chai_1.assert.equal(factor1.val, factor2.val);
chai_1.assert.equal(factor1.val, 'factor!');
}
'different @Injectable() instance should reference to different object'() {
let b = Injector_1.Injector.get(B);
let b2 = Injector_1.Injector.get(B);
let newVal = 'zzzz';
b.val = newVal;
chai_1.assert.equal(b.val, newVal);
chai_1.assert.equal(b2.val, '1234');
chai_1.assert.notEqual(b.val, b2.val);
}
'different @Singleton() instance should reference to the same object'() {
let b = Injector_1.Injector.get(SingletonClass);
let b2 = Injector_1.Injector.get(SingletonClass);
let newVal = 'zzzz';
b.val = newVal;
chai_1.assert.equal(b.val, newVal);
chai_1.assert.equal(b2.val, newVal);
}
'A->B->A dependency is available() when there is a @Singleton among them'() {
let s = Injector_1.Injector.get(loop_dependency_singleton_1.LoopDependencySingleton);
let a = Injector_1.Injector.get(loop_dependency_a_1.LoopDependencyA);
// first: access property then instantiate object
chai_1.assert.equal(s, a.s, '@Singleton should equal');
chai_1.assert.notEqual(a, s.a, '@Injectable() should not equal');
chai_1.assert.equal(s, s.a.s);
// second: get existing property
chai_1.assert.equal(s, a.s);
chai_1.assert.notEqual(a, s.a);
chai_1.assert.equal(s, s.a.s);
}
'A->B->A dependency is available() when there is a @Singleton among them.2'() {
let sa = Injector_1.Injector.get(singleton_a_1.SingletonA);
let sb = Injector_1.Injector.get(singleton_b_1.SingletonB);
// first: access property then instantiate object
chai_1.assert.equal(sa, sb.sa);
chai_1.assert.equal(sb, sa.sb);
// second: get existing property
chai_1.assert.equal(sa, sb.sa);
chai_1.assert.equal(sb, sa.sb);
}
};
__decorate([
mocha_typescript_1.test,
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], InjectorTest.prototype, "get @Injectable()", null);
__decorate([
mocha_typescript_1.test,
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], InjectorTest.prototype, "get @Singleton()", null);
__decorate([
mocha_typescript_1.test,
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], InjectorTest.prototype, "get @Inject()", null);
__decorate([
mocha_typescript_1.test,
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], InjectorTest.prototype, "get @Inject(()=>Type)", null);
__decorate([
mocha_typescript_1.test,
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], InjectorTest.prototype, "Injector.setSingleton()", null);
__decorate([
mocha_typescript_1.test,
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], InjectorTest.prototype, "@FactorOf(Class)", null);
__decorate([
mocha_typescript_1.test,
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], InjectorTest.prototype, "different @Injectable() instance should reference to different object", null);
__decorate([
mocha_typescript_1.test,
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], InjectorTest.prototype, "different @Singleton() instance should reference to the same object", null);
__decorate([
mocha_typescript_1.test,
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], InjectorTest.prototype, "A->B->A dependency is available() when there is a @Singleton among them", null);
__decorate([
mocha_typescript_1.test,
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", void 0)
], InjectorTest.prototype, "A->B->A dependency is available() when there is a @Singleton among them.2", null);
InjectorTest = __decorate([
mocha_typescript_1.suite
], InjectorTest);
//# sourceMappingURL=test.js.map