-
Notifications
You must be signed in to change notification settings - Fork 726
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1310 from inversify/fix/issue-1190-when-target-is…
…-default-with-optional-injection fix: update Target.isTagged to exclude optional
- Loading branch information
Showing
5 changed files
with
108 additions
and
13 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
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