|
| 1 | +import {Component, provide} from 'angular2/core'; |
| 2 | +import {UrlResolver, XHR} from 'angular2/compiler'; |
| 3 | +import { |
| 4 | + AsyncTestCompleter, |
| 5 | + beforeEach, |
| 6 | + beforeEachProviders, |
| 7 | + ComponentFixture, |
| 8 | + ddescribe, |
| 9 | + describe, |
| 10 | + expect, |
| 11 | + fakeAsync, |
| 12 | + iit, |
| 13 | + inject, |
| 14 | + it, |
| 15 | + TestComponentBuilder, |
| 16 | + tick, |
| 17 | + xit |
| 18 | +} from 'angular2/testing_internal'; |
| 19 | +import {BaseException} from 'angular2/src/facade/exceptions'; |
| 20 | +import {CachedXHR} from 'angular2/src/platform/browser/xhr_cache'; |
| 21 | +import {setTemplateCache} from './xhr_cache_setter'; |
| 22 | + |
| 23 | +export function main() { |
| 24 | + describe('CachedXHR', () => { |
| 25 | + var xhr: CachedXHR; |
| 26 | + |
| 27 | + function createCachedXHR(): CachedXHR { |
| 28 | + setTemplateCache({'test.html': '<div>Hello</div>'}); |
| 29 | + return new CachedXHR(); |
| 30 | + } |
| 31 | + beforeEachProviders(() => [ |
| 32 | + provide(UrlResolver, {useClass: TestUrlResolver}), |
| 33 | + provide(XHR, {useFactory: createCachedXHR}) |
| 34 | + ]); |
| 35 | + |
| 36 | + it('should throw exception if $templateCache is not found', () => { |
| 37 | + setTemplateCache(null); |
| 38 | + expect(() => { xhr = new CachedXHR(); }) |
| 39 | + .toThrowErrorWith('CachedXHR: Template cache was not found in $templateCache.'); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should resolve the Promise with the cached file content on success', |
| 43 | + inject([AsyncTestCompleter], (async) => { |
| 44 | + setTemplateCache({'test.html': '<div>Hello</div>'}); |
| 45 | + xhr = new CachedXHR(); |
| 46 | + xhr.get('test.html') |
| 47 | + .then((text) => { |
| 48 | + expect(text).toEqual('<div>Hello</div>'); |
| 49 | + async.done(); |
| 50 | + }); |
| 51 | + })); |
| 52 | + |
| 53 | + it('should reject the Promise on failure', inject([AsyncTestCompleter], (async) => { |
| 54 | + xhr = new CachedXHR(); |
| 55 | + xhr.get('unknown.html') |
| 56 | + .then((text) => { throw new BaseException('Not expected to succeed.'); }) |
| 57 | + .catch((error) => { async.done(); }); |
| 58 | + })); |
| 59 | + |
| 60 | + it('should allow fakeAsync Tests to load components with templateUrl synchronously', |
| 61 | + inject([TestComponentBuilder], fakeAsync((tcb: TestComponentBuilder) => { |
| 62 | + let fixture: ComponentFixture; |
| 63 | + tcb.createAsync(TestComponent).then((f) => { fixture = f; }); |
| 64 | + |
| 65 | + // This should initialize the fixture. |
| 66 | + tick(); |
| 67 | + |
| 68 | + expect(fixture.debugElement.children[0].nativeElement).toHaveText('Hello'); |
| 69 | + }))); |
| 70 | + }); |
| 71 | +} |
| 72 | + |
| 73 | +@Component({selector: 'test-cmp', templateUrl: 'test.html'}) |
| 74 | +class TestComponent { |
| 75 | +} |
| 76 | + |
| 77 | +class TestUrlResolver extends UrlResolver { |
| 78 | + resolve(baseUrl: string, url: string): string { |
| 79 | + // Don't use baseUrl to get the same URL as templateUrl. |
| 80 | + // This is to remove any difference between Dart and TS tests. |
| 81 | + return url; |
| 82 | + } |
| 83 | +} |
0 commit comments