angular-docs-cn/modules/angular2/test/di/async_spec.js

195 lines
6.0 KiB
JavaScript
Raw Normal View History

import {
AsyncTestCompleter,
beforeEach,
ddescribe,
describe,
expect,
iit,
inject,
it,
xit,
} from 'angular2/test_lib';
import {Injector, bind, Key} from 'angular2/di';
import {Inject, InjectPromise} from 'angular2/src/di/annotations_impl';
import {Promise, PromiseWrapper} from 'angular2/src/facade/async';
2014-10-07 10:34:07 -04:00
class UserList {
}
function fetchUsers() {
2014-10-10 15:44:56 -04:00
return PromiseWrapper.resolve(new UserList());
}
2014-10-07 10:34:07 -04:00
class SynchronousUserList {
}
class UserController {
list:UserList;
constructor(list:UserList) {
this.list = list;
}
}
class AsyncUserController {
userList;
2014-10-10 15:44:56 -04:00
constructor(@InjectPromise(UserList) userList) {
this.userList = userList;
}
}
2014-10-07 10:34:07 -04:00
export function main() {
describe("async injection", function () {
describe("asyncGet", function () {
2014-10-10 15:44:56 -04:00
it('should return a promise', function () {
var injector = Injector.resolveAndCreate([
bind(UserList).toAsyncFactory(fetchUsers)
]);
var p = injector.asyncGet(UserList);
2014-10-10 15:44:56 -04:00
expect(p).toBePromise();
});
2014-10-10 15:44:56 -04:00
it('should return a promise when the binding is sync', function () {
var injector = Injector.resolveAndCreate([
SynchronousUserList
]);
var p = injector.asyncGet(SynchronousUserList);
2014-10-10 15:44:56 -04:00
expect(p).toBePromise();
});
2014-10-10 15:44:56 -04:00
it("should return a promise when the binding is sync (from cache)", function () {
var injector = Injector.resolveAndCreate([
2014-10-07 10:42:27 -04:00
UserList
]);
expect(injector.get(UserList)).toBeAnInstanceOf(UserList);
2014-10-10 15:44:56 -04:00
expect(injector.asyncGet(UserList)).toBePromise();
2014-10-07 10:42:27 -04:00
});
it('should return the injector', inject([AsyncTestCompleter], (async) => {
var injector = Injector.resolveAndCreate([]);
var p = injector.asyncGet(Injector);
2014-10-07 10:34:07 -04:00
p.then(function (injector) {
2014-10-07 10:03:06 -04:00
expect(injector).toBe(injector);
async.done();
2014-10-07 10:03:06 -04:00
});
}));
2014-10-10 15:44:56 -04:00
it('should return a promise when instantiating a sync binding ' +
'with an async dependency', inject([AsyncTestCompleter], (async) => {
var injector = Injector.resolveAndCreate([
bind(UserList).toAsyncFactory(fetchUsers),
UserController
]);
2014-10-07 10:34:07 -04:00
injector.asyncGet(UserController).then(function (userController) {
expect(userController).toBeAnInstanceOf(UserController);
expect(userController.list).toBeAnInstanceOf(UserList);
async.done();
});
}));
it("should create only one instance (async + async)", inject([AsyncTestCompleter], (async) => {
var injector = Injector.resolveAndCreate([
bind(UserList).toAsyncFactory(fetchUsers)
]);
var ul1 = injector.asyncGet(UserList);
var ul2 = injector.asyncGet(UserList);
2014-10-10 15:44:56 -04:00
PromiseWrapper.all([ul1, ul2]).then(function (uls) {
expect(uls[0]).toBe(uls[1]);
async.done();
});
}));
it("should create only one instance (sync + async)", inject([AsyncTestCompleter], (async) => {
var injector = Injector.resolveAndCreate([
UserList
]);
2014-10-10 15:44:56 -04:00
var promise = injector.asyncGet(UserList);
var ul = injector.get(UserList);
2014-10-10 15:44:56 -04:00
expect(promise).toBePromise();
expect(ul).toBeAnInstanceOf(UserList);
2014-10-10 15:44:56 -04:00
promise.then(function (ful) {
expect(ful).toBe(ul);
async.done();
});
}));
it('should show the full path when error happens in a constructor', inject([AsyncTestCompleter], (async) => {
var injector = Injector.resolveAndCreate([
UserController,
bind(UserList).toAsyncFactory(function () {
2014-10-07 10:34:07 -04:00
throw "Broken UserList";
})
]);
2014-10-10 15:44:56 -04:00
var promise = injector.asyncGet(UserController);
PromiseWrapper.then(promise, null, function (e) {
expect(e.message).toContain("Error during instantiation of UserList! (UserController -> UserList)");
async.done();
});
}));
});
describe("get", function () {
2014-10-07 10:34:07 -04:00
it('should throw when instantiating an async binding', function () {
var injector = Injector.resolveAndCreate([
bind(UserList).toAsyncFactory(fetchUsers)
]);
expect(() => injector.get(UserList))
2014-10-10 15:44:56 -04:00
.toThrowError('Cannot instantiate UserList synchronously. It is provided as a promise!');
});
it('should throw when instantiating a sync binding with an async dependency', function () {
var injector = Injector.resolveAndCreate([
bind(UserList).toAsyncFactory(fetchUsers),
UserController
]);
expect(() => injector.get(UserController))
2014-10-10 15:44:56 -04:00
.toThrowError('Cannot instantiate UserList synchronously. It is provided as a promise! (UserController -> UserList)');
});
it('should not throw when instantiating a sync binding with a resolved async dependency',
inject([AsyncTestCompleter], (async) => {
var injector = Injector.resolveAndCreate([
bind(UserList).toAsyncFactory(fetchUsers),
UserController
]);
2015-03-26 10:46:26 -04:00
injector.asyncGet(UserList).then((_) => {
expect(() => { injector.get(UserController); }).not.toThrow();
async.done();
});
}));
2014-10-10 15:44:56 -04:00
it('should resolve synchronously when an async dependency requested as a promise', function () {
var injector = Injector.resolveAndCreate([
bind(UserList).toAsyncFactory(fetchUsers),
AsyncUserController
]);
var controller = injector.get(AsyncUserController);
expect(controller).toBeAnInstanceOf(AsyncUserController);
2014-10-10 15:44:56 -04:00
expect(controller.userList).toBePromise();
});
2014-10-10 15:44:56 -04:00
it('should wrap sync dependencies into promises if required', function () {
var injector = Injector.resolveAndCreate([
bind(UserList).toFactory(() => new UserList()),
AsyncUserController
]);
var controller = injector.get(AsyncUserController);
expect(controller).toBeAnInstanceOf(AsyncUserController);
2014-10-10 15:44:56 -04:00
expect(controller.userList).toBePromise();
});
});
});
}