Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

fix #86 #87

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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