@asuka/di
Version:
Dependencies inject library
384 lines • 11.1 kB
JavaScript
import { __decorate, __metadata, __param } from "tslib";
import 'reflect-metadata';
import test from 'ava';
import { Inject, InjectionToken, Injectable, rootInjector } from '../index';
test.afterEach(function () {
rootInjector.reset();
});
test('should get single instance', function (t) {
var Single = (function () {
function Single() {
}
Single = __decorate([
Injectable()
], Single);
return Single;
}());
var instance = rootInjector.getInstance(Single);
t.true(instance instanceof Single);
});
test('should get same instance after add new provider', function (t) {
var Single = (function () {
function Single() {
}
Single = __decorate([
Injectable()
], Single);
return Single;
}());
var NewOne = (function () {
function NewOne() {
}
return NewOne;
}());
var instance1 = rootInjector.getInstance(Single);
rootInjector.addProvider(NewOne);
var instance2 = rootInjector.getInstance(Single);
t.is(instance1, instance2);
});
test('should get same instance after override providers', function (t) {
var Single = (function () {
function Single() {
}
Single = __decorate([
Injectable()
], Single);
return Single;
}());
var NewOne = (function () {
function NewOne() {
}
return NewOne;
}());
var AddedOne = (function () {
function AddedOne() {
}
return AddedOne;
}());
var injector = rootInjector;
var addedInjector = injector.createChild([AddedOne, NewOne]);
var instance1 = injector.getInstance(Single);
var instance2 = addedInjector.getInstance(Single);
t.is(instance1, instance2);
});
test('should get dependencies', function (t) {
var Dep = (function () {
function Dep() {
}
Dep = __decorate([
Injectable()
], Dep);
return Dep;
}());
var DepTwo = (function () {
function DepTwo(dep) {
this.dep = dep;
}
DepTwo = __decorate([
Injectable(),
__metadata("design:paramtypes", [Dep])
], DepTwo);
return DepTwo;
}());
var Service = (function () {
function Service(dep, depTwo) {
this.dep = dep;
this.depTwo = depTwo;
}
Service = __decorate([
Injectable(),
__metadata("design:paramtypes", [Dep, DepTwo])
], Service);
return Service;
}());
var injector = rootInjector;
var service = injector.getInstance(Service);
t.true(injector.getInstance(Dep) instanceof Dep);
t.true(injector.getInstance(DepTwo) instanceof DepTwo);
t.true(service instanceof Service);
});
test('should singleton by default', function (t) {
var Dep = (function () {
function Dep() {
}
Dep = __decorate([
Injectable()
], Dep);
return Dep;
}());
var DepTwo = (function () {
function DepTwo(dep) {
this.dep = dep;
}
DepTwo = __decorate([
Injectable(),
__metadata("design:paramtypes", [Dep])
], DepTwo);
return DepTwo;
}());
var Service = (function () {
function Service(dep, depTwo) {
this.dep = dep;
this.depTwo = depTwo;
}
Service = __decorate([
Injectable(),
__metadata("design:paramtypes", [Dep, DepTwo])
], Service);
return Service;
}());
var injector = rootInjector;
var service = injector.getInstance(Service);
var dep = injector.getInstance(Dep);
var depTwo = injector.getInstance(DepTwo);
t.is(service.dep, dep);
t.is(service.depTwo, depTwo);
});
test('should be able to inject by useValue', function (t) {
function whatever() { }
var token = new InjectionToken('whatever');
rootInjector.addProvider({
provide: token,
useValue: whatever,
});
var Service = (function () {
function Service(dep) {
this.dep = dep;
}
Service = __decorate([
Injectable(),
__param(0, Inject(token)),
__metadata("design:paramtypes", [Object])
], Service);
return Service;
}());
var injector = rootInjector;
var service = injector.getInstance(Service);
t.true(service instanceof Service);
t.is(service.dep, whatever);
});
test('should be able to replace provide', function (t) {
var rawClientProvide = rootInjector.addProvider({
provide: new InjectionToken('raw-client'),
useValue: Object.create(null),
});
var queryProvider = rootInjector.addProvider({
provide: new InjectionToken('query'),
useFactory: function (client) {
return Object.create({
client: client,
});
},
deps: [rawClientProvide.provide],
});
var Client = (function () {
function Client(query) {
this.query = query;
}
Client = __decorate([
Injectable(),
__param(0, Inject(queryProvider.provide)),
__metadata("design:paramtypes", [Object])
], Client);
return Client;
}());
var Module = (function () {
function Module(client) {
this.client = client;
}
Module = __decorate([
Injectable(),
__metadata("design:paramtypes", [Client])
], Module);
return Module;
}());
var childInjector = rootInjector.createChild([
{
provide: rawClientProvide.provide,
useValue: new Date(),
},
]);
var oldM = rootInjector.getInstance(Module);
var m = childInjector.getInstance(Module);
t.not(oldM, m);
t.true(m.client.query.client instanceof Date);
});
test('should be able to inject by useFactory', function (t) {
var Dep = (function () {
function Dep(cacheSize) {
this.cacheSize = cacheSize;
}
return Dep;
}());
var cacheSize = 5;
var token = new InjectionToken('whatever');
rootInjector.addProvider({
provide: token,
useFactory: function () {
return new Dep(cacheSize);
},
});
var Service = (function () {
function Service(dep) {
this.dep = dep;
}
Service = __decorate([
Injectable(),
__param(0, Inject(token)),
__metadata("design:paramtypes", [Dep])
], Service);
return Service;
}());
var injector = rootInjector;
var service = injector.getInstance(Service);
t.true(service.dep instanceof Dep);
t.is(service.dep.cacheSize, cacheSize);
});
test('should be able to resolve deps from useFactory', function (t) {
var Dep = (function () {
function Dep(cacheSize, depTwo) {
this.cacheSize = cacheSize;
this.depTwo = depTwo;
}
Dep = __decorate([
Injectable(),
__metadata("design:paramtypes", [Number, DepTwo])
], Dep);
return Dep;
}());
var DepTwo = (function () {
function DepTwo() {
}
DepTwo = __decorate([
Injectable()
], DepTwo);
return DepTwo;
}());
var cacheSize = 5;
var token = new InjectionToken('whatever');
rootInjector.addProvider({
provide: token,
useFactory: function (depTwo) {
return new Dep(cacheSize, depTwo);
},
deps: [DepTwo],
});
var Service = (function () {
function Service(dep) {
this.dep = dep;
}
Service = __decorate([
Injectable(),
__param(0, Inject(token)),
__metadata("design:paramtypes", [Dep])
], Service);
return Service;
}());
var injector = rootInjector;
var service = injector.getInstance(Service);
var depTwo = injector.getInstance(DepTwo);
t.true(service.dep instanceof Dep);
t.is(service.dep.cacheSize, cacheSize);
t.true(depTwo instanceof DepTwo);
t.is(service.dep.depTwo, depTwo);
});
test('should be able to inject by useClass', function (t) {
var Dep = (function () {
function Dep() {
}
Dep = __decorate([
Injectable(),
__metadata("design:paramtypes", [])
], Dep);
return Dep;
}());
var token = new InjectionToken('whatever');
rootInjector.addProvider({
provide: token,
useClass: Dep,
});
var Service = (function () {
function Service(dep) {
this.dep = dep;
}
Service = __decorate([
Injectable(),
__param(0, Inject(token)),
__metadata("design:paramtypes", [Dep])
], Service);
return Service;
}());
var injector = rootInjector;
var service = injector.getInstance(Service);
t.true(service instanceof Service);
t.true(service.dep instanceof Dep);
});
test('should initialize without cache #1', function (t) {
var Dep = (function () {
function Dep() {
}
Dep = __decorate([
Injectable()
], Dep);
return Dep;
}());
var Service = (function () {
function Service(dep) {
this.dep = dep;
}
Service = __decorate([
Injectable(),
__metadata("design:paramtypes", [Dep])
], Service);
return Service;
}());
var injector = rootInjector;
var dep = injector.resolveAndInstantiate(Dep);
var service = injector.getInstance(Service);
t.not(dep, service.dep);
});
test('should initialize without cache #2', function (t) {
var Dep = (function () {
function Dep() {
}
Dep = __decorate([
Injectable()
], Dep);
return Dep;
}());
var injector = rootInjector;
var dep1 = injector.resolveAndInstantiate(Dep);
var dep2 = injector.resolveAndInstantiate(Dep);
t.not(dep1, dep2);
});
test('should resolve and create new injector', function (t) {
var Dep = (function () {
function Dep() {
}
return Dep;
}());
var token = new InjectionToken('whatever');
rootInjector.addProvider({
provide: token,
useClass: Dep,
});
var Service = (function () {
function Service(dep) {
this.dep = dep;
}
Service = __decorate([
Injectable(),
__param(0, Inject(token)),
__metadata("design:paramtypes", [Dep])
], Service);
return Service;
}());
var replacementProvider = {
provide: token,
useValue: 1,
};
var newInjector = rootInjector.createChild([replacementProvider]);
var service = newInjector.getInstance(Service);
t.is(service.dep, 1);
});
//# sourceMappingURL=injectable.spec.js.map