| 
									
										
										
										
											2015-03-30 15:45:03 +02:00
										 |  |  | import { | 
					
						
							|  |  |  |   beforeEach, | 
					
						
							|  |  |  |   ddescribe, | 
					
						
							|  |  |  |   describe, | 
					
						
							|  |  |  |   el, | 
					
						
							|  |  |  |   expect, | 
					
						
							|  |  |  |   iit, | 
					
						
							|  |  |  |   it, | 
					
						
							|  |  |  | } from 'angular2/test_lib'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import {MockTemplateResolver} from 'angular2/src/mock/template_resolver_mock'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-28 18:17:00 -07:00
										 |  |  | import {Component} from 'angular2/src/core/annotations_impl/annotations'; | 
					
						
							|  |  |  | import {View} from 'angular2/src/core/annotations_impl/view'; | 
					
						
							| 
									
										
										
										
											2015-03-30 15:45:03 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | import {isBlank} from 'angular2/src/facade/lang'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export function main() { | 
					
						
							|  |  |  |   describe('MockTemplateResolver', () => { | 
					
						
							|  |  |  |     var resolver; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     beforeEach(() => { | 
					
						
							|  |  |  |       resolver = new MockTemplateResolver(); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-09 21:20:11 +02:00
										 |  |  |     describe('View overriding', () => { | 
					
						
							| 
									
										
										
										
											2015-03-30 15:45:03 +02:00
										 |  |  |       it('should fallback to the default TemplateResolver when templates are not overridden', () => { | 
					
						
							|  |  |  |         var template = resolver.resolve(SomeComponent); | 
					
						
							| 
									
										
										
										
											2015-04-09 21:20:11 +02:00
										 |  |  |         expect(template.template).toEqual('template'); | 
					
						
							| 
									
										
										
										
											2015-03-30 15:45:03 +02:00
										 |  |  |         expect(template.directives).toEqual([SomeDirective]); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-09 21:20:11 +02:00
										 |  |  |       it('should allow overriding the @View', () => { | 
					
						
							|  |  |  |         resolver.setView(SomeComponent, new View({template: 'overridden template'})); | 
					
						
							| 
									
										
										
										
											2015-03-30 15:45:03 +02:00
										 |  |  |         var template = resolver.resolve(SomeComponent); | 
					
						
							| 
									
										
										
										
											2015-04-09 21:20:11 +02:00
										 |  |  |         expect(template.template).toEqual('overridden template'); | 
					
						
							| 
									
										
										
										
											2015-03-30 15:45:03 +02:00
										 |  |  |         expect(isBlank(template.directives)).toBe(true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should not allow overriding a template after it has been resolved', () => { | 
					
						
							|  |  |  |         resolver.resolve(SomeComponent); | 
					
						
							|  |  |  |         expect(() => { | 
					
						
							| 
									
										
										
										
											2015-04-09 21:20:11 +02:00
										 |  |  |           resolver.setView(SomeComponent, new View({template: 'overridden template'})); | 
					
						
							| 
									
										
										
										
											2015-03-30 15:45:03 +02:00
										 |  |  |         }).toThrowError('The component SomeComponent has already been compiled, its configuration can not be changed'); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-09 21:20:11 +02:00
										 |  |  |     describe('inline template definition overriding', () => { | 
					
						
							|  |  |  |       it('should allow overriding the default template', () => { | 
					
						
							| 
									
										
										
										
											2015-03-30 15:45:03 +02:00
										 |  |  |         resolver.setInlineTemplate(SomeComponent, 'overridden template'); | 
					
						
							|  |  |  |         var template = resolver.resolve(SomeComponent); | 
					
						
							| 
									
										
										
										
											2015-04-09 21:20:11 +02:00
										 |  |  |         expect(template.template).toEqual('overridden template'); | 
					
						
							| 
									
										
										
										
											2015-03-30 15:45:03 +02:00
										 |  |  |         expect(template.directives).toEqual([SomeDirective]); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-09 21:20:11 +02:00
										 |  |  |       it('should allow overriding an overriden @View', () => { | 
					
						
							|  |  |  |         resolver.setView(SomeComponent, new View({template: 'overridden template'})); | 
					
						
							| 
									
										
										
										
											2015-03-30 15:45:03 +02:00
										 |  |  |         resolver.setInlineTemplate(SomeComponent, 'overridden template x 2'); | 
					
						
							|  |  |  |         var template = resolver.resolve(SomeComponent); | 
					
						
							| 
									
										
										
										
											2015-04-09 21:20:11 +02:00
										 |  |  |         expect(template.template).toEqual('overridden template x 2'); | 
					
						
							| 
									
										
										
										
											2015-03-30 15:45:03 +02:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should not allow overriding a template after it has been resolved', () => { | 
					
						
							|  |  |  |         resolver.resolve(SomeComponent); | 
					
						
							|  |  |  |         expect(() => { | 
					
						
							|  |  |  |           resolver.setInlineTemplate(SomeComponent, 'overridden template'); | 
					
						
							|  |  |  |         }).toThrowError('The component SomeComponent has already been compiled, its configuration can not be changed'); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     describe('Directive overriding', () => { | 
					
						
							|  |  |  |       it('should allow overriding a directive from the default template', () => { | 
					
						
							|  |  |  |         resolver.overrideTemplateDirective(SomeComponent, SomeDirective, SomeOtherDirective); | 
					
						
							|  |  |  |         var template = resolver.resolve(SomeComponent); | 
					
						
							|  |  |  |         expect(template.directives.length).toEqual(1); | 
					
						
							|  |  |  |         expect(template.directives[0]).toBe(SomeOtherDirective); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-09 21:20:11 +02:00
										 |  |  |       it('should allow overriding a directive from an overriden @View', () => { | 
					
						
							|  |  |  |         resolver.setView(SomeComponent, new View({directives: [SomeOtherDirective]})); | 
					
						
							| 
									
										
										
										
											2015-03-30 15:45:03 +02:00
										 |  |  |         resolver.overrideTemplateDirective(SomeComponent, SomeOtherDirective, SomeComponent); | 
					
						
							|  |  |  |         var template = resolver.resolve(SomeComponent); | 
					
						
							|  |  |  |         expect(template.directives.length).toEqual(1); | 
					
						
							|  |  |  |         expect(template.directives[0]).toBe(SomeComponent); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should throw when the overridden directive is not present', () => { | 
					
						
							|  |  |  |         resolver.overrideTemplateDirective(SomeComponent, SomeOtherDirective, SomeDirective); | 
					
						
							|  |  |  |         expect(() => { resolver.resolve(SomeComponent); }) | 
					
						
							|  |  |  |             .toThrowError('Overriden directive SomeOtherDirective not found in the template of SomeComponent'); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should not allow overriding a directive after its template has been resolved', () => { | 
					
						
							|  |  |  |         resolver.resolve(SomeComponent); | 
					
						
							|  |  |  |         expect(() => { | 
					
						
							|  |  |  |           resolver.overrideTemplateDirective(SomeComponent, SomeDirective, SomeOtherDirective); | 
					
						
							|  |  |  |         }).toThrowError('The component SomeComponent has already been compiled, its configuration can not be changed'); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @Component({selector: 'cmp'}) | 
					
						
							| 
									
										
										
										
											2015-04-09 21:20:11 +02:00
										 |  |  | @View({ | 
					
						
							|  |  |  |   template: 'template', | 
					
						
							| 
									
										
										
										
											2015-03-30 15:45:03 +02:00
										 |  |  |   directives: [SomeDirective], | 
					
						
							|  |  |  | }) | 
					
						
							|  |  |  | class SomeComponent { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class SomeDirective { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class SomeOtherDirective { | 
					
						
							|  |  |  | } |