From 5efd4a66aa9a969f69c649485846558d42c74eb5 Mon Sep 17 00:00:00 2001 From: Robert Rico Date: Sat, 5 Mar 2022 23:47:22 -0800 Subject: [PATCH] Testing Up to 100% Brought Testing up to 100% Code Coverage Removed Console Log New Lines --- CHANGELOG.md | 11 +++++ test/annotation/inject.test.ts | 7 ++- test/resolution/resolver.test.ts | 35 ++++++++++++++- test/syntax/binding_to_syntax.test.ts | 12 +++++ test/syntax/binding_when_syntax.test.ts | 14 ++++++ test/utils/reflection.test.ts | 59 +++++++++++++++++++++++++ 6 files changed, 135 insertions(+), 3 deletions(-) create mode 100644 test/utils/reflection.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index a3bb9027..ba30949c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/test/annotation/inject.test.ts b/test/annotation/inject.test.ts index 055fe167..12c6b7c4 100644 --- a/test/annotation/inject.test.ts +++ b/test/annotation/inject.test.ts @@ -201,4 +201,9 @@ describe("@inject", () => { }).to.throw(`${ERROR_MSGS.UNDEFINED_INJECT_ANNOTATION("WithUndefinedInject")}`) }); -}); \ No newline at end of file + it('Should unwrap LazyServiceIdentifer', () => { + const unwrapped = lazySwordId.unwrap(); + expect(unwrapped).to.be.equal('Sword'); + }); + +}); diff --git a/test/resolution/resolver.test.ts b/test/resolution/resolver.test.ts index 92216a90..b8695aa3 100644 --- a/test/resolution/resolver.test.ts +++ b/test/resolution/resolver.test.ts @@ -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 { @@ -2521,8 +2554,6 @@ describe("Resolve", () => { const subject1 = await container.getAsync("UseDate"); const subject2 = await container.getAsync("UseDate"); - // tslint:disable-next-line:no-console - console.log(subject1, subject2); expect(subject1.doSomething() === subject2.doSomething()).eql(false); }); diff --git a/test/syntax/binding_to_syntax.test.ts b/test/syntax/binding_to_syntax.test.ts index 88921bd6..9cd97013 100644 --- a/test/syntax/binding_to_syntax.test.ts +++ b/test/syntax/binding_to_syntax.test.ts @@ -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", () => { @@ -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((context: interfaces.Context) => () => new Promise((resolve) => { diff --git a/test/syntax/binding_when_syntax.test.ts b/test/syntax/binding_when_syntax.test.ts index 28482c1b..f9140a63 100644 --- a/test/syntax/binding_when_syntax.test.ts +++ b/test/syntax/binding_when_syntax.test.ts @@ -45,6 +45,20 @@ describe("BindingWhenSyntax", () => { }); + it("Should have false constraint binding null request whenTargetIsDefault", () => { + + interface Weapon { + name: string; + } + + const shurikenBinding = new Binding("Weapon", BindingScopeEnum.Transient); + const shurikenBindingWhenSyntax = new BindingWhenSyntax(shurikenBinding); + + shurikenBindingWhenSyntax.whenTargetIsDefault(); + expect(shurikenBinding.constraint(null)).eql(false); + + }); + it("Should be able to constraint a binding to a named target", () => { interface Ninja { } diff --git a/test/utils/reflection.test.ts b/test/utils/reflection.test.ts new file mode 100644 index 00000000..7b5980e5 --- /dev/null +++ b/test/utils/reflection.test.ts @@ -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(TYPES.Ninja).to(Ninja); + container.bind(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(); + }); +});