| 
									
										
										
										
											2015-03-23 14:10:55 -07:00
										 |  |  | import { | 
					
						
							|  |  |  |   AsyncTestCompleter, | 
					
						
							|  |  |  |   beforeEach, | 
					
						
							|  |  |  |   ddescribe, | 
					
						
							|  |  |  |   describe, | 
					
						
							|  |  |  |   el, | 
					
						
							|  |  |  |   expect, | 
					
						
							|  |  |  |   iit, | 
					
						
							|  |  |  |   inject, | 
					
						
							|  |  |  |   it, | 
					
						
							|  |  |  |   xit, | 
					
						
							|  |  |  | } from 'angular2/test_lib'; | 
					
						
							|  |  |  | import {DOM} from 'angular2/src/dom/dom_adapter'; | 
					
						
							|  |  |  | import {TemplateLoader} from 'angular2/src/render/dom/compiler/template_loader'; | 
					
						
							|  |  |  | import {UrlResolver} from 'angular2/src/services/url_resolver'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-09 21:20:11 +02:00
										 |  |  | import {ViewDefinition} from 'angular2/src/render/api'; | 
					
						
							| 
									
										
										
										
											2015-03-23 14:10:55 -07:00
										 |  |  | import {PromiseWrapper} from 'angular2/src/facade/async'; | 
					
						
							| 
									
										
										
										
											2015-03-31 16:55:46 +02:00
										 |  |  | import {MockXHR} from 'angular2/src/mock/xhr_mock'; | 
					
						
							| 
									
										
										
										
											2015-03-23 14:10:55 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | export function main() { | 
					
						
							|  |  |  |   describe('TemplateLoader', () => { | 
					
						
							|  |  |  |     var loader, xhr; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     beforeEach(() => { | 
					
						
							| 
									
										
										
										
											2015-03-31 16:55:46 +02:00
										 |  |  |       xhr = new MockXHR(); | 
					
						
							| 
									
										
										
										
											2015-03-23 14:10:55 -07:00
										 |  |  |       loader = new TemplateLoader(xhr, new FakeUrlResolver()); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should load inline templates', inject([AsyncTestCompleter], (async) => { | 
					
						
							| 
									
										
										
										
											2015-04-09 21:20:11 +02:00
										 |  |  |       var template = new ViewDefinition({template: 'template template'}); | 
					
						
							| 
									
										
										
										
											2015-03-23 14:10:55 -07:00
										 |  |  |       loader.load(template).then( (el) => { | 
					
						
							| 
									
										
										
										
											2015-04-09 21:20:11 +02:00
										 |  |  |         expect(DOM.content(el)).toHaveText('template template'); | 
					
						
							| 
									
										
										
										
											2015-03-23 14:10:55 -07:00
										 |  |  |         async.done(); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should load templates through XHR', inject([AsyncTestCompleter], (async) => { | 
					
						
							|  |  |  |       xhr.expect('base/foo', 'xhr template'); | 
					
						
							| 
									
										
										
										
											2015-04-09 21:20:11 +02:00
										 |  |  |       var template = new ViewDefinition({absUrl: 'base/foo'}); | 
					
						
							| 
									
										
										
										
											2015-03-23 14:10:55 -07:00
										 |  |  |       loader.load(template).then((el) => { | 
					
						
							|  |  |  |         expect(DOM.content(el)).toHaveText('xhr template'); | 
					
						
							|  |  |  |         async.done(); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       xhr.flush(); | 
					
						
							|  |  |  |     })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-30 15:27:28 -07:00
										 |  |  |     it('should cache template loaded through XHR but clone it as the compiler might change it', inject([AsyncTestCompleter], (async) => { | 
					
						
							| 
									
										
										
										
											2015-03-23 14:10:55 -07:00
										 |  |  |       var firstEl; | 
					
						
							| 
									
										
										
										
											2015-04-30 15:27:28 -07:00
										 |  |  |       // we have only one xhr.expect, so there can only be one xhr call!
 | 
					
						
							| 
									
										
										
										
											2015-03-23 14:10:55 -07:00
										 |  |  |       xhr.expect('base/foo', 'xhr template'); | 
					
						
							| 
									
										
										
										
											2015-04-09 21:20:11 +02:00
										 |  |  |       var template = new ViewDefinition({absUrl: 'base/foo'}); | 
					
						
							| 
									
										
										
										
											2015-03-23 14:10:55 -07:00
										 |  |  |       loader.load(template) | 
					
						
							|  |  |  |         .then((el) => { | 
					
						
							| 
									
										
										
										
											2015-04-30 15:27:28 -07:00
										 |  |  |           expect(DOM.content(el)).toHaveText('xhr template'); | 
					
						
							| 
									
										
										
										
											2015-03-23 14:10:55 -07:00
										 |  |  |           firstEl = el; | 
					
						
							|  |  |  |           return loader.load(template); | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |         .then((el) =>{ | 
					
						
							| 
									
										
										
										
											2015-04-30 15:27:28 -07:00
										 |  |  |           expect(el).not.toBe(firstEl); | 
					
						
							| 
									
										
										
										
											2015-03-23 14:10:55 -07:00
										 |  |  |           expect(DOM.content(el)).toHaveText('xhr template'); | 
					
						
							|  |  |  |           async.done(); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |       xhr.flush(); | 
					
						
							|  |  |  |     })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should throw when no template is defined', () => { | 
					
						
							| 
									
										
										
										
											2015-04-09 21:20:11 +02:00
										 |  |  |       var template =  new ViewDefinition({template: null, absUrl: null}); | 
					
						
							| 
									
										
										
										
											2015-03-23 14:10:55 -07:00
										 |  |  |       expect(() => loader.load(template)) | 
					
						
							| 
									
										
										
										
											2015-04-09 21:20:11 +02:00
										 |  |  |         .toThrowError('View should have either the url or template property set'); | 
					
						
							| 
									
										
										
										
											2015-03-23 14:10:55 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     it('should return a rejected Promise when xhr loading fails', inject([AsyncTestCompleter], (async) => { | 
					
						
							|  |  |  |       xhr.expect('base/foo', null); | 
					
						
							| 
									
										
										
										
											2015-04-09 21:20:11 +02:00
										 |  |  |       var template = new ViewDefinition({absUrl: 'base/foo'}); | 
					
						
							| 
									
										
										
										
											2015-03-23 14:10:55 -07:00
										 |  |  |       PromiseWrapper.then(loader.load(template), | 
					
						
							|  |  |  |         function(_) { throw 'Unexpected response'; }, | 
					
						
							|  |  |  |         function(error) { | 
					
						
							|  |  |  |           expect(error).toEqual('Failed to load base/foo'); | 
					
						
							|  |  |  |           async.done(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       ) | 
					
						
							|  |  |  |       xhr.flush(); | 
					
						
							|  |  |  |     })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class SomeComponent { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class FakeUrlResolver extends UrlResolver { | 
					
						
							|  |  |  |   constructor() { | 
					
						
							|  |  |  |     super(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   resolve(baseUrl: string, url: string): string { | 
					
						
							|  |  |  |     return baseUrl + url; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } |