| 
									
										
										
										
											2015-05-26 09:25:39 -07:00
										 |  |  | import { | 
					
						
							|  |  |  |   AsyncTestCompleter, | 
					
						
							|  |  |  |   beforeEach, | 
					
						
							|  |  |  |   ddescribe, | 
					
						
							|  |  |  |   describe, | 
					
						
							|  |  |  |   el, | 
					
						
							|  |  |  |   expect, | 
					
						
							|  |  |  |   iit, | 
					
						
							|  |  |  |   inject, | 
					
						
							|  |  |  |   it, | 
					
						
							|  |  |  |   xit, | 
					
						
							|  |  |  | } from 'angular2/test_lib'; | 
					
						
							| 
									
										
										
										
											2015-07-24 15:28:44 -07:00
										 |  |  | import {ViewLoader, TemplateAndStyles} from 'angular2/src/render/dom/compiler/view_loader'; | 
					
						
							| 
									
										
										
										
											2015-06-15 15:57:42 +02:00
										 |  |  | import {StyleInliner} from 'angular2/src/render/dom/compiler/style_inliner'; | 
					
						
							|  |  |  | import {StyleUrlResolver} from 'angular2/src/render/dom/compiler/style_url_resolver'; | 
					
						
							| 
									
										
										
										
											2015-05-26 09:25:39 -07:00
										 |  |  | import {UrlResolver} from 'angular2/src/services/url_resolver'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-15 15:57:42 +02:00
										 |  |  | import {PromiseWrapper, Promise} from 'angular2/src/facade/async'; | 
					
						
							|  |  |  | import {MapWrapper, ListWrapper} from 'angular2/src/facade/collection'; | 
					
						
							|  |  |  | import {XHR} from 'angular2/src/render/xhr'; | 
					
						
							| 
									
										
										
										
											2015-06-09 10:21:25 -07:00
										 |  |  | import {MockXHR} from 'angular2/src/render/xhr_mock'; | 
					
						
							| 
									
										
										
										
											2015-07-24 15:28:44 -07:00
										 |  |  | import {ViewDefinition} from 'angular2/src/render/api'; | 
					
						
							| 
									
										
										
										
											2015-05-26 09:25:39 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | export function main() { | 
					
						
							| 
									
										
										
										
											2015-06-24 10:43:36 +02:00
										 |  |  |   describe('ViewLoader', () => { | 
					
						
							| 
									
										
										
										
											2015-07-24 15:28:44 -07:00
										 |  |  |     var loader: ViewLoader; | 
					
						
							|  |  |  |     var xhr, styleUrlResolver, urlResolver; | 
					
						
							| 
									
										
										
										
											2015-05-26 09:25:39 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     beforeEach(() => { | 
					
						
							|  |  |  |       xhr = new MockXHR(); | 
					
						
							| 
									
										
										
										
											2015-07-27 14:15:02 -07:00
										 |  |  |       urlResolver = new UrlResolver(); | 
					
						
							| 
									
										
										
										
											2015-06-15 15:57:42 +02:00
										 |  |  |       styleUrlResolver = new StyleUrlResolver(urlResolver); | 
					
						
							|  |  |  |       let styleInliner = new StyleInliner(xhr, styleUrlResolver, urlResolver); | 
					
						
							| 
									
										
										
										
											2015-06-24 10:43:36 +02:00
										 |  |  |       loader = new ViewLoader(xhr, styleInliner, styleUrlResolver); | 
					
						
							| 
									
										
										
										
											2015-05-26 09:25:39 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-10 14:40:24 +02:00
										 |  |  |     describe('html', () => { | 
					
						
							|  |  |  |       it('should load inline templates', inject([AsyncTestCompleter], (async) => { | 
					
						
							| 
									
										
										
										
											2015-07-24 15:28:44 -07:00
										 |  |  |            loader.load(new ViewDefinition({template: 'template template'})) | 
					
						
							|  |  |  |                .then((el) => { | 
					
						
							|  |  |  |                  expect(el.template).toEqual('template template'); | 
					
						
							|  |  |  |                  async.done(); | 
					
						
							|  |  |  |                }); | 
					
						
							| 
									
										
										
										
											2015-06-10 14:40:24 +02:00
										 |  |  |          })); | 
					
						
							| 
									
										
										
										
											2015-05-26 09:25:39 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-10 14:40:24 +02:00
										 |  |  |       it('should load templates through XHR', inject([AsyncTestCompleter], (async) => { | 
					
						
							| 
									
										
										
										
											2015-06-24 10:27:53 +02:00
										 |  |  |            xhr.expect('http://ng.io/foo.html', 'xhr template'); | 
					
						
							| 
									
										
										
										
											2015-07-24 15:28:44 -07:00
										 |  |  |            loader.load(new ViewDefinition({templateAbsUrl: 'http://ng.io/foo.html'})) | 
					
						
							|  |  |  |                .then((el) => { | 
					
						
							|  |  |  |                  expect(el.template).toEqual('xhr template'); | 
					
						
							|  |  |  |                  async.done(); | 
					
						
							|  |  |  |                }); | 
					
						
							| 
									
										
										
										
											2015-06-10 14:40:24 +02:00
										 |  |  |            xhr.flush(); | 
					
						
							|  |  |  |          })); | 
					
						
							| 
									
										
										
										
											2015-05-26 09:25:39 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-15 15:57:42 +02:00
										 |  |  |       it('should resolve urls in styles', inject([AsyncTestCompleter], (async) => { | 
					
						
							| 
									
										
										
										
											2015-06-24 10:27:53 +02:00
										 |  |  |            xhr.expect('http://ng.io/foo.html', | 
					
						
							| 
									
										
										
										
											2015-06-15 15:57:42 +02:00
										 |  |  |                       '<style>.foo { background-image: url("double.jpg"); }</style>'); | 
					
						
							| 
									
										
										
										
											2015-07-24 15:28:44 -07:00
										 |  |  |            loader.load(new ViewDefinition({templateAbsUrl: 'http://ng.io/foo.html'})) | 
					
						
							|  |  |  |                .then((el) => { | 
					
						
							|  |  |  |                  expect(el.template).toEqual(''); | 
					
						
							|  |  |  |                  expect(el.styles) | 
					
						
							|  |  |  |                      .toEqual([".foo { background-image: url('http://ng.io/double.jpg'); }"]); | 
					
						
							|  |  |  |                  async.done(); | 
					
						
							|  |  |  |                }); | 
					
						
							| 
									
										
										
										
											2015-06-15 15:57:42 +02:00
										 |  |  |            xhr.flush(); | 
					
						
							|  |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should inline styles', inject([AsyncTestCompleter], (async) => { | 
					
						
							|  |  |  |            let xhr = new FakeXHR(); | 
					
						
							| 
									
										
										
										
											2015-06-24 10:27:53 +02:00
										 |  |  |            xhr.reply('http://ng.io/foo.html', '<style>@import "foo.css";</style>'); | 
					
						
							|  |  |  |            xhr.reply('http://ng.io/foo.css', '/* foo.css */'); | 
					
						
							| 
									
										
										
										
											2015-06-15 15:57:42 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |            let styleInliner = new StyleInliner(xhr, styleUrlResolver, urlResolver); | 
					
						
							| 
									
										
										
										
											2015-06-24 10:43:36 +02:00
										 |  |  |            let loader = new ViewLoader(xhr, styleInliner, styleUrlResolver); | 
					
						
							| 
									
										
										
										
											2015-06-15 15:57:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-24 15:28:44 -07:00
										 |  |  |            loader.load(new ViewDefinition({templateAbsUrl: 'http://ng.io/foo.html'})) | 
					
						
							| 
									
										
										
										
											2015-06-10 14:40:24 +02:00
										 |  |  |                .then((el) => { | 
					
						
							| 
									
										
										
										
											2015-07-24 15:28:44 -07:00
										 |  |  |                  expect(el.template).toEqual(''); | 
					
						
							|  |  |  |                  expect(el.styles).toEqual(["/* foo.css */\n"]); | 
					
						
							| 
									
										
										
										
											2015-06-10 14:40:24 +02:00
										 |  |  |                  async.done(); | 
					
						
							|  |  |  |                }); | 
					
						
							|  |  |  |          })); | 
					
						
							| 
									
										
										
										
											2015-05-26 09:25:39 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-10 14:40:24 +02:00
										 |  |  |       it('should throw when no template is defined', () => { | 
					
						
							| 
									
										
										
										
											2015-07-24 15:28:44 -07:00
										 |  |  |         expect(() => loader.load(new ViewDefinition({template: null, templateAbsUrl: null}))) | 
					
						
							| 
									
										
										
										
											2015-06-10 14:40:24 +02:00
										 |  |  |             .toThrowError('View should have either the templateUrl or template property set'); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should return a rejected Promise when XHR loading fails', | 
					
						
							|  |  |  |          inject([AsyncTestCompleter], (async) => { | 
					
						
							| 
									
										
										
										
											2015-06-24 10:27:53 +02:00
										 |  |  |            xhr.expect('http://ng.io/foo.html', null); | 
					
						
							| 
									
										
										
										
											2015-07-24 15:28:44 -07:00
										 |  |  |            PromiseWrapper.then( | 
					
						
							|  |  |  |                loader.load(new ViewDefinition({templateAbsUrl: 'http://ng.io/foo.html'})), | 
					
						
							|  |  |  |                function(_) { throw 'Unexpected response'; }, | 
					
						
							|  |  |  |                function(error) { | 
					
						
							|  |  |  |                  expect(error.message).toEqual('Failed to fetch url "http://ng.io/foo.html"'); | 
					
						
							|  |  |  |                  async.done(); | 
					
						
							|  |  |  |                }); | 
					
						
							| 
									
										
										
										
											2015-06-10 14:40:24 +02:00
										 |  |  |            xhr.flush(); | 
					
						
							|  |  |  |          })); | 
					
						
							| 
									
										
										
										
											2015-07-24 15:28:44 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-17 15:33:40 -07:00
										 |  |  |       it('should replace $baseUrl in attributes with the template base url', | 
					
						
							|  |  |  |          inject([AsyncTestCompleter], (async) => { | 
					
						
							|  |  |  |            xhr.expect('http://ng.io/path/foo.html', '<img src="$baseUrl/logo.png">'); | 
					
						
							| 
									
										
										
										
											2015-07-24 15:28:44 -07:00
										 |  |  |            loader.load(new ViewDefinition({templateAbsUrl: 'http://ng.io/path/foo.html'})) | 
					
						
							|  |  |  |                .then((el) => { | 
					
						
							|  |  |  |                  expect(el.template).toEqual('<img src="http://ng.io/path/logo.png">'); | 
					
						
							|  |  |  |                  async.done(); | 
					
						
							|  |  |  |                }); | 
					
						
							| 
									
										
										
										
											2015-07-17 15:33:40 -07:00
										 |  |  |            xhr.flush(); | 
					
						
							|  |  |  |          })); | 
					
						
							| 
									
										
										
										
											2015-05-26 09:25:39 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-10 14:40:24 +02:00
										 |  |  |     describe('css', () => { | 
					
						
							|  |  |  |       it('should load inline styles', inject([AsyncTestCompleter], (async) => { | 
					
						
							| 
									
										
										
										
											2015-07-24 15:28:44 -07:00
										 |  |  |            loader.load(new ViewDefinition({template: 'html', styles: ['style 1', 'style 2']})) | 
					
						
							|  |  |  |                .then((el) => { | 
					
						
							|  |  |  |                  expect(el.template).toEqual('html'); | 
					
						
							|  |  |  |                  expect(el.styles).toEqual(['style 1', 'style 2']); | 
					
						
							|  |  |  |                  async.done(); | 
					
						
							|  |  |  |                }); | 
					
						
							| 
									
										
										
										
											2015-06-15 15:57:42 +02:00
										 |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should resolve urls in inline styles', inject([AsyncTestCompleter], (async) => { | 
					
						
							| 
									
										
										
										
											2015-06-24 10:27:53 +02:00
										 |  |  |            xhr.expect('http://ng.io/foo.html', 'html'); | 
					
						
							| 
									
										
										
										
											2015-07-24 15:28:44 -07:00
										 |  |  |            loader.load(new ViewDefinition({ | 
					
						
							|  |  |  |                    templateAbsUrl: 'http://ng.io/foo.html', | 
					
						
							|  |  |  |                    styles: ['.foo { background-image: url("double.jpg"); }'] | 
					
						
							|  |  |  |                  })) | 
					
						
							|  |  |  |                .then((el) => { | 
					
						
							|  |  |  |                  expect(el.template).toEqual('html'); | 
					
						
							|  |  |  |                  expect(el.styles) | 
					
						
							|  |  |  |                      .toEqual([".foo { background-image: url('http://ng.io/double.jpg'); }"]); | 
					
						
							|  |  |  |                  async.done(); | 
					
						
							|  |  |  |                }); | 
					
						
							| 
									
										
										
										
											2015-06-24 10:27:53 +02:00
										 |  |  |            xhr.flush(); | 
					
						
							| 
									
										
										
										
											2015-06-10 14:40:24 +02:00
										 |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       it('should load templates through XHR', inject([AsyncTestCompleter], (async) => { | 
					
						
							| 
									
										
										
										
											2015-06-24 10:27:53 +02:00
										 |  |  |            xhr.expect('http://ng.io/foo.html', 'xhr template'); | 
					
						
							|  |  |  |            xhr.expect('http://ng.io/foo-1.css', '1'); | 
					
						
							|  |  |  |            xhr.expect('http://ng.io/foo-2.css', '2'); | 
					
						
							| 
									
										
										
										
											2015-07-24 15:28:44 -07:00
										 |  |  |            loader.load(new ViewDefinition({ | 
					
						
							|  |  |  |                    templateAbsUrl: 'http://ng.io/foo.html', | 
					
						
							|  |  |  |                    styles: ['i1'], | 
					
						
							|  |  |  |                    styleAbsUrls: ['http://ng.io/foo-1.css', 'http://ng.io/foo-2.css'] | 
					
						
							|  |  |  |                  })) | 
					
						
							|  |  |  |                .then((el) => { | 
					
						
							|  |  |  |                  expect(el.template).toEqual('xhr template'); | 
					
						
							|  |  |  |                  expect(el.styles).toEqual(['i1', '1', '2']); | 
					
						
							|  |  |  |                  async.done(); | 
					
						
							|  |  |  |                }); | 
					
						
							| 
									
										
										
										
											2015-06-10 14:40:24 +02:00
										 |  |  |            xhr.flush(); | 
					
						
							|  |  |  |          })); | 
					
						
							| 
									
										
										
										
											2015-05-26 09:25:39 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-15 15:57:42 +02:00
										 |  |  |       it('should inline styles', inject([AsyncTestCompleter], (async) => { | 
					
						
							|  |  |  |            let xhr = new FakeXHR(); | 
					
						
							| 
									
										
										
										
											2015-06-24 10:27:53 +02:00
										 |  |  |            xhr.reply('http://ng.io/foo.html', '<p>template</p>'); | 
					
						
							|  |  |  |            xhr.reply('http://ng.io/foo.css', '/* foo.css */'); | 
					
						
							| 
									
										
										
										
											2015-06-15 15:57:42 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |            let styleInliner = new StyleInliner(xhr, styleUrlResolver, urlResolver); | 
					
						
							| 
									
										
										
										
											2015-06-24 10:43:36 +02:00
										 |  |  |            let loader = new ViewLoader(xhr, styleInliner, styleUrlResolver); | 
					
						
							| 
									
										
										
										
											2015-06-15 15:57:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-24 15:28:44 -07:00
										 |  |  |            loader.load( | 
					
						
							|  |  |  |                      new ViewDefinition( | 
					
						
							|  |  |  |                          {templateAbsUrl: 'http://ng.io/foo.html', styles: ['@import "foo.css";']})) | 
					
						
							|  |  |  |                .then((el) => { | 
					
						
							|  |  |  |                  expect(el.template).toEqual("<p>template</p>"); | 
					
						
							|  |  |  |                  expect(el.styles).toEqual(["/* foo.css */\n"]); | 
					
						
							|  |  |  |                  async.done(); | 
					
						
							|  |  |  |                }); | 
					
						
							| 
									
										
										
										
											2015-06-15 15:57:42 +02:00
										 |  |  |          })); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-10 14:40:24 +02:00
										 |  |  |       it('should return a rejected Promise when XHR loading fails', | 
					
						
							|  |  |  |          inject([AsyncTestCompleter], (async) => { | 
					
						
							| 
									
										
										
										
											2015-06-24 10:27:53 +02:00
										 |  |  |            xhr.expect('http://ng.io/foo.css', null); | 
					
						
							| 
									
										
										
										
											2015-07-24 15:28:44 -07:00
										 |  |  |            PromiseWrapper.then( | 
					
						
							|  |  |  |                loader.load( | 
					
						
							|  |  |  |                    new ViewDefinition({template: '', styleAbsUrls: ['http://ng.io/foo.css']})), | 
					
						
							|  |  |  |                function(_) { throw 'Unexpected response'; }, | 
					
						
							|  |  |  |                function(error) { | 
					
						
							|  |  |  |                  expect(error.message).toEqual('Failed to fetch url "http://ng.io/foo.css"'); | 
					
						
							|  |  |  |                  async.done(); | 
					
						
							|  |  |  |                }); | 
					
						
							| 
									
										
										
										
											2015-06-10 14:40:24 +02:00
										 |  |  |            xhr.flush(); | 
					
						
							|  |  |  |          })); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-05-26 09:25:39 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class SomeComponent {} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-15 15:57:42 +02:00
										 |  |  | class FakeXHR extends XHR { | 
					
						
							|  |  |  |   _responses: Map<string, string> = new Map(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   constructor() { super(); } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   get(url: string): Promise<string> { | 
					
						
							|  |  |  |     return this._responses.has(url) ? PromiseWrapper.resolve(this._responses.get(url)) : | 
					
						
							|  |  |  |                                       PromiseWrapper.reject('xhr error', null); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   reply(url: string, response: string): void { this._responses.set(url, response); } | 
					
						
							| 
									
										
										
										
											2015-05-26 09:25:39 -07:00
										 |  |  | } |