Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing Up to 100% #1443

Merged
merged 1 commit into from
Mar 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

## [6.0.2]

### Added
Brought tests up to 100% Code Coverage

### Changed
LazyIdentfier Tests

### Fixed
Less than 100% code coverage

## [6.0.1] - 2021-10-14
### Added
- add API method for check dependency only in current container
Expand Down
7 changes: 6 additions & 1 deletion test/annotation/inject.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,9 @@ describe("@inject", () => {
}).to.throw(`${ERROR_MSGS.UNDEFINED_INJECT_ANNOTATION("WithUndefinedInject")}`)
});

});
it('Should unwrap LazyServiceIdentifer', () => {
const unwrapped = lazySwordId.unwrap();
expect(unwrapped).to.be.equal('Sword');
});

});
35 changes: 33 additions & 2 deletions test/resolution/resolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1446,6 +1446,39 @@ describe("Resolve", () => {
expect(resolved).eql(true);
});

it("Should call bind.cache.then on unbind w/ PromiseLike binding", async () => {

const bindStub = sinon.stub().callsFake(() => {
return {
serviceIdentifier: "PromiseLike"
};
});

const stub = sinon.stub().callsFake((bindResolve) => {
bindResolve(bindStub());
});

@injectable()
class PromiseLike {
public then() {
return {
then: stub
};
}
}

const container = new Container();

container.bind("PromiseLike").toConstantValue(new PromiseLike());

container.getAsync('PromiseLike');

container.unbindAll();

sinon.assert.calledOnce(stub);
sinon.assert.calledOnce(bindStub);
});

it("Should not allow transient construction with async preDestroy", async () => {
@injectable()
class Destroyable {
Expand Down Expand Up @@ -2521,8 +2554,6 @@ describe("Resolve", () => {

const subject1 = await container.getAsync<UseDate>("UseDate");
const subject2 = await container.getAsync<UseDate>("UseDate");
// tslint:disable-next-line:no-console
console.log(subject1, subject2);
expect(subject1.doSomething() === subject2.doSomething()).eql(false);
});

Expand Down
12 changes: 12 additions & 0 deletions test/syntax/binding_to_syntax.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as ERROR_MSGS from "../../src/constants/error_msgs";
import { BindingScopeEnum, BindingTypeEnum } from "../../src/constants/literal_types";
import { interfaces } from "../../src/interfaces/interfaces";
import { BindingToSyntax } from "../../src/syntax/binding_to_syntax";
import sinon from "sinon";

describe("BindingToSyntax", () => {

Expand Down Expand Up @@ -79,6 +80,17 @@ describe("BindingToSyntax", () => {
expect(binding.type).eql(BindingTypeEnum.Factory);
expect(binding.factory).not.to.eql(null);

const mockContext = {
container: {
getNamed: sinon.stub()
}
};

if (binding.factory !== null) {
binding.factory((mockContext as unknown as interfaces.Context))(ninjaIdentifier);
sinon.assert.calledOnce(mockContext.container.getNamed);
}

bindingToSyntax.toProvider<Ninja>((context: interfaces.Context) =>
() =>
new Promise<Ninja>((resolve) => {
Expand Down
14 changes: 14 additions & 0 deletions test/syntax/binding_when_syntax.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ describe("BindingWhenSyntax", () => {

});

it("Should have false constraint binding null request whenTargetIsDefault", () => {

interface Weapon {
name: string;
}

const shurikenBinding = new Binding<Weapon>("Weapon", BindingScopeEnum.Transient);
const shurikenBindingWhenSyntax = new BindingWhenSyntax<Weapon>(shurikenBinding);

shurikenBindingWhenSyntax.whenTargetIsDefault();
expect(shurikenBinding.constraint(null)).eql(false);

});

it("Should be able to constraint a binding to a named target", () => {

interface Ninja { }
Expand Down
59 changes: 59 additions & 0 deletions test/utils/reflection.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { expect } from "chai";
import { injectable, inject, LazyServiceIdentifer, Container } from '../../src/inversify';
import { getDependencies } from '../../src/planning/reflection_utils';
import { MetadataReader } from "../../src/planning/metadata_reader";
import sinon from "sinon";

describe('Reflection Utilities Unit Tests', () => {

it('Should unwrap LazyServiceIdentifier in getConstructorArgsAsTarget', () => {

interface Ninja {
fight(): string;
}

interface Katana {
hit(): string;
}

@injectable()
class Katana implements Katana {
public hit() {
return "cut!";
}
}

const TYPES = {
Katana: Symbol.for("Katana"),
Ninja: Symbol.for("Ninja"),
};

@injectable()
class Ninja implements Ninja {

private _katana: Katana;

public constructor(
@inject(new LazyServiceIdentifer(() => TYPES.Katana)) katana: Katana,
) {
this._katana = katana;
}

public fight() { return this._katana.hit(); }

}

const container = new Container();
container.bind<Ninja>(TYPES.Ninja).to(Ninja);
container.bind<Katana>(TYPES.Katana).to(Katana);

const unwrapSpy = sinon.spy(LazyServiceIdentifer.prototype, 'unwrap');

const dependencies = getDependencies(new MetadataReader(), Ninja);

expect(dependencies.length).to.be.eql(1);
sinon.assert.calledOnce(unwrapSpy);

sinon.restore();
});
});