From da25860945328c824af6929674927b82346c8c07 Mon Sep 17 00:00:00 2001 From: Markuz gj Date: Mon, 15 Dec 2014 14:41:13 -0200 Subject: [PATCH] fix #86: added a check in the annotate helper to detect if is a property of the function's prototype --- src/annotations.js | 4 ++++ test/injector.spec.js | 41 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/annotations.js b/src/annotations.js index 2984370..f0b5841 100644 --- a/src/annotations.js +++ b/src/annotations.js @@ -61,6 +61,10 @@ class ProvidePromise extends Provide { // Append annotation on a function or class. // This can be helpful when not using ES6+. function annotate(fn, annotation) { + if (fn.annotations === Object.getPrototypeOf(fn).annotations) { + fn.annotations = [] + } + fn.annotations = fn.annotations || []; fn.annotations.push(annotation); } diff --git a/test/injector.spec.js b/test/injector.spec.js index 50d5813..98c5526 100644 --- a/test/injector.spec.js +++ b/test/injector.spec.js @@ -1,5 +1,5 @@ import {Injector} from '../src/injector'; -import {Inject, Provide, SuperConstructor, InjectLazy, TransientScope} from '../src/annotations'; +import {annotate, Inject, Provide, SuperConstructor, InjectLazy, TransientScope} from '../src/annotations'; import {Car, CyclicEngine} from './fixtures/car'; import {module as houseModule} from './fixtures/house'; @@ -273,6 +273,45 @@ describe('injector', function() { }); + it('should support "super" to call multiple parent constructors with annotate helper', function() { + class Foo {} + class Bar {} + + class Parent { + constructor(foo) { + this.parentFoo = foo; + } + } + annotate(Parent, new Inject(Foo)) + + class Child extends Parent { + constructor(superConstructor, foo) { + superConstructor(); + this.childFoo = foo; + } + } + annotate(Child, new Inject(SuperConstructor, Foo)) + + class GrandChild extends Child { + constructor(bar, superConstructor, foo) { + superConstructor(); + this.grandChildBar = bar; + this.grandChildFoo = foo; + } + } + annotate(GrandChild, new Inject(Bar, SuperConstructor, Foo)) + + + var injector = new Injector(); + var instance = injector.get(GrandChild); + + expect(instance.parentFoo).toBeInstanceOf(Foo); + expect(instance.childFoo).toBeInstanceOf(Foo); + expect(instance.grandChildFoo).toBeInstanceOf(Foo); + expect(instance.grandChildBar).toBeInstanceOf(Bar); + }); + + it('should throw an error when used in a factory function', function() { class Something {}