zhtsdi
Version:
Typescript写的依赖注入库。An dependency-injection library for TypeScript.
78 lines (61 loc) • 1.62 kB
text/typescript
/**
* Created by Weizehua on 2017/1/13.
*/
import "reflect-metadata"
import {assert} from "chai"
import {suite, test} from "mocha-typescript"
import {Injectable, Injector, Singleton} from "../src/index";
class A {
}
class B {
private val = '1234';
constructor(private a: A) {
}
getVal() {
return this.val;
}
setVal(newval) {
this.val = newval;
}
}
class SingletonClass {
private val = '1234';
constructor(private a: A) {
}
get() {
return this.val;
}
set(newval) {
this.val = newval;
}
}
class InjectorTest {
static 'get @Injectable()'() {
let b = Injector.get(B);
assert.equal(b.getVal(), '1234');
}
static 'get @Singleton()'() {
let b = Injector.get(SingletonClass);
assert.equal(b.get(), '1234');
}
static 'different @Injectable() instance should reference to different object'() {
let b = Injector.get(B);
let b2 = Injector.get(B);
let newVal = 'zzzz';
b.setVal(newVal);
assert.equal(b.getVal(), newVal);
assert.equal(b2.getVal(), '1234');
}
static 'different @Singleton() instance should reference to the same object'() {
let b = Injector.get(SingletonClass);
let b2 = Injector.get(SingletonClass);
let newVal = 'zzzz';
b.set(newVal);
assert.equal(b.get(), newVal);
assert.equal(b2.get(), newVal);
}
}