Skip to content

Commit

Permalink
fix angular#86: added a check in the annotate helper to detect if is …
Browse files Browse the repository at this point in the history
…a property of the function's prototype
  • Loading branch information
Markuz gj committed Dec 15, 2014
1 parent 70606e1 commit da25860
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/annotations.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
41 changes: 40 additions & 1 deletion test/injector.spec.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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 {}

Expand Down

0 comments on commit da25860

Please sign in to comment.