-
Notifications
You must be signed in to change notification settings - Fork 21
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
Add inRequestScope decorator inversify/InversifyJS/issues/678 #151
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,50 +19,29 @@ describe("ProvideInSyntax", () => { | |
sandbox.restore(); | ||
}); | ||
|
||
it("Should be able to declare a binding with singleton scope", () => { | ||
|
||
class Ninja { } | ||
let inSingletonScopeExpectation = sinon.expectation.create("inSingletonScope"); | ||
let mockBindingInSyntax = { inSingletonScope: inSingletonScopeExpectation } as any as inversifyInterfaces.BindingInSyntax<any>; | ||
let mockBind = sinon.expectation.create("bind"); | ||
let bindingInSyntaxFunction = | ||
(bind: inversifyInterfaces.Bind, target: any) => { | ||
bind<Ninja>("Ninja"); | ||
return mockBindingInSyntax; | ||
}; | ||
let binding: inversifyInterfaces.Binding<any> = (<any>bindingInSyntaxFunction)._binding; | ||
let provideDoneSyntax = new ProvideDoneSyntax(binding as any); | ||
|
||
let provideInSyntax = new ProvideInSyntax(bindingInSyntaxFunction, provideDoneSyntax); | ||
|
||
provideInSyntax.inSingletonScope().done()(Ninja); | ||
let metadata = Reflect.getMetadata(METADATA_KEY.provide, Reflect)[0]; | ||
metadata.constraint(mockBind); | ||
expect(inSingletonScopeExpectation.calledOnce).to.eql(true, "inSingletonScope was not called exactly once"); | ||
expect(mockBind.calledWith("Ninja")).to.be.eql(true, "mock bind was not called"); | ||
|
||
}); | ||
it("Should be able to declare a binding with transient scope", () => { | ||
|
||
class Ninja { } | ||
let inTransientScopeExpectation = sinon.expectation.create("inTransientScope"); | ||
let mockBindingInSyntax = { inTransientScope: inTransientScopeExpectation } as any as inversifyInterfaces.BindingInSyntax<any>; | ||
let mockBind = sinon.expectation.create("bind"); | ||
let bindingInSyntaxFunction = | ||
(bind: inversifyInterfaces.Bind, target: any) => { | ||
bind<Ninja>("Ninja"); | ||
return mockBindingInSyntax; | ||
}; | ||
let binding: inversifyInterfaces.Binding<any> = (<any>bindingInSyntaxFunction)._binding; | ||
let provideDoneSyntax = new ProvideDoneSyntax(binding as any); | ||
|
||
let provideInSyntax = new ProvideInSyntax(bindingInSyntaxFunction, provideDoneSyntax); | ||
|
||
provideInSyntax.inTransientScope().done()(Ninja); | ||
let metadata = Reflect.getMetadata(METADATA_KEY.provide, Reflect)[0]; | ||
metadata.constraint(mockBind); | ||
expect(inTransientScopeExpectation.calledOnce).to.eql(true, "inTransientScope was not called exactly once"); | ||
expect(mockBind.calledWith("Ninja")).to.be.eql(true, "mock bind was not called"); | ||
|
||
["inSingletonScope", "inRequestScope", "inTransientScope"].forEach(scope => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I combined the shared aspects of the code into one repeated test (the equivalent of a data provider). The weirdest part was calling |
||
it(`Should be able to declare a binding with ${scope} scope`, () => { | ||
|
||
class Ninja { } | ||
let inScopeExpectation = sinon.expectation.create(scope); | ||
let mockBindingInSyntax = { [scope]: inScopeExpectation } as any as inversifyInterfaces.BindingInSyntax<any>; | ||
let mockBind = sinon.expectation.create("bind"); | ||
let bindingInSyntaxFunction = | ||
(bind: inversifyInterfaces.Bind, target: any) => { | ||
bind<Ninja>("Ninja"); | ||
return mockBindingInSyntax; | ||
}; | ||
let binding: inversifyInterfaces.Binding<any> = (<any>bindingInSyntaxFunction)._binding; | ||
let provideDoneSyntax = new ProvideDoneSyntax(binding as any); | ||
|
||
let provideInSyntax: {[scope: string]: any} = new ProvideInSyntax(bindingInSyntaxFunction, provideDoneSyntax); | ||
|
||
provideInSyntax[scope]().done()(Ninja); | ||
let metadata = Reflect.getMetadata(METADATA_KEY.provide, Reflect)[0]; | ||
metadata.constraint(mockBind); | ||
expect(inScopeExpectation.calledOnce).to.eql(true, `${scope} was not called exactly once`); | ||
expect(mockBind.calledWith("Ninja")).to.be.eql(true, "mock bind was not called"); | ||
|
||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pulled this out to reduce duplicate code