From 613f8a71cd55b850a37234481de7e3dd90b2edc1 Mon Sep 17 00:00:00 2001 From: remojansen Date: Mon, 26 Sep 2016 23:25:20 +0100 Subject: [PATCH] Improved test coverage --- package.json | 2 +- test/constraints.test.ts | 649 ++++++++++++++++++++++++++++++++++++++- test/register.test.ts | 6 +- 3 files changed, 645 insertions(+), 12 deletions(-) diff --git a/package.json b/package.json index 027b1ab..1df6465 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "inversify-vanillajs-helpers", - "version": "1.0.0-beta.1", + "version": "1.0.0", "description": "Some helper for the development of InversifyJS applications with VanillaJS", "main": "lib/index.js", "jsnext:main": "es/index.js", diff --git a/test/constraints.test.ts b/test/constraints.test.ts index 1f51983..b27d9ad 100644 --- a/test/constraints.test.ts +++ b/test/constraints.test.ts @@ -134,13 +134,646 @@ describe("Register helper constraints", () => { }); - it("Should allow to apply constraints to registerSelf"); - it("Should allow to apply constraints to registerConstructor"); - it("Should allow to apply constraints to registerConstantValue"); - it("Should allow to apply constraints to registerDynamicValue"); - it("Should allow to apply constraints to registerFunction"); - it("Should allow to apply constraints to registerAutoFactory"); - it("Should allow to apply constraints to registerFactory"); - it("Should allow to apply constraints to registerProvider"); + it("Should allow to apply constraints to registerSelf", () => { + + class Katana { + public name: string; + private _durability: number; + public constructor() { + this.name = "Katana"; + this._durability = 100; + } + public use() { + this._durability = this._durability - 10; + } + public durability() { + return this._durability; + } + } + + class Shuriken { + public name: string; + private _durability: number; + public constructor() { + this.name = "Shuriken"; + this._durability = 100; + } + public use() { + this._durability = this._durability - 10; + } + public durability() { + return this._durability; + } + } + + class Ninja { + public katana: Katana; + public shuriken: Shuriken; + public constructor(katana: Katana, shuriken: Shuriken) { + this.katana = katana; + this.shuriken = shuriken; + } + } + + let kernel = new Kernel(); + let registerSelf = helpers.registerSelf(kernel); + + registerSelf( + [], + (b: interfaces.BindingInWhenOnSyntax) => { b.inSingletonScope(); } + )(Katana); + + registerSelf( + [], + (b: interfaces.BindingInWhenOnSyntax) => { b.inTransientScope(); } + )(Shuriken); + + registerSelf([Katana, Shuriken])(Ninja); + + let ninja1 = kernel.get(Ninja); + expect(ninja1.katana.name).to.eql("Katana"); + expect(ninja1.shuriken.name).to.eql("Shuriken"); + expect(ninja1.katana.durability()).to.eql(100); + expect(ninja1.shuriken.durability()).to.eql(100); + ninja1.katana.use(); + ninja1.shuriken.use(); + expect(ninja1.katana.durability()).to.eql(90); + expect(ninja1.shuriken.durability()).to.eql(90); + + let ninja2 = kernel.get(Ninja); + expect(ninja2.katana.name).to.eql("Katana"); + expect(ninja2.shuriken.name).to.eql("Shuriken"); + expect(ninja2.katana.durability()).to.eql(90); + expect(ninja2.shuriken.durability()).to.eql(100); + ninja2.katana.use(); + ninja2.shuriken.use(); + expect(ninja2.katana.durability()).to.eql(80); + expect(ninja2.shuriken.durability()).to.eql(90); + + }); + + it("Should allow to apply constraints to registerConstructor", () => { + + interface Weapon { + name: string; + } + + interface Warrior { + katana: Katana; + shuriken: Shuriken; + } + + class Katana implements Weapon { + public name: string; + public constructor() { + this.name = "Katana"; + } + } + + class Shuriken implements Weapon { + public name: string; + public constructor() { + this.name = "Shuriken"; + } + } + + class Ninja implements Warrior { + public katana: Weapon; + public shuriken: Weapon; + public constructor(katana: Weapon, shuriken: Weapon) { + this.katana = katana; + this.shuriken = shuriken; + } + } + + let kernel = new Kernel(); + let registerConstructor = helpers.registerConstructor(kernel); + + registerConstructor("Newable", (b: interfaces.BindingWhenOnSyntax) => { + b.whenTargetTagged("throwable", false); + })(Katana); + + registerConstructor("Newable", (b: interfaces.BindingWhenOnSyntax) => { + b.whenTargetTagged("throwable", true); + })(Shuriken); + + registerConstructor("Newable")(Ninja); + + let ninjaConstructor = kernel.get>("Newable"); + let katanaConstructor = kernel.getTagged>("Newable", "throwable", false); + let shurikenConstructor = kernel.getTagged>("Newable", "throwable", true); + + let ninja = new ninjaConstructor(new katanaConstructor(), new shurikenConstructor()); + expect(ninja.katana.name).to.eql("Katana"); + expect(ninja.shuriken.name).to.eql("Shuriken"); + + }); + + it("Should allow to apply constraints to registerConstantValue", () => { + + interface Weapon { + name: string; + } + + interface Warrior { + katana: Katana; + shuriken: Shuriken; + } + + class Katana implements Weapon { + public name: string; + public constructor() { + this.name = "Katana"; + } + } + + class Shuriken implements Weapon { + public name: string; + public constructor() { + this.name = "Shuriken"; + } + } + + class Ninja implements Warrior { + public katana: Weapon; + public shuriken: Weapon; + public constructor(katana: Weapon, shuriken: Weapon) { + this.katana = katana; + this.shuriken = shuriken; + } + } + + let kernel = new Kernel(); + let registerConstantValue = helpers.registerConstantValue(kernel); + let register = helpers.register(kernel); + + registerConstantValue("Weapon", new Katana(), (b: interfaces.BindingWhenOnSyntax) => { + b.whenTargetTagged("throwable", false); + }); + + registerConstantValue("Weapon", new Shuriken(), (b: interfaces.BindingWhenOnSyntax) => { + b.whenTargetTagged("throwable", true); + }); + + register("Warrior", [ + { tagged: { key: "throwable", value: false }, type: "Weapon" }, + { tagged: { key: "throwable", value: true }, type: "Weapon" } + ])(Ninja); + + let ninja = kernel.get("Warrior"); + expect(ninja.katana.name).to.eql("Katana"); + expect(ninja.shuriken.name).to.eql("Shuriken"); + + }); + + it("Should allow to apply constraints to registerDynamicValue", () => { + + interface Weapon { + name: string; + } + + class Katana implements Weapon { + public name: string; + public constructor() { + this.name = "Katana"; + } + } + + class Shuriken implements Weapon { + public name: string; + public constructor() { + this.name = "Shuriken"; + } + } + + interface Warrior { + name: string; + weapon: Weapon; + takeHit(): void; + getHealth(): number; + } + + class Ninja implements Warrior { + public weapon: Weapon; + public name: string; + private _health: number; + public constructor(weapon: Weapon) { + this.name = "Ninja"; + this._health = 100; + this.weapon = weapon; + } + public takeHit() { + this._health = this._health - 10; + } + public getHealth() { + return this._health; + } + } + + class Samurai implements Warrior { + public weapon: Weapon; + public name: string; + private _health: number; + public constructor(weapon: Weapon) { + this.name = "Samurai"; + this._health = 100; + this.weapon = weapon; + } + public takeHit() { + this._health = this._health - 10; + } + public getHealth() { + return this._health; + } + } + + let kernel = new Kernel(); + let registerDynamicValue = helpers.registerDynamicValue(kernel); + + registerDynamicValue( + "Warrior", + () => { return new Ninja(new Shuriken()); }, + (b: interfaces.BindingInWhenOnSyntax) => { b.whenTargetNamed("chinese"); } + ); + + registerDynamicValue( + "Warrior", + () => { return new Samurai(new Katana()); }, + (b: interfaces.BindingInWhenOnSyntax) => { b.whenTargetNamed("japanese"); } + ); + + let ninja1 = kernel.getNamed("Warrior", "chinese"); + expect(ninja1.name).to.eql("Ninja"); + expect(ninja1.weapon.name).to.eql("Shuriken"); + expect(ninja1.getHealth()).to.eql(100); + ninja1.takeHit(); + expect(ninja1.getHealth()).to.eql(90); + + let ninja2 = kernel.getNamed("Warrior", "chinese"); + expect(ninja2.name).to.eql("Ninja"); + expect(ninja2.weapon.name).to.eql("Shuriken"); + expect(ninja2.getHealth()).to.eql(100); + ninja2.takeHit(); + expect(ninja2.getHealth()).to.eql(90); + + let samurai1 = kernel.getNamed("Warrior", "japanese"); + expect(samurai1.name).to.eql("Samurai"); + expect(samurai1.weapon.name).to.eql("Katana"); + expect(samurai1.getHealth()).to.eql(100); + samurai1.takeHit(); + expect(samurai1.getHealth()).to.eql(90); + + let samurai2 = kernel.getNamed("Warrior", "japanese"); + expect(samurai2.name).to.eql("Samurai"); + expect(samurai2.weapon.name).to.eql("Katana"); + expect(samurai2.getHealth()).to.eql(100); + samurai2.takeHit(); + expect(samurai2.getHealth()).to.eql(90); + + }); + + it("Should allow to apply constraints to registerFunction", () => { + + interface Weapon { + name: string; + } + + class Katana implements Weapon { + public name: string; + public constructor() { + this.name = "Katana"; + } + } + + class Shuriken implements Weapon { + public name: string; + public constructor() { + this.name = "Shuriken"; + } + } + + class Ninja { + public katana: Weapon; + public shuriken: Weapon; + private _health: number; + public constructor(getKatana: () => Weapon, getShuriken: () => Weapon) { + this._health = 100; + this.katana = getKatana(); + this.shuriken = getShuriken(); + } + public takeHit() { + this._health = this._health - 10; + } + public getHealth() { + return this._health; + } + } + + let kernel = new Kernel(); + let registerFunction = helpers.registerFunction(kernel); + let register = helpers.register(kernel); + + registerFunction<() => Weapon>( + "Weapon", + () => { return new Katana(); }, + (b: interfaces.BindingWhenOnSyntax<() => Weapon>) => { b.whenTargetTagged("throwable", false); } + ); + + registerFunction<() => Weapon>( + "Weapon", + () => { return new Shuriken(); }, + (b: interfaces.BindingWhenOnSyntax<() => Weapon>) => { b.whenTargetTagged("throwable", true); } + ); + + register( + "Ninja", + [ + { tagged: { key: "throwable", value: false }, type: "Weapon" }, + { tagged: { key: "throwable", value: true }, type: "Weapon" } + ] + )(Ninja); + + + let ninja1 = kernel.get("Ninja"); + expect(ninja1.katana.name).to.eql("Katana"); + expect(ninja1.shuriken.name).to.eql("Shuriken"); + expect(ninja1.getHealth()).to.eql(100); + ninja1.takeHit(); + expect(ninja1.getHealth()).to.eql(90); + + let ninja2 = kernel.get("Ninja"); + expect(ninja2.katana.name).to.eql("Katana"); + expect(ninja2.shuriken.name).to.eql("Shuriken"); + expect(ninja2.getHealth()).to.eql(100); + ninja2.takeHit(); + expect(ninja2.getHealth()).to.eql(90); + + }); + + it("Should allow to apply constraints to registerAutoFactory", () => { + + interface Weapon { + name: string; + } + + class Katana implements Weapon { + public name: string; + public constructor() { + this.name = "Katana"; + } + } + + class Shuriken implements Weapon { + public name: string; + public constructor() { + this.name = "Shuriken"; + } + } + + class Ninja { + public katana: Weapon; + public shuriken: Weapon; + private _health: number; + public constructor( + katanaFactory: interfaces.Factory, + shurikenFactory: interfaces.Factory + ) { + this._health = 100; + this.katana = katanaFactory(); + this.shuriken = shurikenFactory(); + } + public takeHit() { + this._health = this._health - 10; + } + public getHealth() { + return this._health; + } + } + + let kernel = new Kernel(); + let registerAutoFactory = helpers.registerAutoFactory(kernel); + let register = helpers.register(kernel); + register("Katana")(Katana); + register("Shuriken")(Shuriken); + registerAutoFactory, Katana>( + "Factory", + "Katana", + (b: interfaces.BindingWhenOnSyntax<() => Weapon>) => { b.whenTargetTagged("throwable", false); } + ); + + registerAutoFactory, Shuriken>( + "Factory", + "Shuriken", + (b: interfaces.BindingWhenOnSyntax<() => Weapon>) => { b.whenTargetTagged("throwable", true); } + ); + + register( + "Ninja", + [ + { tagged: { key: "throwable", value: false }, type: "Factory" }, + { tagged: { key: "throwable", value: true }, type: "Factory" } + ] + )(Ninja); + + let ninja1 = kernel.get("Ninja"); + expect(ninja1.katana.name).to.eql("Katana"); + expect(ninja1.shuriken.name).to.eql("Shuriken"); + expect(ninja1.getHealth()).to.eql(100); + ninja1.takeHit(); + expect(ninja1.getHealth()).to.eql(90); + + }); + + it("Should allow to apply constraints to registerFactory", () => { + + interface Weapon { + name: string; + } + + class Katana implements Weapon { + public name: string; + public constructor() { + this.name = "Katana"; + } + } + + class Shuriken implements Weapon { + public name: string; + public constructor() { + this.name = "Shuriken"; + } + } + + class Ninja { + public katana: Weapon; + public shuriken: Weapon; + private _health: number; + public constructor( + katanaFactory: interfaces.Factory, + shurikenFactory: interfaces.Factory + ) { + this._health = 100; + this.katana = katanaFactory(); + this.shuriken = shurikenFactory(); + } + public takeHit() { + this._health = this._health - 10; + } + public getHealth() { + return this._health; + } + } + + let kernel = new Kernel(); + let registerFactory = helpers.registerFactory(kernel); + let register = helpers.register(kernel); + register("Katana")(Katana); + register("Shuriken")(Shuriken); + + registerFactory, Weapon>( + "Factory", + (context: interfaces.Context) => { + return () => { + return context.kernel.get("Katana"); + }; + }, + (b: interfaces.BindingWhenOnSyntax>) => { b.whenTargetTagged("throwable", false); } + ); + + registerFactory, Weapon>( + "Factory", + (context: interfaces.Context) => { + return () => { + return context.kernel.get("Shuriken"); + }; + }, + (b: interfaces.BindingWhenOnSyntax>) => { b.whenTargetTagged("throwable", true); } + ); + + register( + "Ninja", + [ + { tagged: { key: "throwable", value: false }, type: "Factory" }, + { tagged: { key: "throwable", value: true }, type: "Factory" } + ] + )(Ninja); + + let ninja1 = kernel.get("Ninja"); + expect(ninja1.katana.name).to.eql("Katana"); + expect(ninja1.shuriken.name).to.eql("Shuriken"); + expect(ninja1.getHealth()).to.eql(100); + ninja1.takeHit(); + expect(ninja1.getHealth()).to.eql(90); + + }); + + it("Should allow to apply constraints to registerProvider", () => { + + interface Weapon { + name: string; + } + + class Katana implements Weapon { + public name: string; + public constructor() { + this.name = "Katana"; + } + } + + class Shuriken implements Weapon { + public name: string; + public constructor() { + this.name = "Shuriken"; + } + } + + class Ninja { + + public katana: Weapon; + public shuriken: Weapon; + public katanaProvider: interfaces.Provider; + public shurikenProvider: interfaces.Provider; + private _health: number; + + public constructor( + katanaProvider: interfaces.Provider, + shurikenProvider: interfaces.Provider + ) { + this._health = 100; + this.katana = null; + this.shuriken = null; + this.katanaProvider = katanaProvider; + this.shurikenProvider = shurikenProvider; + } + + public takeHit() { + this._health = this._health - 10; + } + + public getHealth() { + return this._health; + } + + } + + let kernel = new Kernel(); + let registerProvider = helpers.registerProvider(kernel); + let register = helpers.register(kernel); + + register("Katana")(Katana); + register("Shuriken")(Shuriken); + + registerProvider, Weapon>( + "Provider", + (context) => { + return () => { + return new Promise((resolve) => { + let katana = context.kernel.get("Katana"); + resolve(katana); + }); + }; + }, + (b: interfaces.BindingWhenOnSyntax>) => { b.whenTargetTagged("throwable", false); } + ); + + registerProvider, Weapon>( + "Provider", + (context) => { + return () => { + return new Promise((resolve) => { + let katana = context.kernel.get("Shuriken"); + resolve(katana); + }); + }; + }, + (b: interfaces.BindingWhenOnSyntax>) => { b.whenTargetTagged("throwable", true); } + ); + + register( + "Ninja", + [ + { tagged: { key: "throwable", value: false }, type: "Provider" }, + { tagged: { key: "throwable", value: true }, type: "Provider" } + ] + )(Ninja); + + let ninja1 = kernel.get("Ninja"); + expect(ninja1.katana).to.eql(null); + expect(ninja1.shuriken).to.eql(null); + expect(ninja1.getHealth()).to.eql(100); + ninja1.takeHit(); + expect(ninja1.getHealth()).to.eql(90); + + ninja1.katanaProvider().then((katana: Katana) => { + ninja1.katana = katana; + expect(ninja1.katana.name).to.eql("Katana"); + }); + + ninja1.shurikenProvider().then((shuriken: Shuriken) => { + ninja1.shuriken = shuriken; + expect(ninja1.shuriken.name).to.eql("Katana"); + }); + + }); }); diff --git a/test/register.test.ts b/test/register.test.ts index dc41ac0..0fa952b 100644 --- a/test/register.test.ts +++ b/test/register.test.ts @@ -449,7 +449,7 @@ describe("Register Helper", () => { register("Katana")(Katana); register("Shuriken")(Shuriken); - registerProvider, Katana>("Factory", (context) => { + registerProvider, Katana>("Provider", (context) => { return () => { return new Promise((resolve) => { let katana = context.kernel.get("Katana"); @@ -458,7 +458,7 @@ describe("Register Helper", () => { }; }); - registerProvider, Shuriken>("Factory", (context) => { + registerProvider, Shuriken>("Provider", (context) => { return () => { return new Promise((resolve) => { let katana = context.kernel.get("Shuriken"); @@ -467,7 +467,7 @@ describe("Register Helper", () => { }; }); - register("Ninja", ["Factory", "Factory"])(Ninja); + register("Ninja", ["Provider", "Provider"])(Ninja); let ninja1 = kernel.get("Ninja"); expect(ninja1.katana).to.eql(null);