Skip to content

Commit

Permalink
Add some test cases & solve coverage issues (#538)
Browse files Browse the repository at this point in the history
* Downgraded gulp-mocha back to 3.0.1 as it is unstable and causes issues.

* Added test case for #537

* Added test case for #531

* Updated PR template

* Updated PR template
  • Loading branch information
remojansen authored Apr 23, 2017
1 parent 17e4541 commit 7a6a60e
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 1 deletion.
9 changes: 9 additions & 0 deletions PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,40 @@
<!--- Provide a general summary of your changes in the Title above -->

## Description

<!--- Describe your changes in detail -->

## Related Issue

<!--- This project only accepts pull requests related to open issues -->
<!--- If suggesting a new feature or change, please discuss it in an issue first -->
<!--- If fixing a bug, there should be an issue describing it with steps to reproduce -->
<!--- Please link to the issue here: -->

## Motivation and Context

<!--- Why is this change required? What problem does it solve? -->

## How Has This Been Tested?

<!--- Please describe in detail how you tested your changes. -->
<!--- Include details of your testing environment, and the tests you ran to -->
<!--- see how your change affects other areas of the code, etc. -->

## Types of changes

<!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: -->

- [ ] Updated docs / Refactor code / Added a tests case (non-breaking change)
- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)

## Checklist:

<!--- Go over all the following points, and put an `x` in all the boxes that apply. -->
<!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! -->

- [ ] My code follows the code style of this project.
- [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"es6-symbol": "^3.1.0",
"gulp": "^3.9.0",
"gulp-istanbul": "^1.0.0",
"gulp-mocha": "^4.0.0",
"gulp-mocha": "3.0.1",
"gulp-rename": "^1.2.2",
"gulp-sourcemaps": "^2.2.1",
"gulp-tslint": "^7.0.0",
Expand Down
116 changes: 116 additions & 0 deletions test/bugs/bugs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as METADATA_KEY from "../../src/constants/metadata_keys";
import { getDependencies } from "../../src/planning/reflection_utils";
import { MetadataReader } from "../../src/planning/metadata_reader";
import {
decorate,
Container,
injectable,
named,
Expand Down Expand Up @@ -620,4 +621,119 @@ describe("Bugs", () => {

});

it("Should be able apply inject to property shurtcut", () => {

interface Weapon {
use(): string;
}

@injectable()
class Katana implements Weapon {
public use() {
return "Used Katana!";
}
}

@injectable()
class Ninja {
public constructor(@inject("Weapon") @named("sword") private _weapon: Weapon) {
//
}
public fight() {
return this._weapon.use();
}
}

const container = new Container();
container.bind<Weapon>("Weapon").to(Katana).whenTargetNamed("sword");
container.bind<Ninja>(Ninja).toSelf();

let ninja = container.get<Ninja>(Ninja);
expect(ninja.fight()).eql("Used Katana!");

});

it("Should be able inject into abstract base class without decorators", () => {

let TYPES = {
Warrior: "Warrior",
Weapon: "Weapon"
};

let TAGS = {
Primary: "Primary",
Priority: "Priority",
Secondary: "Secondary"
};

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";
}
}

interface Warrior {
name: string;
primaryWeapon: Weapon;
}

abstract class BaseWarrior implements Warrior {

public name: string;
public primaryWeapon: Weapon;

public constructor(@unmanaged() name: string) {
this.name = name;
}

}

// @injectable()
decorate(injectable(), BaseWarrior);

// @inject(TYPES.Weapon)
inject(TYPES.Weapon)(BaseWarrior.prototype, "primaryWeapon");

// @tagged(TAGS.Priority, TAGS.Primary)
tagged(TAGS.Priority, TAGS.Primary)(BaseWarrior.prototype, "primaryWeapon");

@injectable()
class Samurai extends BaseWarrior {

@inject(TYPES.Weapon)
@tagged(TAGS.Priority, TAGS.Secondary)
public secondaryWeapon: Weapon;

public constructor() {
super("Samurai");
}
}

let container = new Container();
container.bind<Warrior>(TYPES.Warrior).to(Samurai);
container.bind<Weapon>(TYPES.Weapon).to(Katana).whenTargetTagged(TAGS.Priority, TAGS.Primary);
container.bind<Weapon>(TYPES.Weapon).to(Shuriken).whenTargetTagged(TAGS.Priority, TAGS.Secondary);

let samurai = container.get<Samurai>(TYPES.Warrior);
expect(samurai.name).to.eql("Samurai");
expect(samurai.secondaryWeapon).not.to.eql(undefined);
expect(samurai.secondaryWeapon.name).to.eql("Shuriken");
expect(samurai.primaryWeapon).not.to.eql(undefined);
expect(samurai.primaryWeapon.name).to.eql("Katana");
});

});

0 comments on commit 7a6a60e

Please sign in to comment.