angular-cn/modules/angular2/test/di/injector_spec.ts

404 lines
14 KiB
TypeScript
Raw Normal View History

2015-05-22 18:39:28 -04:00
import {isBlank, BaseException, stringify} from 'angular2/src/facade/lang';
import {describe, ddescribe, it, iit, expect, beforeEach} from 'angular2/test_lib';
2015-05-22 18:39:28 -04:00
import {
Injector,
bind,
ResolvedBinding,
Key,
forwardRef,
DependencyAnnotation,
Injectable
} from 'angular2/di';
import {Optional, Inject, InjectLazy} from 'angular2/src/di/decorators';
import * as ann from 'angular2/src/di/annotations_impl';
class CustomDependencyAnnotation extends DependencyAnnotation {}
class Engine {}
2014-10-07 10:34:07 -04:00
class BrokenEngine {
2015-05-22 18:39:28 -04:00
constructor() { throw new BaseException("Broken Engine"); }
}
2014-10-07 10:34:07 -04:00
2015-05-22 18:39:28 -04:00
class DashboardSoftware {}
2014-10-07 10:34:07 -04:00
2015-05-22 18:39:28 -04:00
@Injectable()
class Dashboard {
2014-10-07 10:34:07 -04:00
constructor(software: DashboardSoftware) {}
}
2015-05-22 18:39:28 -04:00
class TurboEngine extends Engine {}
2015-05-22 18:39:28 -04:00
@Injectable()
class Car {
2015-05-22 18:39:28 -04:00
engine: Engine;
constructor(engine: Engine) { this.engine = engine; }
}
2015-05-22 18:39:28 -04:00
@Injectable()
2014-10-06 13:45:24 -04:00
class CarWithLazyEngine {
engineFactory;
2015-05-22 18:39:28 -04:00
constructor(@InjectLazy(Engine) engineFactory) { this.engineFactory = engineFactory; }
2014-10-06 13:45:24 -04:00
}
2015-05-22 18:39:28 -04:00
@Injectable()
class CarWithOptionalEngine {
engine;
2015-05-22 18:39:28 -04:00
constructor(@Optional() engine: Engine) { this.engine = engine; }
}
2015-05-22 18:39:28 -04:00
@Injectable()
class CarWithDashboard {
2015-05-22 18:39:28 -04:00
engine: Engine;
dashboard: Dashboard;
constructor(engine: Engine, dashboard: Dashboard) {
this.engine = engine;
this.dashboard = dashboard;
}
}
2015-05-22 18:39:28 -04:00
@Injectable()
class SportsCar extends Car {
2015-05-22 18:39:28 -04:00
engine: Engine;
constructor(engine: Engine) { super(engine); }
}
2015-05-22 18:39:28 -04:00
@Injectable()
class CarWithInject {
2015-05-22 18:39:28 -04:00
engine: Engine;
constructor(@Inject(TurboEngine) engine: Engine) { this.engine = engine; }
}
2015-05-22 18:39:28 -04:00
@Injectable()
class CyclicEngine {
2015-05-22 18:39:28 -04:00
constructor(car: Car) {}
}
class NoAnnotations {
2014-10-07 10:34:07 -04:00
constructor(secretDependency) {}
}
export function main() {
2015-05-22 18:39:28 -04:00
describe('injector', function() {
2015-05-22 18:39:28 -04:00
it('should instantiate a class without dependencies', function() {
var injector = Injector.resolveAndCreate([Engine]);
var engine = injector.get(Engine);
expect(engine).toBeAnInstanceOf(Engine);
});
2015-05-22 18:39:28 -04:00
it('should resolve dependencies based on type information', function() {
var injector = Injector.resolveAndCreate([Engine, Car]);
var car = injector.get(Car);
expect(car).toBeAnInstanceOf(Car);
expect(car.engine).toBeAnInstanceOf(Engine);
});
2015-05-22 18:39:28 -04:00
it('should resolve dependencies based on @Inject annotation', function() {
var injector = Injector.resolveAndCreate([TurboEngine, Engine, CarWithInject]);
var car = injector.get(CarWithInject);
expect(car).toBeAnInstanceOf(CarWithInject);
expect(car.engine).toBeAnInstanceOf(TurboEngine);
});
2015-05-22 18:39:28 -04:00
it('should throw when no type and not @Inject', function() {
expect(() => Injector.resolveAndCreate([NoAnnotations]))
.toThrowError('Cannot resolve all parameters for NoAnnotations. ' +
'Make sure they all have valid type or annotations.');
});
2015-05-22 18:39:28 -04:00
it('should cache instances', function() {
var injector = Injector.resolveAndCreate([Engine]);
var e1 = injector.get(Engine);
var e2 = injector.get(Engine);
expect(e1).toBe(e2);
});
2015-05-22 18:39:28 -04:00
it('should bind to a value', function() {
var injector = Injector.resolveAndCreate([bind(Engine).toValue("fake engine")]);
var engine = injector.get(Engine);
expect(engine).toEqual("fake engine");
});
2015-05-22 18:39:28 -04:00
it('should bind to a factory', function() {
function sportsCarFactory(e) { return new SportsCar(e); }
2015-05-22 18:39:28 -04:00
var injector =
Injector.resolveAndCreate([Engine, bind(Car).toFactory(sportsCarFactory, [Engine])]);
var car = injector.get(Car);
expect(car).toBeAnInstanceOf(SportsCar);
expect(car.engine).toBeAnInstanceOf(Engine);
});
it('should bind to an alias', function() {
2015-05-22 18:39:28 -04:00
var injector = Injector.resolveAndCreate(
[Engine, bind(SportsCar).toClass(SportsCar), bind(Car).toAlias(SportsCar)]);
var car = injector.get(Car);
var sportsCar = injector.get(SportsCar);
expect(car).toBeAnInstanceOf(SportsCar);
expect(car).toBe(sportsCar);
});
2015-05-22 18:39:28 -04:00
it('should throw when the aliased binding does not exist', function() {
var injector = Injector.resolveAndCreate([bind('car').toAlias(SportsCar)]);
var e = `No provider for ${stringify(SportsCar)}! (car -> ${stringify(SportsCar)})`;
expect(() => injector.get('car')).toThrowError(e);
});
2015-05-22 18:39:28 -04:00
it('should handle forwardRef in toAlias', function() {
var injector = Injector.resolveAndCreate([
2015-05-22 18:39:28 -04:00
bind('originalEngine')
.toClass(forwardRef(() => Engine)),
bind('aliasedEngine').toAlias(forwardRef(() => 'originalEngine'))
]);
expect(injector.get('aliasedEngine')).toBeAnInstanceOf(Engine);
});
2015-05-22 18:39:28 -04:00
it('should support overriding factory dependencies', function() {
var injector =
Injector
.resolveAndCreate([Engine, bind(Car).toFactory((e) => new SportsCar(e), [Engine])]);
var car = injector.get(Car);
expect(car).toBeAnInstanceOf(SportsCar);
expect(car.engine).toBeAnInstanceOf(Engine);
});
2015-05-22 18:39:28 -04:00
it('should support optional dependencies', function() {
var injector = Injector.resolveAndCreate([CarWithOptionalEngine]);
var car = injector.get(CarWithOptionalEngine);
expect(car.engine).toEqual(null);
});
2015-05-22 18:39:28 -04:00
it("should flatten passed-in bindings", function() {
var injector = Injector.resolveAndCreate([[[Engine, Car]]]);
var car = injector.get(Car);
expect(car).toBeAnInstanceOf(Car);
});
2015-05-22 18:39:28 -04:00
it("should use the last binding " + "when there are multiple bindings for same token",
function() {
var injector = Injector.resolveAndCreate(
[bind(Engine).toClass(Engine), bind(Engine).toClass(TurboEngine)]);
2015-05-22 18:39:28 -04:00
expect(injector.get(Engine)).toBeAnInstanceOf(TurboEngine);
});
2015-05-22 18:39:28 -04:00
it('should use non-type tokens', function() {
var injector = Injector.resolveAndCreate([bind('token').toValue('value')]);
expect(injector.get('token')).toEqual('value');
});
2015-05-22 18:39:28 -04:00
it('should throw when given invalid bindings', function() {
expect(() => Injector.resolveAndCreate(<any>["blah"]))
.toThrowError(
'Invalid binding - only instances of Binding and Type are allowed, got: blah');
expect(() => Injector.resolveAndCreate(<any>[bind("blah")]))
.toThrowError('Invalid binding - only instances of Binding and Type are allowed, ' +
'got: blah');
});
2015-05-22 18:39:28 -04:00
it('should provide itself', function() {
var parent = Injector.resolveAndCreate([]);
var child = parent.resolveAndCreateChild([]);
expect(child.get(Injector)).toBe(child);
});
2015-05-22 18:39:28 -04:00
it('should throw when no provider defined', function() {
var injector = Injector.resolveAndCreate([]);
expect(() => injector.get('NonExisting')).toThrowError('No provider for NonExisting!');
});
2015-05-22 18:39:28 -04:00
it('should show the full path when no provider', function() {
var injector = Injector.resolveAndCreate([CarWithDashboard, Engine, Dashboard]);
2015-05-22 18:39:28 -04:00
expect(() => injector.get(CarWithDashboard))
.toThrowError(
`No provider for DashboardSoftware! (${stringify(CarWithDashboard)} -> ${stringify(Dashboard)} -> DashboardSoftware)`);
});
2015-05-22 18:39:28 -04:00
it('should throw when trying to instantiate a cyclic dependency', function() {
var injector = Injector.resolveAndCreate([Car, bind(Engine).toClass(CyclicEngine)]);
expect(() => injector.get(Car))
2015-05-22 18:39:28 -04:00
.toThrowError(
`Cannot instantiate cyclic dependency! (${stringify(Car)} -> ${stringify(Engine)} -> ${stringify(Car)})`);
expect(() => injector.asyncGet(Car))
2015-05-22 18:39:28 -04:00
.toThrowError(
`Cannot instantiate cyclic dependency! (${stringify(Car)} -> ${stringify(Engine)} -> ${stringify(Car)})`);
});
2015-05-22 18:39:28 -04:00
it('should show the full path when error happens in a constructor', function() {
var injector = Injector.resolveAndCreate([Car, bind(Engine).toClass(BrokenEngine)]);
try {
injector.get(Car);
throw "Must throw";
} catch (e) {
2015-05-22 18:39:28 -04:00
expect(e.message)
.toContain(`Error during instantiation of Engine! (${stringify(Car)} -> Engine)`);
expect(e.cause instanceof BaseException).toBeTruthy();
expect(e.causeKey.token).toEqual(Engine);
}
});
2014-10-06 13:45:24 -04:00
2015-05-22 18:39:28 -04:00
it('should instantiate an object after a failed attempt', function() {
var isBroken = true;
2015-05-22 18:39:28 -04:00
var injector = Injector.resolveAndCreate(
[Car, bind(Engine).toFactory(() => isBroken ? new BrokenEngine() : new Engine())]);
expect(() => injector.get(Car)).toThrowError(new RegExp("Error"));
isBroken = false;
expect(injector.get(Car)).toBeAnInstanceOf(Car);
});
it('should support null values', () => {
var injector = Injector.resolveAndCreate([bind('null').toValue(null)]);
expect(injector.get('null')).toBe(null);
});
2015-05-22 18:39:28 -04:00
describe("default bindings", function() {
it("should be used when no matching binding found", function() {
var injector = Injector.resolveAndCreate([], { defaultBindings: true });
var car = injector.get(Car);
expect(car).toBeAnInstanceOf(Car);
});
2015-05-22 18:39:28 -04:00
it("should use the matching binding when it is available", function() {
var injector =
Injector.resolveAndCreate([bind(Car).toClass(SportsCar)], {defaultBindings: true});
var car = injector.get(Car);
expect(car).toBeAnInstanceOf(SportsCar);
});
});
2014-10-06 13:45:24 -04:00
2015-05-22 18:39:28 -04:00
describe("child", function() {
it('should load instances from parent injector', function() {
var parent = Injector.resolveAndCreate([Engine]);
var child = parent.resolveAndCreateChild([]);
2014-10-06 13:45:24 -04:00
var engineFromParent = parent.get(Engine);
var engineFromChild = child.get(Engine);
expect(engineFromChild).toBe(engineFromParent);
});
2015-05-22 18:39:28 -04:00
it("should not use the child bindings when resolving the dependencies of a parent binding",
function() {
var parent = Injector.resolveAndCreate([Car, Engine]);
var child = parent.resolveAndCreateChild([bind(Engine).toClass(TurboEngine)]);
2015-05-22 18:39:28 -04:00
var carFromChild = child.get(Car);
expect(carFromChild.engine).toBeAnInstanceOf(Engine);
});
2015-05-22 18:39:28 -04:00
it('should create new instance in a child injector', function() {
var parent = Injector.resolveAndCreate([Engine]);
2015-05-22 18:39:28 -04:00
var child = parent.resolveAndCreateChild([bind(Engine).toClass(TurboEngine)]);
2014-10-06 13:45:24 -04:00
var engineFromParent = parent.get(Engine);
var engineFromChild = child.get(Engine);
expect(engineFromParent).not.toBe(engineFromChild);
expect(engineFromChild).toBeAnInstanceOf(TurboEngine);
});
2015-05-22 18:39:28 -04:00
it("should create child injectors without default bindings", function() {
var parent = Injector.resolveAndCreate([], { defaultBindings: true });
var child = parent.resolveAndCreateChild([]);
2015-05-22 18:39:28 -04:00
// child delegates to parent the creation of Car
var childCar = child.get(Car);
var parentCar = parent.get(Car);
expect(childCar).toBe(parentCar);
});
2015-05-08 19:24:17 -04:00
it("should give access to direct parent", () => {
var parent = Injector.resolveAndCreate([]);
var child = parent.resolveAndCreateChild([]);
expect(child.parent).toBe(parent);
});
2014-10-06 13:45:24 -04:00
});
2015-05-22 18:39:28 -04:00
describe("lazy", function() {
it("should create dependencies lazily", function() {
var injector = Injector.resolveAndCreate([Engine, CarWithLazyEngine]);
2014-10-06 13:45:24 -04:00
var car = injector.get(CarWithLazyEngine);
expect(car.engineFactory()).toBeAnInstanceOf(Engine);
});
2015-05-22 18:39:28 -04:00
it("should cache instance created lazily", function() {
var injector = Injector.resolveAndCreate([Engine, CarWithLazyEngine]);
2014-10-06 13:45:24 -04:00
var car = injector.get(CarWithLazyEngine);
var e1 = car.engineFactory();
var e2 = car.engineFactory();
expect(e1).toBe(e2);
});
});
describe('resolve', function() {
it('should resolve and flatten', () => {
var bindings = Injector.resolve([Engine, [BrokenEngine]]);
bindings.forEach(function(b) {
if (isBlank(b)) return; // the result is a sparse array
expect(b instanceof ResolvedBinding).toBe(true);
});
});
it('should resolve forward references', () => {
var bindings = Injector.resolve([
forwardRef(() => Engine),
2015-05-22 18:39:28 -04:00
[bind(forwardRef(() => BrokenEngine)).toClass(forwardRef(() => Engine))],
bind(forwardRef(() => String)).toFactory(() => 'OK', [forwardRef(() => Engine)]),
2015-05-22 18:39:28 -04:00
bind(forwardRef(() => DashboardSoftware))
.toAsyncFactory(() => 123, [forwardRef(() => BrokenEngine)])
]);
var engineBinding = bindings[Key.get(Engine).id];
var brokenEngineBinding = bindings[Key.get(BrokenEngine).id];
var stringBinding = bindings[Key.get(String).id];
var dashboardSoftwareBinding = bindings[Key.get(DashboardSoftware).id];
expect(engineBinding.factory() instanceof Engine).toBe(true);
expect(brokenEngineBinding.factory() instanceof Engine).toBe(true);
expect(stringBinding.dependencies[0].key).toEqual(Key.get(Engine));
expect(dashboardSoftwareBinding.dependencies[0].key).toEqual(Key.get(BrokenEngine));
});
2015-05-22 18:39:28 -04:00
it('should support overriding factory dependencies with dependency annotations', function() {
var bindings = Injector.resolve(
[bind("token")
.toFactory((e) => "result",
[[new ann.Inject("dep"), new CustomDependencyAnnotation()]])]);
var binding = bindings[Key.get("token").id];
2015-05-22 18:39:28 -04:00
expect(binding.dependencies[0].key.token).toEqual("dep");
expect(binding.dependencies[0].properties).toEqual([new CustomDependencyAnnotation()]);
});
});
});
}