2016-04-06 18:58:23 -04:00
|
|
|
import {Component, provide} from 'angular2/core';
|
|
|
|
import {UrlResolver, XHR} from 'angular2/compiler';
|
2016-04-07 20:17:50 -04:00
|
|
|
import {AsyncTestCompleter, beforeEach, beforeEachProviders, ComponentFixture, ddescribe, describe, expect, fakeAsync, iit, inject, it, TestComponentBuilder, tick, xit} from 'angular2/testing_internal';
|
2016-04-06 18:58:23 -04:00
|
|
|
import {BaseException} from 'angular2/src/facade/exceptions';
|
|
|
|
import {CachedXHR} from 'angular2/src/platform/browser/xhr_cache';
|
|
|
|
import {setTemplateCache} from './xhr_cache_setter';
|
|
|
|
|
|
|
|
export function main() {
|
|
|
|
describe('CachedXHR', () => {
|
|
|
|
var xhr: CachedXHR;
|
|
|
|
|
|
|
|
function createCachedXHR(): CachedXHR {
|
|
|
|
setTemplateCache({'test.html': '<div>Hello</div>'});
|
|
|
|
return new CachedXHR();
|
|
|
|
}
|
2016-04-07 20:17:50 -04:00
|
|
|
beforeEachProviders(() => [provide(UrlResolver, {useClass: TestUrlResolver}), provide(XHR, {
|
|
|
|
useFactory: createCachedXHR
|
|
|
|
})]);
|
2016-04-06 18:58:23 -04:00
|
|
|
|
|
|
|
it('should throw exception if $templateCache is not found', () => {
|
|
|
|
setTemplateCache(null);
|
2016-04-07 20:17:50 -04:00
|
|
|
expect(() => {
|
|
|
|
xhr = new CachedXHR();
|
|
|
|
}).toThrowErrorWith('CachedXHR: Template cache was not found in $templateCache.');
|
2016-04-06 18:58:23 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should resolve the Promise with the cached file content on success',
|
|
|
|
inject([AsyncTestCompleter], (async) => {
|
|
|
|
setTemplateCache({'test.html': '<div>Hello</div>'});
|
|
|
|
xhr = new CachedXHR();
|
2016-04-07 20:17:50 -04:00
|
|
|
xhr.get('test.html').then((text) => {
|
|
|
|
expect(text).toEqual('<div>Hello</div>');
|
|
|
|
async.done();
|
|
|
|
});
|
2016-04-06 18:58:23 -04:00
|
|
|
}));
|
|
|
|
|
|
|
|
it('should reject the Promise on failure', inject([AsyncTestCompleter], (async) => {
|
|
|
|
xhr = new CachedXHR();
|
|
|
|
xhr.get('unknown.html')
|
|
|
|
.then((text) => { throw new BaseException('Not expected to succeed.'); })
|
|
|
|
.catch((error) => { async.done(); });
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should allow fakeAsync Tests to load components with templateUrl synchronously',
|
|
|
|
inject([TestComponentBuilder], fakeAsync((tcb: TestComponentBuilder) => {
|
|
|
|
let fixture: ComponentFixture;
|
|
|
|
tcb.createAsync(TestComponent).then((f) => { fixture = f; });
|
|
|
|
|
|
|
|
// This should initialize the fixture.
|
|
|
|
tick();
|
|
|
|
|
|
|
|
expect(fixture.debugElement.children[0].nativeElement).toHaveText('Hello');
|
|
|
|
})));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({selector: 'test-cmp', templateUrl: 'test.html'})
|
|
|
|
class TestComponent {
|
|
|
|
}
|
|
|
|
|
|
|
|
class TestUrlResolver extends UrlResolver {
|
|
|
|
resolve(baseUrl: string, url: string): string {
|
|
|
|
// Don't use baseUrl to get the same URL as templateUrl.
|
|
|
|
// This is to remove any difference between Dart and TS tests.
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
}
|