| 
									
										
										
										
											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
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-01 10:28:52 -08:00
										 |  |  | import {EventEmitter} from '@angular/core'; | 
					
						
							| 
									
										
										
										
											2016-04-28 17:50:03 -07:00
										 |  |  | import {Injectable} from '@angular/core/src/di'; | 
					
						
							| 
									
										
										
										
											2017-04-27 11:44:14 -07:00
										 |  |  | import {PendingMacrotask, Testability, TestabilityRegistry} from '@angular/core/src/testability/testability'; | 
					
						
							| 
									
										
										
										
											2016-04-28 17:50:03 -07:00
										 |  |  | import {NgZone} from '@angular/core/src/zone/ng_zone'; | 
					
						
							| 
									
										
										
										
											2017-04-27 11:44:14 -07:00
										 |  |  | import {async, fakeAsync, flush, tick} from '@angular/core/testing'; | 
					
						
							|  |  |  | import {SpyObject, beforeEach, describe, expect, it} from '@angular/core/testing/src/testing_internal'; | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-01 15:18:10 -08:00
										 |  |  | import {scheduleMicroTask} from '../../src/util'; | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-17 15:28:21 -08:00
										 |  |  | // Schedules a microtasks (using a resolved promise .then())
 | 
					
						
							|  |  |  | function microTask(fn: Function): void { | 
					
						
							|  |  |  |   scheduleMicroTask(() => { | 
					
						
							|  |  |  |     // We do double dispatch so that we  can wait for scheduleMicrotask in the Testability when
 | 
					
						
							|  |  |  |     // NgZone becomes stable.
 | 
					
						
							|  |  |  |     scheduleMicroTask(fn); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2015-07-24 12:46:12 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-03 09:06:42 -08:00
										 |  |  | @Injectable() | 
					
						
							| 
									
										
										
										
											2015-10-08 13:33:22 -07:00
										 |  |  | class MockNgZone extends NgZone { | 
					
						
							| 
									
										
										
										
											2016-07-21 17:12:00 -07:00
										 |  |  |   /** @internal */ | 
					
						
							| 
									
										
										
										
											2017-07-01 10:29:56 -07:00
										 |  |  |   onUnstable: EventEmitter<any>; | 
					
						
							| 
									
										
										
										
											2015-07-24 12:46:12 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-21 17:12:00 -07:00
										 |  |  |   /** @internal */ | 
					
						
							| 
									
										
										
										
											2017-07-01 10:29:56 -07:00
										 |  |  |   onStable: EventEmitter<any>; | 
					
						
							| 
									
										
										
										
											2015-07-24 12:46:12 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-11-03 18:45:55 -08:00
										 |  |  |   constructor() { | 
					
						
							|  |  |  |     super({enableLongStackTrace: false}); | 
					
						
							| 
									
										
										
										
											2017-07-01 10:29:56 -07:00
										 |  |  |     this.onUnstable = new EventEmitter(false); | 
					
						
							|  |  |  |     this.onStable = new EventEmitter(false); | 
					
						
							| 
									
										
										
										
											2015-07-24 12:46:12 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-01 10:29:56 -07:00
										 |  |  |   unstable(): void { this.onUnstable.emit(null); } | 
					
						
							| 
									
										
										
										
											2015-11-03 18:45:55 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-01 10:29:56 -07:00
										 |  |  |   stable(): void { this.onStable.emit(null); } | 
					
						
							| 
									
										
										
										
											2015-07-24 12:46:12 -07:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-03-23 16:46:18 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-15 16:28:41 -08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2015-03-23 16:46:18 -07:00
										 |  |  |   describe('Testability', () => { | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |     let testability: Testability; | 
					
						
							|  |  |  |     let execute: any; | 
					
						
							|  |  |  |     let execute2: any; | 
					
						
							| 
									
										
										
										
											2017-04-27 11:44:14 -07:00
										 |  |  |     let updateCallback: any; | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |     let ngZone: MockNgZone; | 
					
						
							| 
									
										
										
										
											2015-03-23 16:46:18 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-27 11:44:14 -07:00
										 |  |  |     beforeEach(async(() => { | 
					
						
							| 
									
										
										
										
											2015-07-24 12:46:12 -07:00
										 |  |  |       ngZone = new MockNgZone(); | 
					
						
							|  |  |  |       testability = new Testability(ngZone); | 
					
						
							|  |  |  |       execute = new SpyObject().spy('execute'); | 
					
						
							| 
									
										
										
										
											2016-01-05 12:56:24 -08:00
										 |  |  |       execute2 = new SpyObject().spy('execute'); | 
					
						
							| 
									
										
										
										
											2017-04-27 11:44:14 -07:00
										 |  |  |       updateCallback = new SpyObject().spy('execute'); | 
					
						
							|  |  |  |     })); | 
					
						
							| 
									
										
										
										
											2015-03-23 16:46:18 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-24 12:46:12 -07:00
										 |  |  |     describe('Pending count logic', () => { | 
					
						
							|  |  |  |       it('should start with a pending count of 0', | 
					
						
							|  |  |  |          () => { expect(testability.getPendingRequestCount()).toEqual(0); }); | 
					
						
							| 
									
										
										
										
											2015-03-23 16:46:18 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-27 11:44:14 -07:00
										 |  |  |       it('should fire whenstable callbacks if pending count is 0', async(() => { | 
					
						
							| 
									
										
										
										
											2015-07-24 12:46:12 -07:00
										 |  |  |            testability.whenStable(execute); | 
					
						
							| 
									
										
										
										
											2017-04-27 11:44:14 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |            microTask(() => { expect(execute).toHaveBeenCalled(); }); | 
					
						
							| 
									
										
										
										
											2015-07-24 12:46:12 -07:00
										 |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should not fire whenstable callbacks synchronously if pending count is 0', () => { | 
					
						
							|  |  |  |         testability.whenStable(execute); | 
					
						
							|  |  |  |         expect(execute).not.toHaveBeenCalled(); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-27 11:44:14 -07:00
										 |  |  |       it('should not call whenstable callbacks when there are pending counts', async(() => { | 
					
						
							| 
									
										
										
										
											2015-07-24 12:46:12 -07:00
										 |  |  |            testability.increasePendingRequestCount(); | 
					
						
							|  |  |  |            testability.increasePendingRequestCount(); | 
					
						
							|  |  |  |            testability.whenStable(execute); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-17 15:28:21 -08:00
										 |  |  |            microTask(() => { | 
					
						
							| 
									
										
										
										
											2015-07-24 12:46:12 -07:00
										 |  |  |              expect(execute).not.toHaveBeenCalled(); | 
					
						
							|  |  |  |              testability.decreasePendingRequestCount(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-27 11:44:14 -07:00
										 |  |  |              microTask(() => { expect(execute).not.toHaveBeenCalled(); }); | 
					
						
							| 
									
										
										
										
											2015-07-24 12:46:12 -07:00
										 |  |  |            }); | 
					
						
							|  |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-27 11:44:14 -07:00
										 |  |  |       it('should fire whenstable callbacks when pending drops to 0', async(() => { | 
					
						
							| 
									
										
										
										
											2015-07-24 12:46:12 -07:00
										 |  |  |            testability.increasePendingRequestCount(); | 
					
						
							|  |  |  |            testability.whenStable(execute); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-17 15:28:21 -08:00
										 |  |  |            microTask(() => { | 
					
						
							| 
									
										
										
										
											2015-07-24 12:46:12 -07:00
										 |  |  |              expect(execute).not.toHaveBeenCalled(); | 
					
						
							|  |  |  |              testability.decreasePendingRequestCount(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-27 11:44:14 -07:00
										 |  |  |              microTask(() => { expect(execute).toHaveBeenCalled(); }); | 
					
						
							| 
									
										
										
										
											2015-07-24 12:46:12 -07:00
										 |  |  |            }); | 
					
						
							|  |  |  |          })); | 
					
						
							| 
									
										
										
										
											2015-03-23 16:46:18 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-27 11:44:14 -07:00
										 |  |  |       it('should not fire whenstable callbacks synchronously when pending drops to 0', async(() => { | 
					
						
							|  |  |  |            testability.increasePendingRequestCount(); | 
					
						
							|  |  |  |            testability.whenStable(execute); | 
					
						
							|  |  |  |            testability.decreasePendingRequestCount(); | 
					
						
							| 
									
										
										
										
											2015-03-23 16:46:18 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-27 11:44:14 -07:00
										 |  |  |            expect(execute).not.toHaveBeenCalled(); | 
					
						
							|  |  |  |          })); | 
					
						
							| 
									
										
										
										
											2016-01-05 12:56:24 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-27 11:44:14 -07:00
										 |  |  |       it('should fire whenstable callbacks with didWork if pending count is 0', async(() => { | 
					
						
							| 
									
										
										
										
											2018-01-17 15:28:21 -08:00
										 |  |  |            microTask(() => { | 
					
						
							| 
									
										
										
										
											2017-04-27 11:44:14 -07:00
										 |  |  |              testability.whenStable(execute); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |              microTask(() => { expect(execute).toHaveBeenCalledWith(false); }); | 
					
						
							| 
									
										
										
										
											2016-01-05 12:56:24 -08:00
										 |  |  |            }); | 
					
						
							|  |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-27 11:44:14 -07:00
										 |  |  |       it('should fire whenstable callbacks with didWork when pending drops to 0', async(() => { | 
					
						
							| 
									
										
										
										
											2016-01-05 12:56:24 -08:00
										 |  |  |            testability.increasePendingRequestCount(); | 
					
						
							|  |  |  |            testability.whenStable(execute); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-27 11:44:14 -07:00
										 |  |  |            testability.decreasePendingRequestCount(); | 
					
						
							| 
									
										
										
										
											2016-01-05 12:56:24 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-27 11:44:14 -07:00
										 |  |  |            microTask(() => { | 
					
						
							|  |  |  |              expect(execute).toHaveBeenCalledWith(true); | 
					
						
							|  |  |  |              testability.whenStable(execute2); | 
					
						
							| 
									
										
										
										
											2016-01-05 12:56:24 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-27 11:44:14 -07:00
										 |  |  |              microTask(() => { expect(execute2).toHaveBeenCalledWith(false); }); | 
					
						
							| 
									
										
										
										
											2016-01-05 12:56:24 -08:00
										 |  |  |            }); | 
					
						
							|  |  |  |          })); | 
					
						
							| 
									
										
										
										
											2017-04-27 11:44:14 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-23 16:46:18 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-24 12:46:12 -07:00
										 |  |  |     describe('NgZone callback logic', () => { | 
					
						
							| 
									
										
										
										
											2017-04-27 11:44:14 -07:00
										 |  |  |       describe('whenStable with timeout', () => { | 
					
						
							|  |  |  |         it('should list pending tasks when the timeout is hit', fakeAsync(() => { | 
					
						
							|  |  |  |              const id = ngZone.run(() => setTimeout(() => {}, 1000)); | 
					
						
							|  |  |  |              testability.whenStable(execute, 200); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |              expect(execute).not.toHaveBeenCalled(); | 
					
						
							|  |  |  |              tick(200); | 
					
						
							|  |  |  |              expect(execute).toHaveBeenCalled(); | 
					
						
							|  |  |  |              const tasks = execute.calls.mostRecent().args[1] as PendingMacrotask[]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |              expect(tasks.length).toEqual(1); | 
					
						
							|  |  |  |              expect(tasks[0].delay).toEqual(1000); | 
					
						
							|  |  |  |              expect(tasks[0].source).toEqual('setTimeout'); | 
					
						
							|  |  |  |              expect(tasks[0].isPeriodic).toEqual(false); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |              clearTimeout(id); | 
					
						
							|  |  |  |            })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         it('should fire if Angular is already stable', async(() => { | 
					
						
							|  |  |  |              testability.whenStable(execute, 200); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |              microTask(() => { expect(execute).toHaveBeenCalled(); }); | 
					
						
							|  |  |  |            })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         it('should fire when macroTasks are cancelled', fakeAsync(() => { | 
					
						
							|  |  |  |              const id = ngZone.run(() => setTimeout(() => {}, 1000)); | 
					
						
							|  |  |  |              testability.whenStable(execute, 500); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |              tick(200); | 
					
						
							|  |  |  |              ngZone.run(() => clearTimeout(id)); | 
					
						
							|  |  |  |              // fakeAsync doesn't trigger NgZones whenStable
 | 
					
						
							|  |  |  |              ngZone.stable(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |              tick(1); | 
					
						
							|  |  |  |              expect(execute).toHaveBeenCalled(); | 
					
						
							|  |  |  |            })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         it('calls the done callback when angular is stable', fakeAsync(() => { | 
					
						
							|  |  |  |              let timeout1Done = false; | 
					
						
							|  |  |  |              ngZone.run(() => setTimeout(() => timeout1Done = true, 500)); | 
					
						
							|  |  |  |              testability.whenStable(execute, 1000); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |              tick(600); | 
					
						
							|  |  |  |              ngZone.stable(); | 
					
						
							|  |  |  |              tick(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |              expect(timeout1Done).toEqual(true); | 
					
						
							|  |  |  |              expect(execute).toHaveBeenCalled(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |              // Should cancel the done timeout.
 | 
					
						
							|  |  |  |              tick(500); | 
					
						
							|  |  |  |              ngZone.stable(); | 
					
						
							|  |  |  |              tick(); | 
					
						
							|  |  |  |              expect(execute.calls.count()).toEqual(1); | 
					
						
							|  |  |  |            })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         it('calls update when macro tasks change', fakeAsync(() => { | 
					
						
							|  |  |  |              let timeout1Done = false; | 
					
						
							|  |  |  |              let timeout2Done = false; | 
					
						
							|  |  |  |              ngZone.run(() => setTimeout(() => timeout1Done = true, 500)); | 
					
						
							|  |  |  |              tick(); | 
					
						
							|  |  |  |              testability.whenStable(execute, 1000, updateCallback); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |              tick(100); | 
					
						
							|  |  |  |              ngZone.run(() => setTimeout(() => timeout2Done = true, 300)); | 
					
						
							|  |  |  |              expect(updateCallback.calls.count()).toEqual(1); | 
					
						
							|  |  |  |              tick(600); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |              expect(timeout1Done).toEqual(true); | 
					
						
							|  |  |  |              expect(timeout2Done).toEqual(true); | 
					
						
							|  |  |  |              expect(updateCallback.calls.count()).toEqual(3); | 
					
						
							|  |  |  |              expect(execute).toHaveBeenCalled(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |              const update1 = updateCallback.calls.all()[0].args[0] as PendingMacrotask[]; | 
					
						
							|  |  |  |              expect(update1[0].delay).toEqual(500); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |              const update2 = updateCallback.calls.all()[1].args[0] as PendingMacrotask[]; | 
					
						
							|  |  |  |              expect(update2[0].delay).toEqual(500); | 
					
						
							|  |  |  |              expect(update2[1].delay).toEqual(300); | 
					
						
							|  |  |  |            })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         it('cancels the done callback if the update callback returns true', fakeAsync(() => { | 
					
						
							|  |  |  |              let timeoutDone = false; | 
					
						
							|  |  |  |              ngZone.unstable(); | 
					
						
							|  |  |  |              execute2.and.returnValue(true); | 
					
						
							|  |  |  |              testability.whenStable(execute, 1000, execute2); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |              tick(100); | 
					
						
							|  |  |  |              ngZone.run(() => setTimeout(() => timeoutDone = true, 500)); | 
					
						
							|  |  |  |              ngZone.stable(); | 
					
						
							|  |  |  |              expect(execute2).toHaveBeenCalled(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |              tick(500); | 
					
						
							|  |  |  |              ngZone.stable(); | 
					
						
							|  |  |  |              tick(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |              expect(execute).not.toHaveBeenCalled(); | 
					
						
							|  |  |  |            })); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should fire whenstable callback if event is already finished', fakeAsync(() => { | 
					
						
							| 
									
										
										
										
											2016-02-25 14:24:17 -08:00
										 |  |  |            ngZone.unstable(); | 
					
						
							|  |  |  |            ngZone.stable(); | 
					
						
							| 
									
										
										
										
											2015-07-24 12:46:12 -07:00
										 |  |  |            testability.whenStable(execute); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-27 11:44:14 -07:00
										 |  |  |            tick(); | 
					
						
							|  |  |  |            expect(execute).toHaveBeenCalled(); | 
					
						
							| 
									
										
										
										
											2015-07-24 12:46:12 -07:00
										 |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should not fire whenstable callbacks synchronously if event is already finished', () => { | 
					
						
							| 
									
										
										
										
											2016-02-25 14:24:17 -08:00
										 |  |  |         ngZone.unstable(); | 
					
						
							|  |  |  |         ngZone.stable(); | 
					
						
							| 
									
										
										
										
											2015-07-24 12:46:12 -07:00
										 |  |  |         testability.whenStable(execute); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         expect(execute).not.toHaveBeenCalled(); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-27 11:44:14 -07:00
										 |  |  |       it('should fire whenstable callback when event finishes', fakeAsync(() => { | 
					
						
							| 
									
										
										
										
											2016-02-25 14:24:17 -08:00
										 |  |  |            ngZone.unstable(); | 
					
						
							| 
									
										
										
										
											2015-07-24 12:46:12 -07:00
										 |  |  |            testability.whenStable(execute); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-27 11:44:14 -07:00
										 |  |  |            tick(); | 
					
						
							|  |  |  |            expect(execute).not.toHaveBeenCalled(); | 
					
						
							|  |  |  |            ngZone.stable(); | 
					
						
							| 
									
										
										
										
											2015-07-24 12:46:12 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-27 11:44:14 -07:00
										 |  |  |            tick(); | 
					
						
							|  |  |  |            expect(execute).toHaveBeenCalled(); | 
					
						
							| 
									
										
										
										
											2015-07-24 12:46:12 -07:00
										 |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should not fire whenstable callbacks synchronously when event finishes', () => { | 
					
						
							| 
									
										
										
										
											2016-02-25 14:24:17 -08:00
										 |  |  |         ngZone.unstable(); | 
					
						
							| 
									
										
										
										
											2015-07-24 12:46:12 -07:00
										 |  |  |         testability.whenStable(execute); | 
					
						
							| 
									
										
										
										
											2016-02-25 14:24:17 -08:00
										 |  |  |         ngZone.stable(); | 
					
						
							| 
									
										
										
										
											2015-07-24 12:46:12 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |         expect(execute).not.toHaveBeenCalled(); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-27 11:44:14 -07:00
										 |  |  |       it('should not fire whenstable callback when event did not finish', fakeAsync(() => { | 
					
						
							| 
									
										
										
										
											2016-02-25 14:24:17 -08:00
										 |  |  |            ngZone.unstable(); | 
					
						
							| 
									
										
										
										
											2015-07-24 12:46:12 -07:00
										 |  |  |            testability.increasePendingRequestCount(); | 
					
						
							|  |  |  |            testability.whenStable(execute); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-27 11:44:14 -07:00
										 |  |  |            tick(); | 
					
						
							|  |  |  |            expect(execute).not.toHaveBeenCalled(); | 
					
						
							|  |  |  |            testability.decreasePendingRequestCount(); | 
					
						
							| 
									
										
										
										
											2015-07-24 12:46:12 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-27 11:44:14 -07:00
										 |  |  |            tick(); | 
					
						
							|  |  |  |            expect(execute).not.toHaveBeenCalled(); | 
					
						
							|  |  |  |            ngZone.stable(); | 
					
						
							| 
									
										
										
										
											2015-07-24 12:46:12 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-27 11:44:14 -07:00
										 |  |  |            tick(); | 
					
						
							|  |  |  |            expect(execute).toHaveBeenCalled(); | 
					
						
							| 
									
										
										
										
											2015-07-24 12:46:12 -07:00
										 |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-27 11:44:14 -07:00
										 |  |  |       it('should not fire whenstable callback when there are pending counts', fakeAsync(() => { | 
					
						
							| 
									
										
										
										
											2016-02-25 14:24:17 -08:00
										 |  |  |            ngZone.unstable(); | 
					
						
							| 
									
										
										
										
											2015-07-24 12:46:12 -07:00
										 |  |  |            testability.increasePendingRequestCount(); | 
					
						
							|  |  |  |            testability.increasePendingRequestCount(); | 
					
						
							|  |  |  |            testability.whenStable(execute); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-27 11:44:14 -07:00
										 |  |  |            tick(); | 
					
						
							|  |  |  |            expect(execute).not.toHaveBeenCalled(); | 
					
						
							|  |  |  |            ngZone.stable(); | 
					
						
							| 
									
										
										
										
											2015-07-24 12:46:12 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-27 11:44:14 -07:00
										 |  |  |            tick(); | 
					
						
							|  |  |  |            expect(execute).not.toHaveBeenCalled(); | 
					
						
							|  |  |  |            testability.decreasePendingRequestCount(); | 
					
						
							| 
									
										
										
										
											2015-03-23 16:46:18 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-27 11:44:14 -07:00
										 |  |  |            tick(); | 
					
						
							|  |  |  |            expect(execute).not.toHaveBeenCalled(); | 
					
						
							|  |  |  |            testability.decreasePendingRequestCount(); | 
					
						
							| 
									
										
										
										
											2015-03-23 16:46:18 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-27 11:44:14 -07:00
										 |  |  |            tick(); | 
					
						
							|  |  |  |            expect(execute).toHaveBeenCalled(); | 
					
						
							| 
									
										
										
										
											2015-07-24 12:46:12 -07:00
										 |  |  |          })); | 
					
						
							| 
									
										
										
										
											2016-01-05 12:56:24 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |       it('should fire whenstable callback with didWork if event is already finished', | 
					
						
							| 
									
										
										
										
											2017-04-27 11:44:14 -07:00
										 |  |  |          fakeAsync(() => { | 
					
						
							| 
									
										
										
										
											2016-02-25 14:24:17 -08:00
										 |  |  |            ngZone.unstable(); | 
					
						
							|  |  |  |            ngZone.stable(); | 
					
						
							| 
									
										
										
										
											2016-01-05 12:56:24 -08:00
										 |  |  |            testability.whenStable(execute); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-27 11:44:14 -07:00
										 |  |  |            tick(); | 
					
						
							|  |  |  |            expect(execute).toHaveBeenCalledWith(true); | 
					
						
							|  |  |  |            testability.whenStable(execute2); | 
					
						
							| 
									
										
										
										
											2016-01-05 12:56:24 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-27 11:44:14 -07:00
										 |  |  |            tick(); | 
					
						
							|  |  |  |            expect(execute2).toHaveBeenCalledWith(false); | 
					
						
							| 
									
										
										
										
											2016-01-05 12:56:24 -08:00
										 |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-27 11:44:14 -07:00
										 |  |  |       it('should fire whenstable callback with didwork when event finishes', fakeAsync(() => { | 
					
						
							| 
									
										
										
										
											2016-02-25 14:24:17 -08:00
										 |  |  |            ngZone.unstable(); | 
					
						
							| 
									
										
										
										
											2016-01-05 12:56:24 -08:00
										 |  |  |            testability.whenStable(execute); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-27 11:44:14 -07:00
										 |  |  |            tick(); | 
					
						
							|  |  |  |            ngZone.stable(); | 
					
						
							| 
									
										
										
										
											2016-01-05 12:56:24 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-27 11:44:14 -07:00
										 |  |  |            tick(); | 
					
						
							|  |  |  |            expect(execute).toHaveBeenCalledWith(true); | 
					
						
							|  |  |  |            testability.whenStable(execute2); | 
					
						
							| 
									
										
										
										
											2016-01-05 12:56:24 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-27 11:44:14 -07:00
										 |  |  |            tick(); | 
					
						
							|  |  |  |            expect(execute2).toHaveBeenCalledWith(false); | 
					
						
							| 
									
										
										
										
											2016-01-05 12:56:24 -08:00
										 |  |  |          })); | 
					
						
							| 
									
										
										
										
											2015-03-23 16:46:18 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2017-09-08 11:50:13 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   describe('TestabilityRegistry', () => { | 
					
						
							|  |  |  |     let testability1: Testability; | 
					
						
							|  |  |  |     let testability2: Testability; | 
					
						
							| 
									
										
										
										
											2017-04-27 11:44:14 -07:00
										 |  |  |     let registry: TestabilityRegistry; | 
					
						
							| 
									
										
										
										
											2017-09-08 11:50:13 -07:00
										 |  |  |     let ngZone: MockNgZone; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-27 11:44:14 -07:00
										 |  |  |     beforeEach(async(() => { | 
					
						
							| 
									
										
										
										
											2017-09-08 11:50:13 -07:00
										 |  |  |       ngZone = new MockNgZone(); | 
					
						
							|  |  |  |       testability1 = new Testability(ngZone); | 
					
						
							|  |  |  |       testability2 = new Testability(ngZone); | 
					
						
							| 
									
										
										
										
											2017-04-27 11:44:14 -07:00
										 |  |  |       registry = new TestabilityRegistry(); | 
					
						
							|  |  |  |     })); | 
					
						
							| 
									
										
										
										
											2017-09-08 11:50:13 -07:00
										 |  |  |     describe('unregister testability', () => { | 
					
						
							|  |  |  |       it('should remove the testability when unregistering an existing testability', () => { | 
					
						
							| 
									
										
										
										
											2017-04-27 11:44:14 -07:00
										 |  |  |         registry.registerApplication('testability1', testability1); | 
					
						
							|  |  |  |         registry.registerApplication('testability2', testability2); | 
					
						
							|  |  |  |         registry.unregisterApplication('testability2'); | 
					
						
							|  |  |  |         expect(registry.getAllTestabilities().length).toEqual(1); | 
					
						
							|  |  |  |         expect(registry.getTestability('testability1')).toEqual(testability1); | 
					
						
							| 
									
										
										
										
											2017-09-08 11:50:13 -07:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should remain the same when unregistering a non-existing testability', () => { | 
					
						
							| 
									
										
										
										
											2017-04-27 11:44:14 -07:00
										 |  |  |         expect(registry.getAllTestabilities().length).toEqual(0); | 
					
						
							|  |  |  |         registry.registerApplication('testability1', testability1); | 
					
						
							|  |  |  |         registry.registerApplication('testability2', testability2); | 
					
						
							|  |  |  |         registry.unregisterApplication('testability3'); | 
					
						
							|  |  |  |         expect(registry.getAllTestabilities().length).toEqual(2); | 
					
						
							|  |  |  |         expect(registry.getTestability('testability1')).toEqual(testability1); | 
					
						
							|  |  |  |         expect(registry.getTestability('testability2')).toEqual(testability2); | 
					
						
							| 
									
										
										
										
											2017-09-08 11:50:13 -07:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should remove all the testability when unregistering all testabilities', () => { | 
					
						
							| 
									
										
										
										
											2017-04-27 11:44:14 -07:00
										 |  |  |         registry.registerApplication('testability1', testability1); | 
					
						
							|  |  |  |         registry.registerApplication('testability2', testability2); | 
					
						
							|  |  |  |         registry.unregisterAllApplications(); | 
					
						
							|  |  |  |         expect(registry.getAllTestabilities().length).toEqual(0); | 
					
						
							| 
									
										
										
										
											2017-09-08 11:50:13 -07:00
										 |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2015-03-23 16:46:18 -07:00
										 |  |  | } |