@sigi/di
Version:
Dependencies injection library for sigi framework
374 lines • 12.3 kB
JavaScript
import { __decorate, __metadata, __param } from "tslib";
import '@abraham/reflection';
import { Inject, InjectionToken, Injectable, rootInjector } from '../index';
describe('injectable specs', () => {
afterEach(() => {
rootInjector.reset();
});
it('should get single instance', () => {
let Single = class Single {
};
Single = __decorate([
Injectable()
], Single);
const instance = rootInjector.getInstance(Single);
expect(instance instanceof Single).toBeTruthy();
});
it('should get same instance after add new provider', () => {
let Single = class Single {
};
Single = __decorate([
Injectable()
], Single);
class NewOne {
}
const instance1 = rootInjector.getInstance(Single);
rootInjector.addProvider(NewOne);
const instance2 = rootInjector.getInstance(Single);
expect(instance1).toBe(instance2);
});
it('should get same instance after override providers', () => {
let Single = class Single {
};
Single = __decorate([
Injectable()
], Single);
class NewOne {
}
class AddedOne {
}
const injector = rootInjector;
const addedInjector = injector.createChild([AddedOne, NewOne]);
const instance1 = injector.getInstance(Single);
const instance2 = addedInjector.getInstance(Single);
expect(instance1).toBe(instance2);
});
it('should get dependencies', () => {
let Dep = class Dep {
};
Dep = __decorate([
Injectable()
], Dep);
let DepTwo = class DepTwo {
constructor(dep) {
this.dep = dep;
}
};
DepTwo = __decorate([
Injectable(),
__metadata("design:paramtypes", [Dep])
], DepTwo);
let Service = class Service {
constructor(dep, depTwo) {
this.dep = dep;
this.depTwo = depTwo;
}
};
Service = __decorate([
Injectable(),
__metadata("design:paramtypes", [Dep,
DepTwo])
], Service);
const injector = rootInjector;
const service = injector.getInstance(Service);
expect(injector.getInstance(Dep) instanceof Dep).toBeTruthy();
expect(injector.getInstance(DepTwo) instanceof DepTwo).toBeTruthy();
expect(service instanceof Service).toBeTruthy();
});
it('should singleton by default', () => {
let Dep = class Dep {
};
Dep = __decorate([
Injectable()
], Dep);
let DepTwo = class DepTwo {
constructor(dep) {
this.dep = dep;
}
};
DepTwo = __decorate([
Injectable(),
__metadata("design:paramtypes", [Dep])
], DepTwo);
let Service = class Service {
constructor(dep, depTwo) {
this.dep = dep;
this.depTwo = depTwo;
}
};
Service = __decorate([
Injectable(),
__metadata("design:paramtypes", [Dep,
DepTwo])
], Service);
const injector = rootInjector;
const service = injector.getInstance(Service);
const dep = injector.getInstance(Dep);
const depTwo = injector.getInstance(DepTwo);
expect(service.dep).toBe(dep);
expect(service.depTwo).toBe(depTwo);
});
it('should be able to inject by useValue', () => {
function whatever() { }
const token = new InjectionToken('whatever');
rootInjector.addProvider({
provide: token,
useValue: whatever,
});
let Service = class Service {
constructor(dep) {
this.dep = dep;
}
};
Service = __decorate([
Injectable(),
__param(0, Inject(token)),
__metadata("design:paramtypes", [Object])
], Service);
const injector = rootInjector;
const service = injector.getInstance(Service);
expect(service instanceof Service).toBeTruthy();
expect(service.dep).toBe(whatever);
});
it('should be able to replace provide', () => {
const rawClientProvide = rootInjector.addProvider({
provide: new InjectionToken('raw-client'),
useValue: Object.create(null),
});
const queryProvider = rootInjector.addProvider({
provide: new InjectionToken('query'),
useFactory: (client) => Object.create({
client: client,
}),
deps: [rawClientProvide.provide],
});
let Client = class Client {
constructor(query) {
this.query = query;
}
};
Client = __decorate([
Injectable(),
__param(0, Inject(queryProvider.provide)),
__metadata("design:paramtypes", [Object])
], Client);
let Module = class Module {
constructor(client) {
this.client = client;
}
};
Module = __decorate([
Injectable(),
__metadata("design:paramtypes", [Client])
], Module);
const childInjector = rootInjector.createChild([
{
provide: rawClientProvide.provide,
useValue: new Date(),
},
]);
const oldM = rootInjector.getInstance(Module);
const m = childInjector.getInstance(Module);
expect(oldM).not.toBe(m);
expect(m.client.query.client instanceof Date).toBeTruthy();
});
it('should be able to inject by useFactory', () => {
class Dep {
constructor(cacheSize) {
this.cacheSize = cacheSize;
}
}
const cacheSize = 5;
const token = new InjectionToken('whatever');
rootInjector.addProvider({
provide: token,
useFactory() {
return new Dep(cacheSize);
},
});
let Service = class Service {
constructor(dep) {
this.dep = dep;
}
};
Service = __decorate([
Injectable(),
__param(0, Inject(token)),
__metadata("design:paramtypes", [Dep])
], Service);
const injector = rootInjector;
const service = injector.getInstance(Service);
expect(service.dep instanceof Dep).toBeTruthy();
expect(service.dep.cacheSize).toBe(cacheSize);
});
it('should be able to resolve deps from useFactory', () => {
let DepTwo = class DepTwo {
};
DepTwo = __decorate([
Injectable()
], DepTwo);
let Dep = class Dep {
constructor(cacheSize, depTwo) {
this.cacheSize = cacheSize;
this.depTwo = depTwo;
}
};
Dep = __decorate([
Injectable(),
__metadata("design:paramtypes", [Number, DepTwo])
], Dep);
const cacheSize = 5;
const token = new InjectionToken('whatever');
rootInjector.addProvider({
provide: token,
useFactory(depTwo) {
return new Dep(cacheSize, depTwo);
},
deps: [DepTwo],
});
let Service = class Service {
constructor(dep) {
this.dep = dep;
}
};
Service = __decorate([
Injectable(),
__param(0, Inject(token)),
__metadata("design:paramtypes", [Dep])
], Service);
const injector = rootInjector;
const service = injector.getInstance(Service);
const depTwo = injector.getInstance(DepTwo);
expect(service.dep instanceof Dep).toBeTruthy();
expect(service.dep.cacheSize).toBe(cacheSize);
expect(depTwo instanceof DepTwo).toBeTruthy();
expect(service.dep.depTwo).toBe(depTwo);
});
it('should be able to inject by useClass', () => {
let Dep = class Dep {
};
Dep = __decorate([
Injectable()
], Dep);
const token = new InjectionToken('whatever');
rootInjector.addProvider({
provide: token,
useClass: Dep,
});
let Service = class Service {
constructor(dep) {
this.dep = dep;
}
};
Service = __decorate([
Injectable(),
__param(0, Inject(token)),
__metadata("design:paramtypes", [Dep])
], Service);
const injector = rootInjector;
const service = injector.getInstance(Service);
expect(service instanceof Service).toBeTruthy();
expect(service.dep instanceof Dep).toBeTruthy();
});
it('should initialize without cache #1', () => {
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 injector = rootInjector;
const dep = injector.resolveAndInstantiate(Dep);
const service = injector.getInstance(Service);
expect(dep).not.toBe(service.dep);
});
it('should initialize without cache #2', () => {
let Dep = class Dep {
};
Dep = __decorate([
Injectable()
], Dep);
const injector = rootInjector;
const dep1 = injector.resolveAndInstantiate(Dep);
const dep2 = injector.resolveAndInstantiate(Dep);
expect(dep1).not.toBe(dep2);
});
it('should initialize without cache #3', () => {
let Dep = class Dep {
};
Dep = __decorate([
Injectable()
], Dep);
const injector = rootInjector;
const dep1 = injector.resolveAndInstantiate(Dep);
const dep2 = injector.getInstance(Dep);
const dep3 = injector.resolveAndInstantiate(Dep);
expect(dep1).not.toBe(dep2);
expect(dep3).not.toBe(dep1);
expect(dep3).not.toBe(dep2);
});
it('should resolve and create new injector', () => {
class Dep {
}
const token = new InjectionToken('whatever');
rootInjector.addProvider({
provide: token,
useClass: Dep,
});
let Service = class Service {
constructor(dep) {
this.dep = dep;
}
};
Service = __decorate([
Injectable(),
__param(0, Inject(token)),
__metadata("design:paramtypes", [Dep])
], Service);
const replacementProvider = {
provide: token,
useValue: 1,
};
const newInjector = rootInjector.createChild([replacementProvider]);
const service = newInjector.getInstance(Service);
const service1 = newInjector.getInstance(Service);
expect(service.dep).toBe(1);
expect(service).toBe(service1);
});
it('should be able to inject provider via InjectableConfigs', () => {
let Dep = class Dep {
};
Dep = __decorate([
Injectable()
], Dep);
const token = new InjectionToken('whatever');
let Service = class Service {
constructor(dep) {
this.dep = dep;
}
};
Service = __decorate([
Injectable({
providers: [
{
useClass: Dep,
provide: token,
},
],
}),
__param(0, Inject(token)),
__metadata("design:paramtypes", [Dep])
], Service);
expect(rootInjector.getInstance(Service).dep instanceof Dep).toBeTruthy();
expect(rootInjector.getInstance(Service).dep).toBe(rootInjector.getInstance(Dep));
});
});
//# sourceMappingURL=injectable.spec.js.map