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