2015-05-26 12:25:39 -04:00
|
|
|
import {
|
|
|
|
AsyncTestCompleter,
|
|
|
|
beforeEach,
|
|
|
|
ddescribe,
|
|
|
|
describe,
|
|
|
|
el,
|
|
|
|
expect,
|
|
|
|
iit,
|
|
|
|
inject,
|
|
|
|
it,
|
|
|
|
xit,
|
|
|
|
} from 'angular2/test_lib';
|
|
|
|
import {DOM} from 'angular2/src/dom/dom_adapter';
|
2015-06-24 04:43:36 -04:00
|
|
|
import {ViewLoader} from 'angular2/src/render/dom/compiler/view_loader';
|
2015-06-15 09:57:42 -04: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 12:25:39 -04:00
|
|
|
import {UrlResolver} from 'angular2/src/services/url_resolver';
|
2015-07-24 17:49:16 -04:00
|
|
|
import {AppRootUrl} from 'angular2/src/services/app_root_url';
|
2015-05-26 12:25:39 -04:00
|
|
|
|
|
|
|
import {ViewDefinition} from 'angular2/src/render/api';
|
2015-06-15 09:57:42 -04: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 13:21:25 -04:00
|
|
|
import {MockXHR} from 'angular2/src/render/xhr_mock';
|
2015-05-26 12:25:39 -04:00
|
|
|
|
|
|
|
export function main() {
|
2015-06-24 04:43:36 -04:00
|
|
|
describe('ViewLoader', () => {
|
2015-06-15 09:57:42 -04:00
|
|
|
var loader, xhr, styleUrlResolver, urlResolver;
|
2015-05-26 12:25:39 -04:00
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
xhr = new MockXHR();
|
2015-07-24 17:49:16 -04:00
|
|
|
urlResolver = new UrlResolver(new AppRootUrl(''));
|
2015-06-15 09:57:42 -04:00
|
|
|
styleUrlResolver = new StyleUrlResolver(urlResolver);
|
|
|
|
let styleInliner = new StyleInliner(xhr, styleUrlResolver, urlResolver);
|
2015-06-24 04:43:36 -04:00
|
|
|
loader = new ViewLoader(xhr, styleInliner, styleUrlResolver);
|
2015-05-26 12:25:39 -04:00
|
|
|
});
|
|
|
|
|
2015-06-10 08:40:24 -04:00
|
|
|
describe('html', () => {
|
|
|
|
it('should load inline templates', inject([AsyncTestCompleter], (async) => {
|
2015-06-24 04:27:53 -04:00
|
|
|
var view = new ViewDefinition({template: 'template template'});
|
|
|
|
loader.load(view).then((el) => {
|
2015-06-10 08:40:24 -04:00
|
|
|
expect(DOM.content(el)).toHaveText('template template');
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
2015-05-26 12:25:39 -04:00
|
|
|
|
2015-06-10 08:40:24 -04:00
|
|
|
it('should load templates through XHR', inject([AsyncTestCompleter], (async) => {
|
2015-06-24 04:27:53 -04:00
|
|
|
xhr.expect('http://ng.io/foo.html', 'xhr template');
|
|
|
|
var view = new ViewDefinition({templateAbsUrl: 'http://ng.io/foo.html'});
|
|
|
|
loader.load(view).then((el) => {
|
2015-06-10 08:40:24 -04:00
|
|
|
expect(DOM.content(el)).toHaveText('xhr template');
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
xhr.flush();
|
|
|
|
}));
|
2015-05-26 12:25:39 -04:00
|
|
|
|
2015-06-15 09:57:42 -04:00
|
|
|
it('should resolve urls in styles', inject([AsyncTestCompleter], (async) => {
|
2015-06-24 04:27:53 -04:00
|
|
|
xhr.expect('http://ng.io/foo.html',
|
2015-06-15 09:57:42 -04:00
|
|
|
'<style>.foo { background-image: url("double.jpg"); }</style>');
|
2015-06-24 04:27:53 -04:00
|
|
|
var view = new ViewDefinition({templateAbsUrl: 'http://ng.io/foo.html'});
|
|
|
|
loader.load(view).then((el) => {
|
2015-06-15 09:57:42 -04:00
|
|
|
expect(DOM.content(el))
|
2015-06-24 04:27:53 -04:00
|
|
|
.toHaveText(".foo { background-image: url('http://ng.io/double.jpg'); }");
|
2015-06-15 09:57:42 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
xhr.flush();
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should inline styles', inject([AsyncTestCompleter], (async) => {
|
|
|
|
let xhr = new FakeXHR();
|
2015-06-24 04:27:53 -04: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 09:57:42 -04:00
|
|
|
|
|
|
|
let styleInliner = new StyleInliner(xhr, styleUrlResolver, urlResolver);
|
2015-06-24 04:43:36 -04:00
|
|
|
let loader = new ViewLoader(xhr, styleInliner, styleUrlResolver);
|
2015-06-15 09:57:42 -04:00
|
|
|
|
2015-06-24 04:27:53 -04:00
|
|
|
var view = new ViewDefinition({templateAbsUrl: 'http://ng.io/foo.html'});
|
|
|
|
loader.load(view).then((el) => {
|
2015-06-15 09:57:42 -04:00
|
|
|
expect(DOM.getInnerHTML(el)).toEqual("<style>/* foo.css */\n</style>");
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
2015-06-10 08:40:24 -04:00
|
|
|
it('should return a new template element on each call',
|
|
|
|
inject([AsyncTestCompleter], (async) => {
|
|
|
|
var firstEl;
|
|
|
|
// we have only one xhr.expect, so there can only be one xhr call!
|
2015-06-24 04:27:53 -04:00
|
|
|
xhr.expect('http://ng.io/foo.html', 'xhr template');
|
|
|
|
var view = new ViewDefinition({templateAbsUrl: 'http://ng.io/foo.html'});
|
|
|
|
loader.load(view)
|
2015-06-10 08:40:24 -04:00
|
|
|
.then((el) => {
|
|
|
|
expect(DOM.content(el)).toHaveText('xhr template');
|
|
|
|
firstEl = el;
|
2015-06-24 04:27:53 -04:00
|
|
|
return loader.load(view);
|
2015-06-10 08:40:24 -04:00
|
|
|
})
|
|
|
|
.then((el) => {
|
|
|
|
expect(el).not.toBe(firstEl);
|
|
|
|
expect(DOM.content(el)).toHaveText('xhr template');
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
xhr.flush();
|
|
|
|
}));
|
2015-05-26 12:25:39 -04:00
|
|
|
|
2015-06-10 08:40:24 -04:00
|
|
|
it('should throw when no template is defined', () => {
|
2015-06-24 04:27:53 -04:00
|
|
|
var view = new ViewDefinition({template: null, templateAbsUrl: null});
|
|
|
|
expect(() => loader.load(view))
|
2015-06-10 08:40:24 -04: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 04:27:53 -04:00
|
|
|
xhr.expect('http://ng.io/foo.html', null);
|
|
|
|
var view = new ViewDefinition({templateAbsUrl: 'http://ng.io/foo.html'});
|
|
|
|
PromiseWrapper.then(loader.load(view), function(_) { throw 'Unexpected response'; },
|
2015-06-10 08:40:24 -04:00
|
|
|
function(error) {
|
|
|
|
expect(error.message)
|
2015-06-24 04:27:53 -04:00
|
|
|
.toEqual('Failed to fetch url "http://ng.io/foo.html"');
|
2015-06-10 08:40:24 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
xhr.flush();
|
|
|
|
}));
|
2015-07-17 18:33:40 -04: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">');
|
|
|
|
var view = new ViewDefinition({templateAbsUrl: 'http://ng.io/path/foo.html'});
|
|
|
|
loader.load(view).then((el) => {
|
|
|
|
expect(DOM.getInnerHTML(el)).toEqual('<img src="http://ng.io/path/logo.png">');
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
xhr.flush();
|
|
|
|
}));
|
2015-05-26 12:25:39 -04:00
|
|
|
});
|
|
|
|
|
2015-06-10 08:40:24 -04:00
|
|
|
describe('css', () => {
|
|
|
|
it('should load inline styles', inject([AsyncTestCompleter], (async) => {
|
2015-06-24 04:27:53 -04:00
|
|
|
var view = new ViewDefinition({template: 'html', styles: ['style 1', 'style 2']});
|
|
|
|
loader.load(view).then((el) => {
|
2015-06-10 08:40:24 -04:00
|
|
|
expect(DOM.getInnerHTML(el))
|
2015-06-15 09:57:42 -04:00
|
|
|
.toEqual('<style>style 1</style><style>style 2</style>html');
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should resolve urls in inline styles', inject([AsyncTestCompleter], (async) => {
|
2015-06-24 04:27:53 -04:00
|
|
|
xhr.expect('http://ng.io/foo.html', 'html');
|
|
|
|
var view = new ViewDefinition({
|
|
|
|
templateAbsUrl: 'http://ng.io/foo.html',
|
|
|
|
styles: ['.foo { background-image: url("double.jpg"); }']
|
|
|
|
});
|
|
|
|
loader.load(view).then((el) => {
|
2015-06-15 09:57:42 -04:00
|
|
|
expect(DOM.getInnerHTML(el))
|
2015-06-24 04:27:53 -04:00
|
|
|
.toEqual(
|
|
|
|
"<style>.foo { background-image: url('http://ng.io/double.jpg'); }</style>html");
|
2015-06-10 08:40:24 -04:00
|
|
|
async.done();
|
|
|
|
});
|
2015-06-24 04:27:53 -04:00
|
|
|
xhr.flush();
|
2015-06-10 08:40:24 -04:00
|
|
|
}));
|
|
|
|
|
|
|
|
it('should load templates through XHR', inject([AsyncTestCompleter], (async) => {
|
2015-06-24 04:27:53 -04: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');
|
|
|
|
var view = new ViewDefinition({
|
|
|
|
templateAbsUrl: 'http://ng.io/foo.html',
|
2015-06-10 08:40:24 -04:00
|
|
|
styles: ['i1'],
|
2015-06-24 04:27:53 -04:00
|
|
|
styleAbsUrls: ['http://ng.io/foo-1.css', 'http://ng.io/foo-2.css']
|
2015-06-10 08:40:24 -04:00
|
|
|
});
|
2015-06-24 04:27:53 -04:00
|
|
|
loader.load(view).then((el) => {
|
2015-06-10 08:40:24 -04:00
|
|
|
expect(DOM.getInnerHTML(el))
|
2015-06-15 09:57:42 -04:00
|
|
|
.toEqual('<style>i1</style><style>1</style><style>2</style>xhr template');
|
2015-06-10 08:40:24 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
xhr.flush();
|
|
|
|
}));
|
2015-05-26 12:25:39 -04:00
|
|
|
|
2015-06-15 09:57:42 -04:00
|
|
|
it('should inline styles', inject([AsyncTestCompleter], (async) => {
|
|
|
|
let xhr = new FakeXHR();
|
2015-06-24 04:27:53 -04:00
|
|
|
xhr.reply('http://ng.io/foo.html', '<p>template</p>');
|
|
|
|
xhr.reply('http://ng.io/foo.css', '/* foo.css */');
|
2015-06-15 09:57:42 -04:00
|
|
|
|
|
|
|
let styleInliner = new StyleInliner(xhr, styleUrlResolver, urlResolver);
|
2015-06-24 04:43:36 -04:00
|
|
|
let loader = new ViewLoader(xhr, styleInliner, styleUrlResolver);
|
2015-06-15 09:57:42 -04:00
|
|
|
|
2015-06-24 04:27:53 -04:00
|
|
|
var view = new ViewDefinition(
|
|
|
|
{templateAbsUrl: 'http://ng.io/foo.html', styles: ['@import "foo.css";']});
|
|
|
|
loader.load(view).then((el) => {
|
2015-06-15 09:57:42 -04:00
|
|
|
expect(DOM.getInnerHTML(el)).toEqual("<style>/* foo.css */\n</style><p>template</p>");
|
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
2015-06-10 08:40:24 -04:00
|
|
|
it('should return a rejected Promise when XHR loading fails',
|
|
|
|
inject([AsyncTestCompleter], (async) => {
|
2015-06-24 04:27:53 -04:00
|
|
|
xhr.expect('http://ng.io/foo.css', null);
|
|
|
|
var view = new ViewDefinition({template: '', styleAbsUrls: ['http://ng.io/foo.css']});
|
|
|
|
PromiseWrapper.then(loader.load(view), function(_) { throw 'Unexpected response'; },
|
2015-06-10 08:40:24 -04:00
|
|
|
function(error) {
|
|
|
|
expect(error.message)
|
2015-06-24 04:27:53 -04:00
|
|
|
.toEqual('Failed to fetch url "http://ng.io/foo.css"');
|
2015-06-10 08:40:24 -04:00
|
|
|
async.done();
|
|
|
|
});
|
|
|
|
xhr.flush();
|
|
|
|
}));
|
|
|
|
});
|
2015-05-26 12:25:39 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
class SomeComponent {}
|
|
|
|
|
2015-06-15 09:57:42 -04: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 12:25:39 -04:00
|
|
|
}
|