tedi
Version:
Express wrappper written in typescript with dependency injection capabilities
222 lines (221 loc) • 10.9 kB
JavaScript
;
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);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("../../core");
const utils_1 = require("../../core/utils");
const decorators_1 = require("../../decorators");
describe("Module:", () => {
describe("when we have a valid Module", () => {
let module;
beforeEach(() => {
module = new core_1.Module();
});
describe("#validateModule", () => {
it("should validate", () => {
expect(() => core_1.validateModule(module)).not.toThrow();
});
});
describe("and we register a dependency", () => {
let Dependency = class Dependency {
};
Dependency = __decorate([
decorators_1.Injectable()
], Dependency);
describe("using the simple notation", () => {
let aDependency;
beforeEach(() => {
module.setDependency(Dependency);
aDependency = module.getDependency(Dependency);
});
it("we should get the right instance", () => {
expect(aDependency).toEqual(jasmine.any(Dependency));
});
it("we should get the same instance every time we ask for one", () => {
expect(module.getDependency(Dependency) === aDependency).toBeTruthy();
});
describe("and we override the dependency", () => {
let OverridedDependency = class OverridedDependency {
};
OverridedDependency = __decorate([
decorators_1.Injectable()
], OverridedDependency);
beforeEach(() => {
module.setDependency(core_1.dependency(Dependency, { class: OverridedDependency }));
});
it("should get an instance of the overrided dependency", () => {
expect(module.getDependency(Dependency)).toEqual(jasmine.any(OverridedDependency));
});
});
});
describe("using the complex notation", () => {
let aDependency;
beforeEach(() => {
module.setDependency(core_1.dependency(Dependency, { class: Dependency }));
aDependency = module.getDependency(Dependency);
});
it("we should get the right instance", () => {
expect(aDependency).toEqual(jasmine.any(Dependency));
});
it("we should get the same instance every time we ask for one", () => {
expect(module.getDependency(Dependency) === aDependency).toBeTruthy();
});
describe("and then we override the dependency token with a new class", () => {
let AnotherDependency = class AnotherDependency {
};
AnotherDependency = __decorate([
decorators_1.Injectable()
], AnotherDependency);
beforeEach(() => {
module.setDependency(core_1.dependency(Dependency, { class: AnotherDependency }));
});
it("override should have worked", () => {
expect(module.getDependency(Dependency)).toEqual(jasmine.any(AnotherDependency));
expect(module.getDependency(Dependency)).not.toEqual(jasmine.any(Dependency));
});
});
});
describe("using the complex notation in transient mode", () => {
let aDependency;
beforeEach(() => {
module.setDependency(core_1.dependency(Dependency, { class: Dependency, transient: true }));
aDependency = module.getDependency(Dependency);
});
it("we should get the right instance", () => {
expect(aDependency).toEqual(jasmine.any(Dependency));
});
it("we should get a diferent instance every time we ask for one", () => {
expect(module.getDependency(Dependency) === aDependency).toBeFalsy();
});
});
describe("that has no Injectable decoration", () => {
class AnotherDependency {
}
beforeEach(() => {
module.setDependency(core_1.dependency(Dependency, { class: AnotherDependency }));
});
it("should throw a ModuleError", () => {
expect(() => module.getDependency(Dependency))
.toThrowError(core_1.ModuleError, `Module: Error loading dependency for token ${utils_1.getClassName(Dependency)}`);
});
});
describe("that depends on another dependency", () => {
let AnotherDependency = class AnotherDependency {
constructor(dep) {
this.dep = dep;
}
};
AnotherDependency = __decorate([
decorators_1.Injectable(),
__param(0, decorators_1.Inject(Dependency)),
__metadata("design:paramtypes", [Object])
], AnotherDependency);
beforeEach(() => {
module.dependencies(Dependency, AnotherDependency);
});
describe("when we load the main dependency", () => {
let anotherDependency;
beforeEach(() => {
anotherDependency = module.getDependency(AnotherDependency);
});
it("should have injected the required dependency", () => {
expect(anotherDependency.dep).toEqual(jasmine.any(Dependency));
});
});
});
describe("using a mixed notation", () => {
beforeEach(() => {
module.dependencies(Dependency, [
core_1.dependency("1", { class: Dependency }),
core_1.dependency("2", { class: Dependency }),
], core_1.dependency("3", { class: Dependency }));
});
it("should have registered all the dependencies", () => {
expect(module.getDependency(Dependency)).toEqual(jasmine.any(Dependency));
expect(module.getDependency("1")).toEqual(jasmine.any(Dependency));
expect(module.getDependency("2")).toEqual(jasmine.any(Dependency));
expect(module.getDependency("3")).toEqual(jasmine.any(Dependency));
});
});
});
describe("and we register a child module", () => {
let childModule;
beforeEach(() => {
childModule = new core_1.Module();
module.setModule("childModule", childModule);
});
describe("and a dependency is registered in the main module", () => {
let Dependency = class Dependency {
};
Dependency = __decorate([
decorators_1.Injectable()
], Dependency);
beforeEach(() => {
module.setDependency(Dependency);
});
it("should be accessible from the child module", () => {
expect(childModule.getDependency(Dependency)).toEqual(jasmine.any(Dependency));
});
describe("and we override the dependency in the child module", () => {
let OverridedDependency = class OverridedDependency {
};
OverridedDependency = __decorate([
decorators_1.Injectable()
], OverridedDependency);
beforeEach(() => {
childModule.setDependency(core_1.dependency(Dependency, { class: OverridedDependency }));
});
it("child module should get it's registered dependency", () => {
expect(childModule.getDependency(Dependency)).toEqual(jasmine.any(OverridedDependency));
});
});
});
describe("a dependency registered in the child module", () => {
let Dependency = class Dependency {
};
Dependency = __decorate([
decorators_1.Injectable()
], Dependency);
beforeEach(() => {
childModule.setDependency(Dependency);
});
it("should not be accessible to the main module", () => {
expect(() => module.getDependency(Dependency))
.toThrowError(core_1.ModuleError, `Module: Could not find dependency "${utils_1.getClassName(Dependency)}" in the module tree`);
});
});
});
});
describe("when we have an invalid module", () => {
let invalidModule;
beforeEach(() => {
invalidModule = {};
});
describe("#validateModule", () => {
it("should throw an error", (done) => {
try {
core_1.validateModule(invalidModule);
}
catch (error) {
expect(error).toEqual(jasmine.any(core_1.ModuleError));
done();
}
});
});
});
});
describe("ModuleError", () => {
it("should inherit from TediError", () => {
expect(new core_1.ModuleError({}, "")).toEqual(jasmine.any(core_1.TediError));
});
});