-
Notifications
You must be signed in to change notification settings - Fork 713
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/inversify/InversifyJS int…
…o fix/issue-928-constructor-injection-broken
- Loading branch information
Showing
7 changed files
with
258 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { expect } from "chai"; | ||
import { injectable, inject, optional, Container, named } from "../../src/inversify"; | ||
|
||
describe("Issue 1190", () => { | ||
|
||
it('should inject a katana as default weapon to ninja', () => { | ||
const TYPES = { | ||
Weapon: "Weapon" | ||
}; | ||
|
||
const TAG = { | ||
throwable: "throwable" | ||
}; | ||
|
||
interface Weapon { | ||
name: string; | ||
} | ||
|
||
@injectable() | ||
class Katana implements Weapon { | ||
public name: string; | ||
public constructor() { | ||
this.name = "Katana"; | ||
} | ||
} | ||
|
||
@injectable() | ||
class Shuriken implements Weapon { | ||
public name: string; | ||
public constructor() { | ||
this.name = "Shuriken"; | ||
} | ||
} | ||
|
||
@injectable() | ||
class Ninja { | ||
public name: string; | ||
public katana: Katana; | ||
public shuriken: Shuriken; | ||
public constructor( | ||
@inject(TYPES.Weapon) @optional() katana: Weapon, | ||
@inject(TYPES.Weapon) @named(TAG.throwable) shuriken: Weapon | ||
) { | ||
this.name = "Ninja"; | ||
this.katana = katana; | ||
this.shuriken = shuriken; | ||
} | ||
} | ||
|
||
const container = new Container(); | ||
|
||
container.bind<Weapon>(TYPES.Weapon).to(Katana).whenTargetIsDefault(); | ||
container.bind<Weapon>(TYPES.Weapon).to(Shuriken).whenTargetNamed(TAG.throwable); | ||
|
||
container.bind<Ninja>("Ninja").to(Ninja); | ||
|
||
const ninja = container.get<Ninja>("Ninja"); | ||
|
||
expect(ninja.katana).to.deep.eq(new Katana()); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
import { expect } from "chai"; | ||
import * as sinon from "sinon"; | ||
import { Container, injectable, interfaces } from "../../src/inversify"; | ||
|
||
describe("Issue 1297", () => { | ||
it('should call onActivation once if the service is a constant value binding', () => { | ||
const container = new Container(); | ||
|
||
const onActivationHandlerSpy = sinon.spy< | ||
(ctx: interfaces.Context, message: string) => string | ||
>((_ctx: interfaces.Context, message: string) => message); | ||
|
||
container.bind("message") | ||
.toConstantValue("Hello world") | ||
.onActivation(onActivationHandlerSpy); | ||
|
||
container.get("message"); | ||
container.get("message"); | ||
|
||
expect(onActivationHandlerSpy.callCount).to.eq(1); | ||
}); | ||
|
||
it('should call onActivation once if the service is a factory binding', () => { | ||
|
||
@injectable() | ||
class Katana { | ||
public hit() { | ||
return "cut!"; | ||
} | ||
} | ||
|
||
const container = new Container(); | ||
|
||
const onActivationHandlerSpy = sinon.spy< | ||
(ctx: interfaces.Context, instance: interfaces.Factory<Katana>) => interfaces.Factory<Katana> | ||
>((_ctx: interfaces.Context, instance: interfaces.Factory<Katana>) => instance); | ||
|
||
container.bind<Katana>("Katana").to(Katana); | ||
|
||
container.bind<interfaces.Factory<Katana>>("Factory<Katana>").toFactory<Katana>((context) => | ||
() => | ||
context.container.get<Katana>("Katana")).onActivation(onActivationHandlerSpy); | ||
|
||
container.get("Factory<Katana>"); | ||
container.get("Factory<Katana>"); | ||
|
||
expect(onActivationHandlerSpy.callCount).to.eq(1); | ||
}); | ||
|
||
it('should call onActivation once if the service is an auto factory binding', () => { | ||
|
||
@injectable() | ||
class Katana { | ||
public hit() { | ||
return "cut!"; | ||
} | ||
} | ||
|
||
const container = new Container(); | ||
|
||
const onActivationHandlerSpy = sinon.spy< | ||
(ctx: interfaces.Context, instance: interfaces.Factory<Katana>) => interfaces.Factory<Katana> | ||
>((_ctx: interfaces.Context, instance: interfaces.Factory<Katana>) => instance); | ||
|
||
container.bind<Katana>("Katana").to(Katana); | ||
|
||
container.bind<interfaces.Factory<Katana>>("Factory<Katana>") | ||
.toAutoFactory<Katana>("Katana").onActivation(onActivationHandlerSpy); | ||
|
||
container.get("Factory<Katana>"); | ||
container.get("Factory<Katana>"); | ||
|
||
expect(onActivationHandlerSpy.callCount).to.eq(1); | ||
}); | ||
|
||
it('should call onActivation once if the service is a function binding', () => { | ||
|
||
const container = new Container(); | ||
|
||
const onActivationHandlerSpy = sinon.spy< | ||
(ctx: interfaces.Context, messageGenerator: () => string) => () => string | ||
>((_ctx: interfaces.Context, messageGenerator: () => string) => messageGenerator); | ||
|
||
container.bind<() => string>("message") | ||
.toFunction(() => "Hello world") | ||
.onActivation(onActivationHandlerSpy); | ||
|
||
container.get("message"); | ||
container.get("message"); | ||
|
||
expect(onActivationHandlerSpy.callCount).to.eq(1); | ||
}); | ||
|
||
it('should call onActivation once if the service is a constructor binding', () => { | ||
|
||
@injectable() | ||
class Katana { | ||
public hit() { | ||
return "cut!"; | ||
} | ||
} | ||
|
||
const container = new Container(); | ||
|
||
const onActivationHandlerSpy = sinon.spy< | ||
(ctx: interfaces.Context, injectableObj: unknown) => unknown | ||
>((_ctx: interfaces.Context, injectableObj: unknown) => injectableObj); | ||
|
||
container.bind("Katana") | ||
.toConstructor<Katana>(Katana) | ||
.onActivation(onActivationHandlerSpy); | ||
|
||
container.get("Katana"); | ||
container.get("Katana"); | ||
|
||
expect(onActivationHandlerSpy.callCount).to.eq(1); | ||
}); | ||
|
||
it('should call onActivation once if the service is a provider binding', () => { | ||
|
||
@injectable() | ||
class Katana { | ||
public hit() { | ||
return "cut!"; | ||
} | ||
} | ||
|
||
const container = new Container(); | ||
|
||
const onActivationHandlerSpy = sinon.spy< | ||
(ctx: interfaces.Context, injectableObj: unknown) => unknown | ||
>((_ctx: interfaces.Context, injectableObj: unknown) => injectableObj); | ||
|
||
container.bind("Provider<Katana>") | ||
.toProvider<Katana>((context: interfaces.Context) => | ||
() => | ||
Promise.resolve(new Katana())).onActivation(onActivationHandlerSpy); | ||
|
||
container.get("Provider<Katana>"); | ||
container.get("Provider<Katana>"); | ||
|
||
expect(onActivationHandlerSpy.callCount).to.eq(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters