| 
									
										
										
										
											2016-10-19 21:41:04 +01:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @license | 
					
						
							| 
									
										
										
										
											2020-05-19 12:08:49 -07:00
										 |  |  |  * Copyright Google LLC All Rights Reserved. | 
					
						
							| 
									
										
										
										
											2016-10-19 21:41:04 +01:00
										 |  |  |  * | 
					
						
							|  |  |  |  * 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
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  | import {destroyPlatform, NgModule, Testability} from '@angular/core'; | 
					
						
							| 
									
										
										
										
											2016-11-16 01:04:56 -08:00
										 |  |  | import {NgZone} from '@angular/core/src/zone/ng_zone'; | 
					
						
							| 
									
										
										
										
											2018-03-14 00:17:09 -07:00
										 |  |  | import {fakeAsync, flush, tick} from '@angular/core/testing'; | 
					
						
							| 
									
										
										
										
											2016-10-19 21:41:04 +01:00
										 |  |  | import {BrowserModule} from '@angular/platform-browser'; | 
					
						
							|  |  |  | import {platformBrowserDynamic} from '@angular/platform-browser-dynamic'; | 
					
						
							| 
									
										
										
										
											2016-10-20 19:35:35 -07:00
										 |  |  | import {UpgradeModule} from '@angular/upgrade/static'; | 
					
						
							| 
									
										
										
										
											2016-10-19 21:41:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  | import * as angular from '../../../src/common/src/angular1'; | 
					
						
							| 
									
										
										
										
											2019-03-22 09:42:52 +00:00
										 |  |  | import {html, withEachNg1Version} from '../../../src/common/test/helpers/common_test_helpers'; | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-22 09:42:52 +00:00
										 |  |  | import {bootstrap} from './static_test_helpers'; | 
					
						
							| 
									
										
										
										
											2016-10-19 21:41:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-15 19:21:18 +02:00
										 |  |  | withEachNg1Version(() => { | 
					
						
							| 
									
										
										
										
											2016-10-19 21:41:04 +01:00
										 |  |  |   describe('testability', () => { | 
					
						
							|  |  |  |     beforeEach(() => destroyPlatform()); | 
					
						
							|  |  |  |     afterEach(() => destroyPlatform()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     @NgModule({imports: [BrowserModule, UpgradeModule]}) | 
					
						
							|  |  |  |     class Ng2Module { | 
					
						
							|  |  |  |       ngDoBootstrap() {} | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should handle deferred bootstrap', fakeAsync(() => { | 
					
						
							|  |  |  |          let applicationRunning = false; | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |          let stayedInTheZone: boolean = undefined!; | 
					
						
							| 
									
										
										
										
											2019-04-23 18:00:04 +03:00
										 |  |  |          const ng1Module = angular.module_('ng1', []).run(() => { | 
					
						
							| 
									
										
										
										
											2016-11-16 01:04:56 -08:00
										 |  |  |            applicationRunning = true; | 
					
						
							|  |  |  |            stayedInTheZone = NgZone.isInAngularZone(); | 
					
						
							|  |  |  |          }); | 
					
						
							| 
									
										
										
										
											2016-10-19 21:41:04 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |          const element = html('<div></div>'); | 
					
						
							|  |  |  |          window.name = 'NG_DEFER_BOOTSTRAP!' + window.name; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          bootstrap(platformBrowserDynamic(), Ng2Module, element, ng1Module); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |          setTimeout(() => { | 
					
						
							|  |  |  |            (<any>window).angular.resumeBootstrap(); | 
					
						
							|  |  |  |          }, 100); | 
					
						
							| 
									
										
										
										
											2016-10-19 21:41:04 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |          expect(applicationRunning).toEqual(false); | 
					
						
							|  |  |  |          tick(100); | 
					
						
							|  |  |  |          expect(applicationRunning).toEqual(true); | 
					
						
							| 
									
										
										
										
											2016-11-16 01:04:56 -08:00
										 |  |  |          expect(stayedInTheZone).toEqual(true); | 
					
						
							| 
									
										
										
										
											2016-10-19 21:41:04 +01:00
										 |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-14 00:17:09 -07:00
										 |  |  |     it('should propagate return value of resumeBootstrap', fakeAsync(() => { | 
					
						
							| 
									
										
										
										
											2019-04-23 18:00:04 +03:00
										 |  |  |          const ng1Module = angular.module_('ng1', []); | 
					
						
							| 
									
										
										
										
											2018-03-14 00:17:09 -07:00
										 |  |  |          let a1Injector: angular.IInjectorService|undefined; | 
					
						
							|  |  |  |          ng1Module.run([ | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |            '$injector', | 
					
						
							|  |  |  |            function($injector: angular.IInjectorService) { | 
					
						
							|  |  |  |              a1Injector = $injector; | 
					
						
							|  |  |  |            } | 
					
						
							| 
									
										
										
										
											2018-03-14 00:17:09 -07:00
										 |  |  |          ]); | 
					
						
							|  |  |  |          const element = html('<div></div>'); | 
					
						
							|  |  |  |          window.name = 'NG_DEFER_BOOTSTRAP!' + window.name; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          bootstrap(platformBrowserDynamic(), Ng2Module, element, ng1Module); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          tick(100); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          const value = (<any>window).angular.resumeBootstrap(); | 
					
						
							|  |  |  |          expect(value).toBe(a1Injector); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          flush(); | 
					
						
							|  |  |  |        })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-19 21:41:04 +01:00
										 |  |  |     it('should wait for ng2 testability', fakeAsync(() => { | 
					
						
							| 
									
										
										
										
											2019-04-23 18:00:04 +03:00
										 |  |  |          const ng1Module = angular.module_('ng1', []); | 
					
						
							| 
									
										
										
										
											2016-10-19 21:41:04 +01:00
										 |  |  |          const element = html('<div></div>'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          bootstrap(platformBrowserDynamic(), Ng2Module, element, ng1Module).then((upgrade) => { | 
					
						
							|  |  |  |            const ng2Testability: Testability = upgrade.injector.get(Testability); | 
					
						
							|  |  |  |            ng2Testability.increasePendingRequestCount(); | 
					
						
							|  |  |  |            let ng2Stable = false; | 
					
						
							|  |  |  |            let ng1Stable = false; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |            angular.getTestability(element).whenStable(() => { | 
					
						
							|  |  |  |              ng1Stable = true; | 
					
						
							|  |  |  |            }); | 
					
						
							| 
									
										
										
										
											2016-10-19 21:41:04 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |            setTimeout(() => { | 
					
						
							|  |  |  |              ng2Stable = true; | 
					
						
							|  |  |  |              ng2Testability.decreasePendingRequestCount(); | 
					
						
							|  |  |  |            }, 100); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            expect(ng1Stable).toEqual(false); | 
					
						
							|  |  |  |            expect(ng2Stable).toEqual(false); | 
					
						
							|  |  |  |            tick(100); | 
					
						
							|  |  |  |            expect(ng1Stable).toEqual(true); | 
					
						
							|  |  |  |            expect(ng2Stable).toEqual(true); | 
					
						
							|  |  |  |          }); | 
					
						
							|  |  |  |        })); | 
					
						
							| 
									
										
										
										
											2017-05-16 18:03:25 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     it('should not wait for $interval', fakeAsync(() => { | 
					
						
							| 
									
										
										
										
											2019-04-23 18:00:04 +03:00
										 |  |  |          const ng1Module = angular.module_('ng1', []); | 
					
						
							| 
									
										
										
										
											2017-05-16 18:03:25 -07:00
										 |  |  |          const element = html('<div></div>'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |          bootstrap(platformBrowserDynamic(), Ng2Module, element, ng1Module).then((upgrade) => { | 
					
						
							|  |  |  |            const ng2Testability: Testability = upgrade.injector.get(Testability); | 
					
						
							|  |  |  |            const $interval: angular.IIntervalService = upgrade.$injector.get('$interval'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            let ng2Stable = false; | 
					
						
							|  |  |  |            let intervalDone = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            const id = $interval((arg: string) => { | 
					
						
							|  |  |  |              // should only be called once
 | 
					
						
							|  |  |  |              expect(intervalDone).toEqual(false); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |              intervalDone = true; | 
					
						
							|  |  |  |              expect(NgZone.isInAngularZone()).toEqual(true); | 
					
						
							|  |  |  |              expect(arg).toEqual('passed argument'); | 
					
						
							|  |  |  |            }, 200, 0, true, 'passed argument'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-13 16:40:21 -07:00
										 |  |  |            ng2Testability.whenStable(() => { | 
					
						
							|  |  |  |              ng2Stable = true; | 
					
						
							|  |  |  |            }); | 
					
						
							| 
									
										
										
										
											2017-05-16 18:03:25 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |            tick(100); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            expect(intervalDone).toEqual(false); | 
					
						
							|  |  |  |            expect(ng2Stable).toEqual(true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            tick(200); | 
					
						
							|  |  |  |            expect(intervalDone).toEqual(true); | 
					
						
							|  |  |  |            expect($interval.cancel(id)).toEqual(true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            // Interval should not fire after cancel
 | 
					
						
							|  |  |  |            tick(200); | 
					
						
							|  |  |  |          }); | 
					
						
							|  |  |  |        })); | 
					
						
							| 
									
										
										
										
											2016-10-19 21:41:04 +01:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2018-02-15 19:21:18 +02:00
										 |  |  | }); |