| 
									
										
										
										
											2016-07-21 17:12:00 -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-08-17 09:24:44 -07:00
										 |  |  | import {DirectiveResolver, ResourceLoader} from '@angular/compiler'; | 
					
						
							| 
									
										
										
										
											2016-09-27 17:12:25 -07:00
										 |  |  | import {Compiler, Component, Injector, NgModule, NgModuleFactory} from '@angular/core'; | 
					
						
							|  |  |  | import {TestBed, async, fakeAsync, tick} from '@angular/core/testing'; | 
					
						
							|  |  |  | import {beforeEach, describe, inject, it} from '@angular/core/testing/testing_internal'; | 
					
						
							| 
									
										
										
										
											2016-07-22 09:20:51 -07:00
										 |  |  | import {expect} from '@angular/platform-browser/testing/matchers'; | 
					
						
							| 
									
										
										
										
											2016-08-02 15:53:34 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-10 18:21:28 -07:00
										 |  |  | import {stringify} from '../src/facade/lang'; | 
					
						
							| 
									
										
										
										
											2016-08-30 18:07:40 -07:00
										 |  |  | import {MockDirectiveResolver} from '../testing/index'; | 
					
						
							| 
									
										
										
										
											2016-06-24 08:46:43 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-30 18:07:40 -07:00
										 |  |  | import {ViewMetadata} from './private_import_core'; | 
					
						
							| 
									
										
										
										
											2016-08-17 09:24:44 -07:00
										 |  |  | import {SpyResourceLoader} from './spies'; | 
					
						
							| 
									
										
										
										
											2016-06-24 08:46:43 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-18 11:20:02 -07:00
										 |  |  | @Component({selector: 'child-cmp'}) | 
					
						
							| 
									
										
										
										
											2016-06-24 08:46:43 -07:00
										 |  |  | class ChildComp { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @Component({selector: 'some-cmp', template: 'someComp'}) | 
					
						
							|  |  |  | class SomeComp { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @Component({selector: 'some-cmp', templateUrl: './someTpl'}) | 
					
						
							|  |  |  | class SomeCompWithUrlTemplate { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export function main() { | 
					
						
							| 
									
										
										
										
											2016-08-18 11:20:02 -07:00
										 |  |  |   describe('RuntimeCompiler', () => { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     describe('compilerComponentSync', () => { | 
					
						
							|  |  |  |       describe('never resolving loader', () => { | 
					
						
							|  |  |  |         class StubResourceLoader { | 
					
						
							|  |  |  |           get(url: string) { return new Promise(() => {}); } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         beforeEach(() => { | 
					
						
							|  |  |  |           TestBed.configureCompiler( | 
					
						
							|  |  |  |               {providers: [{provide: ResourceLoader, useClass: StubResourceLoader}]}); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         it('should throw when using a templateUrl that has not been compiled before', async(() => { | 
					
						
							|  |  |  |              TestBed.configureTestingModule({declarations: [SomeCompWithUrlTemplate]}); | 
					
						
							|  |  |  |              TestBed.compileComponents().then(() => { | 
					
						
							|  |  |  |                expect(() => TestBed.createComponent(SomeCompWithUrlTemplate)) | 
					
						
							|  |  |  |                    .toThrowError( | 
					
						
							|  |  |  |                        `Can't compile synchronously as ${stringify(SomeCompWithUrlTemplate)} is still being loaded!`); | 
					
						
							|  |  |  |              }); | 
					
						
							|  |  |  |            })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         it('should throw when using a templateUrl in a nested component that has not been compiled before', | 
					
						
							|  |  |  |            () => { | 
					
						
							|  |  |  |              TestBed.configureTestingModule({declarations: [SomeComp, ChildComp]}); | 
					
						
							|  |  |  |              TestBed.overrideComponent(ChildComp, {set: {templateUrl: '/someTpl.html'}}); | 
					
						
							|  |  |  |              TestBed.overrideComponent(SomeComp, {set: {template: '<child-cmp></child-cmp>'}}); | 
					
						
							|  |  |  |              TestBed.compileComponents().then(() => { | 
					
						
							|  |  |  |                expect(() => TestBed.createComponent(SomeComp)) | 
					
						
							|  |  |  |                    .toThrowError( | 
					
						
							|  |  |  |                        `Can't compile synchronously as ${stringify(ChildComp)} is still being loaded!`); | 
					
						
							|  |  |  |              }); | 
					
						
							|  |  |  |            }); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       describe('resolving loader', () => { | 
					
						
							|  |  |  |         class StubResourceLoader { | 
					
						
							|  |  |  |           get(url: string) { return Promise.resolve('hello'); } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         beforeEach(() => { | 
					
						
							|  |  |  |           TestBed.configureCompiler( | 
					
						
							|  |  |  |               {providers: [{provide: ResourceLoader, useClass: StubResourceLoader}]}); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         it('should allow to use templateUrl components that have been loaded before', async(() => { | 
					
						
							|  |  |  |              TestBed.configureTestingModule({declarations: [SomeCompWithUrlTemplate]}); | 
					
						
							|  |  |  |              TestBed.compileComponents().then(() => { | 
					
						
							|  |  |  |                const fixture = TestBed.createComponent(SomeCompWithUrlTemplate); | 
					
						
							|  |  |  |                expect(fixture.nativeElement).toHaveText('hello'); | 
					
						
							|  |  |  |              }); | 
					
						
							|  |  |  |            })); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-24 08:46:43 -07:00
										 |  |  |   describe('RuntimeCompiler', () => { | 
					
						
							|  |  |  |     let compiler: Compiler; | 
					
						
							| 
									
										
										
										
											2016-08-17 09:24:44 -07:00
										 |  |  |     let resourceLoader: SpyResourceLoader; | 
					
						
							| 
									
										
										
										
											2016-07-28 06:31:26 -07:00
										 |  |  |     let dirResolver: MockDirectiveResolver; | 
					
						
							| 
									
										
										
										
											2016-06-24 08:46:43 -07:00
										 |  |  |     let injector: Injector; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-17 09:24:44 -07:00
										 |  |  |     beforeEach(() => { | 
					
						
							|  |  |  |       TestBed.configureCompiler( | 
					
						
							|  |  |  |           {providers: [{provide: ResourceLoader, useClass: SpyResourceLoader}]}); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2016-06-24 08:46:43 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-02 02:32:27 -07:00
										 |  |  |     beforeEach(fakeAsync(inject( | 
					
						
							| 
									
										
										
										
											2016-08-18 11:20:02 -07:00
										 |  |  |         [Compiler, ResourceLoader, DirectiveResolver, Injector], | 
					
						
							|  |  |  |         (_compiler: Compiler, _resourceLoader: SpyResourceLoader, | 
					
						
							| 
									
										
										
										
											2016-07-28 06:31:26 -07:00
										 |  |  |          _dirResolver: MockDirectiveResolver, _injector: Injector) => { | 
					
						
							| 
									
										
										
										
											2016-06-24 08:46:43 -07:00
										 |  |  |           compiler = _compiler; | 
					
						
							| 
									
										
										
										
											2016-08-17 09:24:44 -07:00
										 |  |  |           resourceLoader = _resourceLoader; | 
					
						
							| 
									
										
										
										
											2016-07-28 06:31:26 -07:00
										 |  |  |           dirResolver = _dirResolver; | 
					
						
							| 
									
										
										
										
											2016-06-24 08:46:43 -07:00
										 |  |  |           injector = _injector; | 
					
						
							| 
									
										
										
										
											2016-08-02 02:32:27 -07:00
										 |  |  |         }))); | 
					
						
							| 
									
										
										
										
											2016-06-24 08:46:43 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-18 03:50:31 -07:00
										 |  |  |     describe('compileModuleAsync', () => { | 
					
						
							| 
									
										
										
										
											2016-06-28 09:54:42 -07:00
										 |  |  |       it('should allow to use templateUrl components', fakeAsync(() => { | 
					
						
							| 
									
										
										
										
											2016-07-25 00:36:30 -07:00
										 |  |  |            @NgModule({ | 
					
						
							|  |  |  |              declarations: [SomeCompWithUrlTemplate], | 
					
						
							|  |  |  |              entryComponents: [SomeCompWithUrlTemplate] | 
					
						
							|  |  |  |            }) | 
					
						
							| 
									
										
										
										
											2016-07-18 03:50:31 -07:00
										 |  |  |            class SomeModule { | 
					
						
							|  |  |  |            } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-17 09:24:44 -07:00
										 |  |  |            resourceLoader.spy('get').andCallFake(() => Promise.resolve('hello')); | 
					
						
							| 
									
										
										
										
											2016-07-18 03:50:31 -07:00
										 |  |  |            let ngModuleFactory: NgModuleFactory<any>; | 
					
						
							|  |  |  |            compiler.compileModuleAsync(SomeModule).then((f) => ngModuleFactory = f); | 
					
						
							| 
									
										
										
										
											2016-06-28 09:54:42 -07:00
										 |  |  |            tick(); | 
					
						
							| 
									
										
										
										
											2016-07-18 03:50:31 -07:00
										 |  |  |            expect(ngModuleFactory.moduleType).toBe(SomeModule); | 
					
						
							| 
									
										
										
										
											2016-06-28 09:54:42 -07:00
										 |  |  |          })); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-18 03:50:31 -07:00
										 |  |  |     describe('compileModuleSync', () => { | 
					
						
							| 
									
										
										
										
											2016-06-28 09:54:42 -07:00
										 |  |  |       it('should throw when using a templateUrl that has not been compiled before', () => { | 
					
						
							| 
									
										
										
										
											2016-07-25 00:36:30 -07:00
										 |  |  |         @NgModule( | 
					
						
							|  |  |  |             {declarations: [SomeCompWithUrlTemplate], entryComponents: [SomeCompWithUrlTemplate]}) | 
					
						
							| 
									
										
										
										
											2016-07-18 03:50:31 -07:00
										 |  |  |         class SomeModule { | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-17 09:24:44 -07:00
										 |  |  |         resourceLoader.spy('get').andCallFake(() => Promise.resolve('')); | 
					
						
							| 
									
										
										
										
											2016-07-18 03:50:31 -07:00
										 |  |  |         expect(() => compiler.compileModuleSync(SomeModule)) | 
					
						
							| 
									
										
										
										
											2016-06-28 09:54:42 -07:00
										 |  |  |             .toThrowError( | 
					
						
							|  |  |  |                 `Can't compile synchronously as ${stringify(SomeCompWithUrlTemplate)} is still being loaded!`); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should throw when using a templateUrl in a nested component that has not been compiled before', | 
					
						
							|  |  |  |          () => { | 
					
						
							| 
									
										
										
										
											2016-08-19 13:51:45 -07:00
										 |  |  |            @NgModule({declarations: [SomeComp, ChildComp], entryComponents: [SomeComp]}) | 
					
						
							| 
									
										
										
										
											2016-07-18 03:50:31 -07:00
										 |  |  |            class SomeModule { | 
					
						
							|  |  |  |            } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-17 09:24:44 -07:00
										 |  |  |            resourceLoader.spy('get').andCallFake(() => Promise.resolve('')); | 
					
						
							| 
									
										
										
										
											2016-08-19 13:51:45 -07:00
										 |  |  |            dirResolver.setView(SomeComp, new ViewMetadata({template: ''})); | 
					
						
							| 
									
										
										
										
											2016-07-28 06:31:26 -07:00
										 |  |  |            dirResolver.setView(ChildComp, new ViewMetadata({templateUrl: '/someTpl.html'})); | 
					
						
							| 
									
										
										
										
											2016-07-18 03:50:31 -07:00
										 |  |  |            expect(() => compiler.compileModuleSync(SomeModule)) | 
					
						
							| 
									
										
										
										
											2016-06-28 09:54:42 -07:00
										 |  |  |                .toThrowError( | 
					
						
							|  |  |  |                    `Can't compile synchronously as ${stringify(ChildComp)} is still being loaded!`); | 
					
						
							|  |  |  |          }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should allow to use templateUrl components that have been loaded before', | 
					
						
							|  |  |  |          fakeAsync(() => { | 
					
						
							| 
									
										
										
										
											2016-07-25 00:36:30 -07:00
										 |  |  |            @NgModule({ | 
					
						
							|  |  |  |              declarations: [SomeCompWithUrlTemplate], | 
					
						
							|  |  |  |              entryComponents: [SomeCompWithUrlTemplate] | 
					
						
							|  |  |  |            }) | 
					
						
							| 
									
										
										
										
											2016-07-18 03:50:31 -07:00
										 |  |  |            class SomeModule { | 
					
						
							|  |  |  |            } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-17 09:24:44 -07:00
										 |  |  |            resourceLoader.spy('get').andCallFake(() => Promise.resolve('hello')); | 
					
						
							| 
									
										
										
										
											2016-07-18 03:50:31 -07:00
										 |  |  |            compiler.compileModuleAsync(SomeModule); | 
					
						
							|  |  |  |            tick(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |            let ngModuleFactory = compiler.compileModuleSync(SomeModule); | 
					
						
							|  |  |  |            expect(ngModuleFactory).toBeTruthy(); | 
					
						
							| 
									
										
										
										
											2016-06-28 09:54:42 -07:00
										 |  |  |          })); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2016-06-24 08:46:43 -07:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2016-07-22 09:20:51 -07:00
										 |  |  | } |