| 
									
										
										
										
											2016-06-23 09:47:54 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @license | 
					
						
							|  |  |  |  * Copyright Google Inc. All Rights Reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Use of this source code is governed by an MIT-style license that can be | 
					
						
							|  |  |  |  * found in the LICENSE file at https://angular.io/license
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  | import {describe, it, expect, beforeEach, ddescribe, iit, xit, inject,} from '@angular/core/testing/testing_internal'; | 
					
						
							| 
									
										
										
										
											2016-04-28 17:50:03 -07:00
										 |  |  | import {AsyncTestCompleter} from '@angular/core/testing/testing_internal'; | 
					
						
							| 
									
										
										
										
											2016-06-23 16:42:25 -07:00
										 |  |  | import {browserDetection} from '@angular/platform-browser/testing/browser_util'; | 
					
						
							| 
									
										
										
										
											2016-05-01 22:50:37 -07:00
										 |  |  | import {ObservableWrapper, Observable, Subject, EventEmitter, PromiseWrapper} from '../src/async'; | 
					
						
							| 
									
										
										
										
											2015-05-26 17:00:31 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | export function main() { | 
					
						
							|  |  |  |   describe('EventEmitter', () => { | 
					
						
							| 
									
										
										
										
											2015-10-24 18:48:43 -07:00
										 |  |  |     var emitter: EventEmitter<any>; | 
					
						
							| 
									
										
										
										
											2015-05-26 17:00:31 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     beforeEach(() => { emitter = new EventEmitter(); }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |     it('should call the next callback', | 
					
						
							|  |  |  |        inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2015-05-26 17:00:31 -07:00
										 |  |  |          ObservableWrapper.subscribe(emitter, (value) => { | 
					
						
							|  |  |  |            expect(value).toEqual(99); | 
					
						
							|  |  |  |            async.done(); | 
					
						
							|  |  |  |          }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-15 23:58:59 -08:00
										 |  |  |          ObservableWrapper.callEmit(emitter, 99); | 
					
						
							| 
									
										
										
										
											2015-05-26 17:00:31 -07:00
										 |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |     it('should call the throw callback', | 
					
						
							|  |  |  |        inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2015-05-26 17:00:31 -07:00
										 |  |  |          ObservableWrapper.subscribe(emitter, (_) => {}, (error) => { | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |            expect(error).toEqual('Boom'); | 
					
						
							| 
									
										
										
										
											2015-05-26 17:00:31 -07:00
										 |  |  |            async.done(); | 
					
						
							|  |  |  |          }); | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |          ObservableWrapper.callError(emitter, 'Boom'); | 
					
						
							| 
									
										
										
										
											2015-05-26 17:00:31 -07:00
										 |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |     it('should work when no throw callback is provided', | 
					
						
							|  |  |  |        inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2015-05-26 17:00:31 -07:00
										 |  |  |          ObservableWrapper.subscribe(emitter, (_) => {}, (_) => { async.done(); }); | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |          ObservableWrapper.callError(emitter, 'Boom'); | 
					
						
							| 
									
										
										
										
											2015-05-26 17:00:31 -07:00
										 |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |     it('should call the return callback', | 
					
						
							|  |  |  |        inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2015-05-26 17:00:31 -07:00
										 |  |  |          ObservableWrapper.subscribe(emitter, (_) => {}, (_) => {}, () => { async.done(); }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-24 18:48:43 -07:00
										 |  |  |          ObservableWrapper.callComplete(emitter); | 
					
						
							| 
									
										
										
										
											2015-05-26 17:00:31 -07:00
										 |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |     it('should subscribe to the wrapper synchronously', () => { | 
					
						
							| 
									
										
										
										
											2015-05-26 17:00:31 -07:00
										 |  |  |       var called = false; | 
					
						
							|  |  |  |       ObservableWrapper.subscribe(emitter, (value) => { called = true; }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-15 23:58:59 -08:00
										 |  |  |       ObservableWrapper.callEmit(emitter, 99); | 
					
						
							| 
									
										
										
										
											2016-05-26 09:34:04 -07:00
										 |  |  |       expect(called).toBe(true); | 
					
						
							| 
									
										
										
										
											2015-05-26 17:00:31 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-26 15:54:25 +01:00
										 |  |  |     // Makes Edge to disconnect when running the full unit test campaign
 | 
					
						
							|  |  |  |     // TODO: remove when issue is solved: https://github.com/angular/angular/issues/4756
 | 
					
						
							|  |  |  |     if (!browserDetection.isEdge) { | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       it('delivers next and error events synchronously', | 
					
						
							|  |  |  |          inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2016-06-08 15:45:15 -07:00
										 |  |  |            let log: any[] /** TODO #9100 */ = []; | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |            ObservableWrapper.subscribe( | 
					
						
							|  |  |  |                emitter, | 
					
						
							|  |  |  |                (x) => { | 
					
						
							|  |  |  |                  log.push(x); | 
					
						
							|  |  |  |                  expect(log).toEqual([1, 2]); | 
					
						
							|  |  |  |                }, | 
					
						
							|  |  |  |                (err) => { | 
					
						
							|  |  |  |                  log.push(err); | 
					
						
							|  |  |  |                  expect(log).toEqual([1, 2, 3, 4]); | 
					
						
							|  |  |  |                  async.done(); | 
					
						
							|  |  |  |                }); | 
					
						
							| 
									
										
										
										
											2015-11-26 15:54:25 +01:00
										 |  |  |            log.push(1); | 
					
						
							|  |  |  |            ObservableWrapper.callEmit(emitter, 2); | 
					
						
							|  |  |  |            log.push(3); | 
					
						
							|  |  |  |            ObservableWrapper.callError(emitter, 4); | 
					
						
							|  |  |  |            log.push(5); | 
					
						
							|  |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |       it('delivers next and complete events synchronously', () => { | 
					
						
							|  |  |  |         let log: any[] /** TODO #9100 */ = []; | 
					
						
							|  |  |  |         ObservableWrapper.subscribe( | 
					
						
							|  |  |  |             emitter, | 
					
						
							|  |  |  |             (x) => { | 
					
						
							|  |  |  |               log.push(x); | 
					
						
							|  |  |  |               expect(log).toEqual([1, 2]); | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |             null, | 
					
						
							|  |  |  |             () => { | 
					
						
							|  |  |  |               log.push(4); | 
					
						
							|  |  |  |               expect(log).toEqual([1, 2, 3, 4]); | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |         log.push(1); | 
					
						
							|  |  |  |         ObservableWrapper.callEmit(emitter, 2); | 
					
						
							|  |  |  |         log.push(3); | 
					
						
							|  |  |  |         ObservableWrapper.callComplete(emitter); | 
					
						
							|  |  |  |         log.push(5); | 
					
						
							|  |  |  |         expect(log).toEqual([1, 2, 3, 4, 5]); | 
					
						
							| 
									
										
										
										
											2016-05-26 09:34:04 -07:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2015-11-26 15:54:25 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-10-19 14:41:15 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |     it('delivers events asynchronously when forced to async mode', | 
					
						
							|  |  |  |        inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							|  |  |  |          var e = new EventEmitter(true); | 
					
						
							|  |  |  |          var log: any[] /** TODO #9100 */ = []; | 
					
						
							|  |  |  |          ObservableWrapper.subscribe(e, (x) => { | 
					
						
							|  |  |  |            log.push(x); | 
					
						
							|  |  |  |            expect(log).toEqual([1, 3, 2]); | 
					
						
							|  |  |  |            async.done(); | 
					
						
							|  |  |  |          }); | 
					
						
							|  |  |  |          log.push(1); | 
					
						
							|  |  |  |          ObservableWrapper.callEmit(e, 2); | 
					
						
							|  |  |  |          log.push(3); | 
					
						
							| 
									
										
										
										
											2016-05-26 09:34:04 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |        })); | 
					
						
							| 
									
										
										
										
											2015-10-19 14:41:15 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it('reports whether it has subscribers', () => { | 
					
						
							|  |  |  |       var e = new EventEmitter(false); | 
					
						
							|  |  |  |       expect(ObservableWrapper.hasSubscribers(e)).toBe(false); | 
					
						
							|  |  |  |       ObservableWrapper.subscribe(e, (_) => {}); | 
					
						
							|  |  |  |       expect(ObservableWrapper.hasSubscribers(e)).toBe(true); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-26 17:00:31 -07:00
										 |  |  |     // TODO: vsavkin: add tests cases
 | 
					
						
							|  |  |  |     // should call dispose on the subscription if generator returns {done:true}
 | 
					
						
							|  |  |  |     // should call dispose on the subscription on throw
 | 
					
						
							|  |  |  |     // should call dispose on the subscription on return
 | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2015-05-29 17:09:59 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |   describe('ObservableWrapper', () => { | 
					
						
							| 
									
										
										
										
											2015-10-28 21:49:22 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it('should correctly check isObservable for EventEmitter', () => { | 
					
						
							|  |  |  |       var e = new EventEmitter(false); | 
					
						
							|  |  |  |       expect(ObservableWrapper.isObservable(e)).toBe(true); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should correctly check isObservable for Subject', () => { | 
					
						
							|  |  |  |       var e = new Subject(); | 
					
						
							|  |  |  |       expect(ObservableWrapper.isObservable(e)).toBe(true); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-29 19:57:28 -07:00
										 |  |  |     it('should subscribe to EventEmitters', () => { | 
					
						
							|  |  |  |       let e = new EventEmitter(false); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       ObservableWrapper.subscribe(e, (val) => {}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       ObservableWrapper.callEmit(e, 1); | 
					
						
							|  |  |  |       ObservableWrapper.callComplete(e); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-28 21:49:22 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-29 17:09:59 -07:00
										 |  |  |   // See ECMAScript 6 Spec 25.4.4.1
 | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |   describe('PromiseWrapper', () => { | 
					
						
							|  |  |  |     describe('#all', () => { | 
					
						
							|  |  |  |       it('should combine lists of Promises', | 
					
						
							|  |  |  |          inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2015-05-29 17:09:59 -07:00
										 |  |  |            var one = PromiseWrapper.completer(); | 
					
						
							|  |  |  |            var two = PromiseWrapper.completer(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            var all = PromiseWrapper.all([one.promise, two.promise]); | 
					
						
							|  |  |  |            var allCalled = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            PromiseWrapper.then(one.promise, (_) => { | 
					
						
							|  |  |  |              expect(allCalled).toBe(false); | 
					
						
							|  |  |  |              two.resolve('two'); | 
					
						
							| 
									
										
										
										
											2015-06-23 22:02:20 -07:00
										 |  |  |              return null; | 
					
						
							| 
									
										
										
										
											2015-05-29 17:09:59 -07:00
										 |  |  |            }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            PromiseWrapper.then(all, (_) => { | 
					
						
							|  |  |  |              allCalled = true; | 
					
						
							|  |  |  |              async.done(); | 
					
						
							| 
									
										
										
										
											2015-06-23 22:02:20 -07:00
										 |  |  |              return null; | 
					
						
							| 
									
										
										
										
											2015-05-29 17:09:59 -07:00
										 |  |  |            }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            one.resolve('one'); | 
					
						
							|  |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-07 09:09:43 -07:00
										 |  |  |       [null, true, false, 10, 'thing', {}, []].forEach(abruptCompletion => { | 
					
						
							| 
									
										
										
										
											2015-05-29 17:09:59 -07:00
										 |  |  |         it(`should treat "${abruptCompletion}" as an "abrupt completion"`, | 
					
						
							| 
									
										
										
										
											2016-06-09 11:04:15 -07:00
										 |  |  |            inject([AsyncTestCompleter], (async: AsyncTestCompleter) => { | 
					
						
							| 
									
										
										
										
											2015-05-29 17:09:59 -07:00
										 |  |  |              var one = PromiseWrapper.completer(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |              var all = PromiseWrapper.all([one.promise, abruptCompletion]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |              PromiseWrapper.then(all, (val) => { | 
					
						
							|  |  |  |                expect(val[1]).toEqual(abruptCompletion); | 
					
						
							|  |  |  |                async.done(); | 
					
						
							|  |  |  |              }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |              one.resolve('one'); | 
					
						
							|  |  |  |            })); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } |