Permalink
Browse files

fix(dependency-injection): add more tests using cases suggested by @f…

  • Loading branch information...
silbinarywolf committed Dec 13, 2018
1 parent c093756 commit ac4e6f9c70fec07578bfca39398cc4ba4ecfeeac
Showing with 202 additions and 26 deletions.
  1. +202 −26 test/injection.spec.js
@@ -278,44 +278,220 @@ describe('injection', () => {
});

it('test variadic arguments (TypeScript metadata)', function() {
class VariadicParentApp {
static inject() { return [Logger]; }
constructor(logger) {
this.logger = logger;
const container = new Container();
const VariadicArg = Object; // TypeScript emits "Object" type for "...rest" param

class Dep$1 {}
class Dep$2 {}
class Dep$3 {}
class Dep$4 {}
class Dep$5 {}

const ParentOneDep = decorators(autoinject(), Reflect.metadata('design:paramtypes', [Dep$1])).on(
class ParentOneDep {
constructor(dep1: Dep$1) {
this.dep1 = dep1;
}
}
)

const ParentTwoDeps = decorators(autoinject(), Reflect.metadata('design:paramtypes', [Dep$1, Dep$2])).on(
class ParentTwoDeps {
constructor(dep1: Dep$1, dep2: Dep$2) {
this.dep1 = dep1;
this.dep2 = dep2;
}
}
)

class ChildZeroDeps$1 extends ParentOneDep {}
class ChildZeroDeps$2 extends ParentTwoDeps {}

const ChildOneDep$1 = decorators(autoinject(), Reflect.metadata('design:paramtypes', [Dep$3, VariadicArg])).on(
class ChildOneDep$1 extends ParentOneDep {
constructor(dep3: Dep$3, ...rest) {
super(...rest);
this.dep3 = dep3;
}
}
)
{
const a = container.get(ChildOneDep$1);
expect(a.dep1).toEqual(jasmine.any(Dep$1));
expect(a.dep3).toEqual(jasmine.any(Dep$3));
}
Reflect.metadata(ParentApp, 'design:paramtypes', [Logger]);

// TypeScript 3.0 gives "Object" in metadata for "...rest" arguments.
let VariadicChildApp = decorators(autoinject(), Reflect.metadata('design:paramtypes', [Service, Object])).on(
class VariadicChildApp extends VariadicParentApp {
constructor(service, ...rest) {
const ChildOneDep$2 = decorators(autoinject(), Reflect.metadata('design:paramtypes', [Dep$3, VariadicArg])).on(
class ChildOneDep$2 extends ParentTwoDeps {
constructor(dep3: Dep$3, ...rest) {
super(...rest);
this.service = service;
this.dep3 = dep3;
}
});
}
)
{
const a = container.get(ChildOneDep$2);
expect(a.dep1).toEqual(jasmine.any(Dep$1));
expect(a.dep2).toEqual(jasmine.any(Dep$2));
expect(a.dep3).toEqual(jasmine.any(Dep$3));
}

let VariadicGrandchildApp = decorators(autoinject(), Reflect.metadata('design:paramtypes', [SubService1, Object])).on(
class VariadicGrandchildApp extends VariadicChildApp {
constructor(subService, ...rest) {
const ChildTwoDeps$1 = decorators(autoinject(), Reflect.metadata('design:paramtypes', [Dep$3, Dep$4, VariadicArg])).on(
class ChildTwoDeps$1 extends ParentOneDep {
constructor(dep3: Dep$3, dep4: Dep$4, ...rest) {
super(...rest);
this.subService = subService;
this.dep3 = dep3;
this.dep4 = dep4;
}
});
}
)
const ChildTwoDeps$2 = decorators(autoinject(), Reflect.metadata('design:paramtypes', [Dep$3, Dep$4, VariadicArg])).on(
class ChildTwoDeps$2 extends ParentTwoDeps {
constructor(dep3: Dep$3, dep4: Dep$4, ...rest) {
super(...rest);
this.dep3 = dep3;
this.dep4 = dep4;
}
}
)

let container = new Container();
class GrandChildZeroDeps$01 extends ChildZeroDeps$1 {}
{
const a = container.get(GrandChildZeroDeps$01);
expect(a.dep1).toEqual(jasmine.any(Dep$1));
}

let app1 = container.get(VariadicParentApp);
expect(app1.logger).toEqual(jasmine.any(Logger));
class GrandChildZeroDeps$02 extends ChildZeroDeps$2 {}
{
const a = container.get(GrandChildZeroDeps$02);
expect(a.dep1).toEqual(jasmine.any(Dep$1));
expect(a.dep2).toEqual(jasmine.any(Dep$2));
}

let app2 = container.get(VariadicChildApp);
expect(app2.logger).toEqual(jasmine.any(Logger));
expect(app2.service).toEqual(jasmine.any(Service));
class GrandChildZeroDeps$11 extends ChildOneDep$1 {}
{
const a = container.get(GrandChildZeroDeps$11);
expect(a.dep1).toEqual(jasmine.any(Dep$1));
expect(a.dep3).toEqual(jasmine.any(Dep$3));
}

let app3 = container.get(VariadicGrandchildApp);
expect(app3.logger).toEqual(jasmine.any(Logger));
expect(app3.service).toEqual(jasmine.any(Service));
expect(app3.subService).toEqual(jasmine.any(SubService1));
class GrandChildZeroDeps$12 extends ChildOneDep$2 {}
{
const a = container.get(GrandChildZeroDeps$12);
expect(a.dep1).toEqual(jasmine.any(Dep$1));
expect(a.dep2).toEqual(jasmine.any(Dep$2));
expect(a.dep3).toEqual(jasmine.any(Dep$3));
}

class GrandChildZeroDeps$21 extends ChildTwoDeps$1 {}
{
const a = container.get(GrandChildZeroDeps$21);
expect(a.dep1).toEqual(jasmine.any(Dep$1));
expect(a.dep3).toEqual(jasmine.any(Dep$3));
expect(a.dep4).toEqual(jasmine.any(Dep$4));
}

class GrandChildZeroDeps$22 extends ChildTwoDeps$2 {}
{
const a = container.get(GrandChildZeroDeps$22);
expect(a.dep1).toEqual(jasmine.any(Dep$1));
expect(a.dep2).toEqual(jasmine.any(Dep$2));
expect(a.dep3).toEqual(jasmine.any(Dep$3));
expect(a.dep4).toEqual(jasmine.any(Dep$4));
}

const GrandChildOneDep$01 = decorators(autoinject(), Reflect.metadata('design:paramtypes', [Dep$5, VariadicArg])).on(
class GrandChildOneDep$01 extends ChildZeroDeps$1 {
constructor(dep5: Dep$5, ...rest) {
super(...rest);
this.dep5 = dep5;
}
}
)
{
const a = container.get(GrandChildOneDep$01);
expect(a.dep1).toEqual(jasmine.any(Dep$1));
expect(a.dep5).toEqual(jasmine.any(Dep$5));
}

const GrandChildOneDep$02 = decorators(autoinject(), Reflect.metadata('design:paramtypes', [Dep$5, VariadicArg])).on(
class GrandChildOneDep$02 extends ChildZeroDeps$2 {
constructor(dep5: Dep$5, ...rest) {
super(...rest);
this.dep5 = dep5;
}
}
)
{
const a = container.get(GrandChildOneDep$02);
expect(a.dep1).toEqual(jasmine.any(Dep$1));
expect(a.dep2).toEqual(jasmine.any(Dep$2));
expect(a.dep5).toEqual(jasmine.any(Dep$5));
}

const GrandChildOneDep$11 = decorators(autoinject(), Reflect.metadata('design:paramtypes', [Dep$5, VariadicArg])).on(
class GrandChildOneDep$11 extends ChildOneDep$1 {
constructor(dep5: Dep$5, ...rest) {
super(...rest);
this.dep5 = dep5;
}
}
)
{
const a = container.get(GrandChildOneDep$11);
expect(a.dep1).toEqual(jasmine.any(Dep$1));
expect(a.dep3).toEqual(jasmine.any(Dep$3));
expect(a.dep5).toEqual(jasmine.any(Dep$5));
}

const GrandChildOneDep$12 = decorators(autoinject(), Reflect.metadata('design:paramtypes', [Dep$5, VariadicArg])).on(
class GrandChildOneDep$12 extends ChildOneDep$2 {
constructor(dep5: Dep$5, ...rest) {
super(...rest);
this.dep5 = dep5;
}
}
)
{
const a = container.get(GrandChildOneDep$12);
expect(a.dep1).toEqual(jasmine.any(Dep$1));
expect(a.dep2).toEqual(jasmine.any(Dep$2));
expect(a.dep3).toEqual(jasmine.any(Dep$3));
expect(a.dep5).toEqual(jasmine.any(Dep$5));
}

const GrandChildOneDep$21 = decorators(autoinject(), Reflect.metadata('design:paramtypes', [Dep$5, VariadicArg])).on(
class GrandChildOneDep$21 extends ChildTwoDeps$1 {
constructor(dep5: Dep$5, ...rest) {
super(...rest);
this.dep5 = dep5;
}
}
)
{
const a = container.get(GrandChildOneDep$21);
expect(a.dep1).toEqual(jasmine.any(Dep$1));
expect(a.dep3).toEqual(jasmine.any(Dep$3));
expect(a.dep4).toEqual(jasmine.any(Dep$4));
expect(a.dep5).toEqual(jasmine.any(Dep$5));
}

const GrandChildOneDep$22 = decorators(autoinject(), Reflect.metadata('design:paramtypes', [Dep$5, VariadicArg])).on(
class GrandChildOneDep$22 extends ChildTwoDeps$2 {
constructor(dep5: Dep$5, ...rest) {
super(...rest);
this.dep5 = dep5;
}
}
)
{
const a = container.get(GrandChildOneDep$22);
expect(a.dep1).toEqual(jasmine.any(Dep$1));
expect(a.dep2).toEqual(jasmine.any(Dep$2));
expect(a.dep3).toEqual(jasmine.any(Dep$3));
expect(a.dep4).toEqual(jasmine.any(Dep$4));
expect(a.dep5).toEqual(jasmine.any(Dep$5));
}
});
});
});

0 comments on commit ac4e6f9

Please sign in to comment.