fix(platform-browser-dynamic): Rename CACHED_TEMPLATE_PROVIDER to RESOURCE_CACHE_PROVIDER (#10866)
* fix(platform-browser-dynamic): Rename CACHED_TEMPLATE_PROVIDER to RESOURCE_CACHE_PROVIDER Closes #9741 BREAKING CHANGE: `CACHED_TEMPLATE_PROVIDER` is now renamed to `RESOURCE_CACHE_PROVIDER` Before: ```js import {CACHED_TEMPLATE_PROVIDER} from '@angular/platform-browser-dynamic'; ``` After: ```js import {RESOURCE_CACHE_PROVIDER} from '@angular/platform-browser-dynamic'; ``` * Rename XHR -> ResourceLoader
This commit is contained in:
@ -13,7 +13,7 @@
|
||||
*/
|
||||
import * as i18n from './src/i18n/index';
|
||||
|
||||
export {COMPILER_PROVIDERS, CompileDiDependencyMetadata, CompileDirectiveMetadata, CompileFactoryMetadata, CompileIdentifierMetadata, CompileMetadataWithIdentifier, CompilePipeMetadata, CompileProviderMetadata, CompileQueryMetadata, CompileTemplateMetadata, CompileTokenMetadata, CompileTypeMetadata, CompilerConfig, DEFAULT_PACKAGE_URL_PROVIDER, DirectiveResolver, NgModuleResolver, OfflineCompiler, PipeResolver, RenderTypes, RuntimeCompiler, SourceModule, TEMPLATE_TRANSFORMS, UrlResolver, XHR, analyzeAppProvidersForDeprecatedConfiguration, createOfflineCompileUrlResolver, platformCoreDynamic} from './src/compiler';
|
||||
export {COMPILER_PROVIDERS, CompileDiDependencyMetadata, CompileDirectiveMetadata, CompileFactoryMetadata, CompileIdentifierMetadata, CompileMetadataWithIdentifier, CompilePipeMetadata, CompileProviderMetadata, CompileQueryMetadata, CompileTemplateMetadata, CompileTokenMetadata, CompileTypeMetadata, CompilerConfig, DEFAULT_PACKAGE_URL_PROVIDER, DirectiveResolver, NgModuleResolver, OfflineCompiler, PipeResolver, RenderTypes, ResourceLoader, RuntimeCompiler, SourceModule, TEMPLATE_TRANSFORMS, UrlResolver, analyzeAppProvidersForDeprecatedConfiguration, createOfflineCompileUrlResolver, platformCoreDynamic} from './src/compiler';
|
||||
export {DEFAULT_INTERPOLATION_CONFIG, InterpolationConfig} from './src/ml_parser/interpolation_config';
|
||||
export {ElementSchemaRegistry} from './src/schema/element_schema_registry';
|
||||
export {i18n};
|
||||
|
@ -15,7 +15,7 @@ export * from './compile_metadata';
|
||||
export * from './offline_compiler';
|
||||
export {RuntimeCompiler} from './runtime_compiler';
|
||||
export * from './url_resolver';
|
||||
export * from './xhr';
|
||||
export * from './resource_loader';
|
||||
|
||||
export {DirectiveResolver} from './directive_resolver';
|
||||
export {PipeResolver} from './pipe_resolver';
|
||||
@ -41,12 +41,13 @@ import {DirectiveResolver} from './directive_resolver';
|
||||
import {PipeResolver} from './pipe_resolver';
|
||||
import {NgModuleResolver} from './ng_module_resolver';
|
||||
import {Console, Reflector, reflector, ReflectorReader, ReflectionCapabilities} from '../core_private';
|
||||
import {XHR} from './xhr';
|
||||
import {ResourceLoader} from './resource_loader';
|
||||
import * as i18n from './i18n/index';
|
||||
|
||||
const _NO_XHR: XHR = {
|
||||
const _NO_RESOURCE_LOADER: ResourceLoader = {
|
||||
get(url: string): Promise<string>{
|
||||
throw new Error(`No XHR implementation has been provided. Can't read the url "${url}"`);}
|
||||
throw new Error(
|
||||
`No ResourceLoader implementation has been provided. Can't read the url "${url}"`);}
|
||||
};
|
||||
|
||||
/**
|
||||
@ -56,7 +57,7 @@ const _NO_XHR: XHR = {
|
||||
export const COMPILER_PROVIDERS: Array<any|Type<any>|{[k: string]: any}|any[]> = [
|
||||
{provide: Reflector, useValue: reflector},
|
||||
{provide: ReflectorReader, useExisting: Reflector},
|
||||
{provide: XHR, useValue: _NO_XHR},
|
||||
{provide: ResourceLoader, useValue: _NO_RESOURCE_LOADER},
|
||||
Console,
|
||||
Lexer,
|
||||
Parser,
|
||||
@ -101,7 +102,7 @@ export function analyzeAppProvidersForDeprecatedConfiguration(appProviders: any[
|
||||
const deprecationMessages: string[] = [];
|
||||
|
||||
// Note: This is a hack to still support the old way
|
||||
// of configuring platform directives / pipes and the compiler xhr.
|
||||
// of configuring platform directives / pipes and the compiler resource loader.
|
||||
// This will soon be deprecated!
|
||||
const tempInj = ReflectiveInjector.resolveAndCreate(appProviders);
|
||||
const compilerConfig: CompilerConfig = tempInj.get(CompilerConfig, null);
|
||||
@ -112,11 +113,11 @@ export function analyzeAppProvidersForDeprecatedConfiguration(appProviders: any[
|
||||
deprecationMessages.push(
|
||||
`Passing CompilerConfig as a regular provider is deprecated. Use "compilerOptions" use a custom "CompilerFactory" platform provider instead.`);
|
||||
}
|
||||
const xhr = tempInj.get(XHR, null);
|
||||
if (xhr) {
|
||||
compilerProviders.push([{provide: XHR, useValue: xhr}]);
|
||||
const resourceLoader = tempInj.get(ResourceLoader, null);
|
||||
if (resourceLoader) {
|
||||
compilerProviders.push([{provide: ResourceLoader, useValue: resourceLoader}]);
|
||||
deprecationMessages.push(
|
||||
`Passing XHR as regular provider is deprecated. Pass the provider via "compilerOptions" instead.`);
|
||||
`Passing ResourceLoader as regular provider is deprecated. Pass the provider via "compilerOptions" instead.`);
|
||||
}
|
||||
const compilerOptions: CompilerOptions = {
|
||||
useJit: useJit,
|
||||
|
@ -15,36 +15,36 @@ import {isBlank, isPresent} from './facade/lang';
|
||||
import * as html from './ml_parser/ast';
|
||||
import {HtmlParser} from './ml_parser/html_parser';
|
||||
import {InterpolationConfig} from './ml_parser/interpolation_config';
|
||||
import {ResourceLoader} from './resource_loader';
|
||||
import {extractStyleUrls, isStyleUrlResolvable} from './style_url_resolver';
|
||||
import {PreparsedElementType, preparseElement} from './template_parser/template_preparser';
|
||||
import {UrlResolver} from './url_resolver';
|
||||
import {SyncAsyncResult} from './util';
|
||||
import {XHR} from './xhr';
|
||||
|
||||
@Injectable()
|
||||
export class DirectiveNormalizer {
|
||||
private _xhrCache = new Map<string, Promise<string>>();
|
||||
private _resourceLoaderCache = new Map<string, Promise<string>>();
|
||||
|
||||
constructor(
|
||||
private _xhr: XHR, private _urlResolver: UrlResolver, private _htmlParser: HtmlParser,
|
||||
private _config: CompilerConfig) {}
|
||||
private _resourceLoader: ResourceLoader, private _urlResolver: UrlResolver,
|
||||
private _htmlParser: HtmlParser, private _config: CompilerConfig) {}
|
||||
|
||||
clearCache() { this._xhrCache.clear(); }
|
||||
clearCache() { this._resourceLoaderCache.clear(); }
|
||||
|
||||
clearCacheFor(normalizedDirective: CompileDirectiveMetadata) {
|
||||
if (!normalizedDirective.isComponent) {
|
||||
return;
|
||||
}
|
||||
this._xhrCache.delete(normalizedDirective.template.templateUrl);
|
||||
this._resourceLoaderCache.delete(normalizedDirective.template.templateUrl);
|
||||
normalizedDirective.template.externalStylesheets.forEach(
|
||||
(stylesheet) => { this._xhrCache.delete(stylesheet.moduleUrl); });
|
||||
(stylesheet) => { this._resourceLoaderCache.delete(stylesheet.moduleUrl); });
|
||||
}
|
||||
|
||||
private _fetch(url: string): Promise<string> {
|
||||
var result = this._xhrCache.get(url);
|
||||
var result = this._resourceLoaderCache.get(url);
|
||||
if (!result) {
|
||||
result = this._xhr.get(url);
|
||||
this._xhrCache.set(url, result);
|
||||
result = this._resourceLoader.get(url);
|
||||
this._resourceLoaderCache.set(url, result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
@ -6,11 +6,10 @@
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
// TODO: vsavkin rename it into TemplateLoader
|
||||
/**
|
||||
* An interface for retrieving documents by URL that the compiler uses
|
||||
* to load templates.
|
||||
*/
|
||||
export class XHR {
|
||||
export class ResourceLoader {
|
||||
get(url: string): Promise<string> { return null; }
|
||||
}
|
@ -9,13 +9,13 @@
|
||||
import {CompileDirectiveMetadata, CompileStylesheetMetadata, CompileTemplateMetadata, CompileTypeMetadata} from '@angular/compiler/src/compile_metadata';
|
||||
import {CompilerConfig} from '@angular/compiler/src/config';
|
||||
import {DirectiveNormalizer} from '@angular/compiler/src/directive_normalizer';
|
||||
import {XHR} from '@angular/compiler/src/xhr';
|
||||
import {MockXHR} from '@angular/compiler/testing/xhr_mock';
|
||||
import {ResourceLoader} from '@angular/compiler/src/resource_loader';
|
||||
import {MockResourceLoader} from '@angular/compiler/testing/resource_loader_mock';
|
||||
import {ViewEncapsulation} from '@angular/core/src/metadata/view';
|
||||
import {TestBed} from '@angular/core/testing';
|
||||
import {AsyncTestCompleter, beforeEach, beforeEachProviders, ddescribe, describe, expect, iit, inject, it, xit} from '@angular/core/testing/testing_internal';
|
||||
|
||||
import {SpyXHR} from './spies';
|
||||
import {SpyResourceLoader} from './spies';
|
||||
import {TEST_COMPILER_PROVIDERS} from './test_bindings';
|
||||
|
||||
export function main() {
|
||||
@ -115,9 +115,10 @@ export function main() {
|
||||
|
||||
it('should load a template from a url that is resolved against moduleUrl',
|
||||
inject(
|
||||
[AsyncTestCompleter, DirectiveNormalizer, XHR],
|
||||
(async: AsyncTestCompleter, normalizer: DirectiveNormalizer, xhr: MockXHR) => {
|
||||
xhr.expect('package:some/module/sometplurl.html', 'a');
|
||||
[AsyncTestCompleter, DirectiveNormalizer, ResourceLoader],
|
||||
(async: AsyncTestCompleter, normalizer: DirectiveNormalizer,
|
||||
resourceLoader: MockResourceLoader) => {
|
||||
resourceLoader.expect('package:some/module/sometplurl.html', 'a');
|
||||
normalizer
|
||||
.normalizeTemplateAsync(dirType, new CompileTemplateMetadata({
|
||||
encapsulation: null,
|
||||
@ -131,14 +132,15 @@ export function main() {
|
||||
expect(template.templateUrl).toEqual('package:some/module/sometplurl.html');
|
||||
async.done();
|
||||
});
|
||||
xhr.flush();
|
||||
resourceLoader.flush();
|
||||
}));
|
||||
|
||||
it('should resolve styles on the annotation against the moduleUrl',
|
||||
inject(
|
||||
[AsyncTestCompleter, DirectiveNormalizer, XHR],
|
||||
(async: AsyncTestCompleter, normalizer: DirectiveNormalizer, xhr: MockXHR) => {
|
||||
xhr.expect('package:some/module/tpl/sometplurl.html', '');
|
||||
[AsyncTestCompleter, DirectiveNormalizer, ResourceLoader],
|
||||
(async: AsyncTestCompleter, normalizer: DirectiveNormalizer,
|
||||
resourceLoader: MockResourceLoader) => {
|
||||
resourceLoader.expect('package:some/module/tpl/sometplurl.html', '');
|
||||
normalizer
|
||||
.normalizeTemplateAsync(dirType, new CompileTemplateMetadata({
|
||||
encapsulation: null,
|
||||
@ -151,14 +153,15 @@ export function main() {
|
||||
expect(template.styleUrls).toEqual(['package:some/module/test.css']);
|
||||
async.done();
|
||||
});
|
||||
xhr.flush();
|
||||
resourceLoader.flush();
|
||||
}));
|
||||
|
||||
it('should resolve styles in the template against the templateUrl',
|
||||
inject(
|
||||
[AsyncTestCompleter, DirectiveNormalizer, XHR],
|
||||
(async: AsyncTestCompleter, normalizer: DirectiveNormalizer, xhr: MockXHR) => {
|
||||
xhr.expect(
|
||||
[AsyncTestCompleter, DirectiveNormalizer, ResourceLoader],
|
||||
(async: AsyncTestCompleter, normalizer: DirectiveNormalizer,
|
||||
resourceLoader: MockResourceLoader) => {
|
||||
resourceLoader.expect(
|
||||
'package:some/module/tpl/sometplurl.html', '<style>@import test.css</style>');
|
||||
normalizer
|
||||
.normalizeTemplateAsync(dirType, new CompileTemplateMetadata({
|
||||
@ -172,21 +175,24 @@ export function main() {
|
||||
expect(template.styleUrls).toEqual(['package:some/module/tpl/test.css']);
|
||||
async.done();
|
||||
});
|
||||
xhr.flush();
|
||||
resourceLoader.flush();
|
||||
}));
|
||||
|
||||
});
|
||||
|
||||
describe('normalizeExternalStylesheets', () => {
|
||||
|
||||
beforeEach(
|
||||
() => { TestBed.configureCompiler({providers: [{provide: XHR, useClass: SpyXHR}]}); });
|
||||
beforeEach(() => {
|
||||
TestBed.configureCompiler(
|
||||
{providers: [{provide: ResourceLoader, useClass: SpyResourceLoader}]});
|
||||
});
|
||||
|
||||
it('should load an external stylesheet',
|
||||
inject(
|
||||
[AsyncTestCompleter, DirectiveNormalizer, XHR],
|
||||
(async: AsyncTestCompleter, normalizer: DirectiveNormalizer, xhr: SpyXHR) => {
|
||||
programXhrSpy(xhr, {'package:some/module/test.css': 'a'});
|
||||
[AsyncTestCompleter, DirectiveNormalizer, ResourceLoader],
|
||||
(async: AsyncTestCompleter, normalizer: DirectiveNormalizer,
|
||||
resourceLoader: SpyResourceLoader) => {
|
||||
programResourceLoaderSpy(resourceLoader, {'package:some/module/test.css': 'a'});
|
||||
normalizer
|
||||
.normalizeExternalStylesheets(new CompileTemplateMetadata({
|
||||
template: '',
|
||||
@ -206,9 +212,10 @@ export function main() {
|
||||
|
||||
it('should load stylesheets referenced by external stylesheets',
|
||||
inject(
|
||||
[AsyncTestCompleter, DirectiveNormalizer, XHR],
|
||||
(async: AsyncTestCompleter, normalizer: DirectiveNormalizer, xhr: SpyXHR) => {
|
||||
programXhrSpy(xhr, {
|
||||
[AsyncTestCompleter, DirectiveNormalizer, ResourceLoader],
|
||||
(async: AsyncTestCompleter, normalizer: DirectiveNormalizer,
|
||||
resourceLoader: SpyResourceLoader) => {
|
||||
programResourceLoaderSpy(resourceLoader, {
|
||||
'package:some/module/test.css': 'a@import "test2.css"',
|
||||
'package:some/module/test2.css': 'b'
|
||||
});
|
||||
@ -238,9 +245,10 @@ export function main() {
|
||||
describe('caching', () => {
|
||||
it('should work for templateUrl',
|
||||
inject(
|
||||
[AsyncTestCompleter, DirectiveNormalizer, XHR],
|
||||
(async: AsyncTestCompleter, normalizer: DirectiveNormalizer, xhr: MockXHR) => {
|
||||
xhr.expect('package:some/module/cmp.html', 'a');
|
||||
[AsyncTestCompleter, DirectiveNormalizer, ResourceLoader],
|
||||
(async: AsyncTestCompleter, normalizer: DirectiveNormalizer,
|
||||
resourceLoader: MockResourceLoader) => {
|
||||
resourceLoader.expect('package:some/module/cmp.html', 'a');
|
||||
var templateMeta = new CompileTemplateMetadata({
|
||||
templateUrl: 'cmp.html',
|
||||
});
|
||||
@ -254,7 +262,7 @@ export function main() {
|
||||
expect(templates[1].template).toEqual('a');
|
||||
async.done();
|
||||
});
|
||||
xhr.flush();
|
||||
resourceLoader.flush();
|
||||
}));
|
||||
|
||||
});
|
||||
@ -426,7 +434,7 @@ export function main() {
|
||||
});
|
||||
}
|
||||
|
||||
function programXhrSpy(spy: SpyXHR, results: {[key: string]: string}) {
|
||||
function programResourceLoaderSpy(spy: SpyResourceLoader, results: {[key: string]: string}) {
|
||||
spy.spy('get').andCallFake((url: string): Promise<any> => {
|
||||
var result = results[url];
|
||||
if (result) {
|
||||
|
@ -6,21 +6,21 @@
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {DirectiveResolver, XHR, i18n} from '@angular/compiler';
|
||||
import {DirectiveResolver, ResourceLoader, i18n} from '@angular/compiler';
|
||||
import {MockDirectiveResolver} from '@angular/compiler/testing';
|
||||
import {Compiler, Component, DebugElement, Injector, TRANSLATIONS, TRANSLATIONS_FORMAT} from '@angular/core';
|
||||
import {TestBed, fakeAsync} from '@angular/core/testing';
|
||||
import {beforeEach, TestComponentBuilder, ddescribe, describe, iit, inject, it, xdescribe, xit,} from '@angular/core/testing/testing_internal';
|
||||
import {expect} from '@angular/platform-browser/testing/matchers';
|
||||
import {By} from '@angular/platform-browser/src/dom/debug/by';
|
||||
import {SpyXHR} from '../spies';
|
||||
import {SpyResourceLoader} from '../spies';
|
||||
import {NgLocalization} from '@angular/common';
|
||||
import {stringifyElement} from '@angular/platform-browser/testing/browser_util';
|
||||
|
||||
export function main() {
|
||||
describe('i18n integration spec', () => {
|
||||
let compiler: Compiler;
|
||||
let xhr: SpyXHR;
|
||||
let xhr: SpyResourceLoader;
|
||||
let tcb: TestComponentBuilder;
|
||||
let dirResolver: MockDirectiveResolver;
|
||||
let injector: Injector;
|
||||
@ -28,7 +28,7 @@ export function main() {
|
||||
beforeEach(() => {
|
||||
TestBed.configureCompiler({
|
||||
providers: [
|
||||
{provide: XHR, useClass: SpyXHR},
|
||||
{provide: ResourceLoader, useClass: SpyResourceLoader},
|
||||
{provide: NgLocalization, useClass: FrLocalization},
|
||||
{provide: TRANSLATIONS, useValue: XTB},
|
||||
{provide: TRANSLATIONS_FORMAT, useValue: 'xtb'},
|
||||
@ -37,8 +37,8 @@ export function main() {
|
||||
});
|
||||
|
||||
beforeEach(fakeAsync(inject(
|
||||
[Compiler, TestComponentBuilder, XHR, DirectiveResolver, Injector],
|
||||
(_compiler: Compiler, _tcb: TestComponentBuilder, _xhr: SpyXHR,
|
||||
[Compiler, TestComponentBuilder, ResourceLoader, DirectiveResolver, Injector],
|
||||
(_compiler: Compiler, _tcb: TestComponentBuilder, _xhr: SpyResourceLoader,
|
||||
_dirResolver: MockDirectiveResolver, _injector: Injector) => {
|
||||
compiler = _compiler;
|
||||
tcb = _tcb;
|
||||
|
@ -6,15 +6,15 @@
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {MockXHR} from '@angular/compiler/testing/xhr_mock';
|
||||
import {MockResourceLoader} from '@angular/compiler/testing/resource_loader_mock';
|
||||
import {AsyncTestCompleter, beforeEach, ddescribe, describe, expect, iit, inject, it} from '@angular/core/testing/testing_internal';
|
||||
import {isPresent} from '../src/facade/lang';
|
||||
|
||||
export function main() {
|
||||
describe('MockXHR', () => {
|
||||
var xhr: MockXHR;
|
||||
describe('MockResourceLoader', () => {
|
||||
var resourceLoader: MockResourceLoader;
|
||||
|
||||
beforeEach(() => { xhr = new MockXHR(); });
|
||||
beforeEach(() => { resourceLoader = new MockResourceLoader(); });
|
||||
|
||||
function expectResponse(
|
||||
request: Promise<string>, url: string, response: string, done: () => void = null) {
|
||||
@ -45,70 +45,71 @@ export function main() {
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
var url = '/foo';
|
||||
var response = 'bar';
|
||||
xhr.when(url, response);
|
||||
expectResponse(xhr.get(url), url, response, () => async.done());
|
||||
xhr.flush();
|
||||
resourceLoader.when(url, response);
|
||||
expectResponse(resourceLoader.get(url), url, response, () => async.done());
|
||||
resourceLoader.flush();
|
||||
}));
|
||||
|
||||
it('should return an error from the definitions',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
var url = '/foo';
|
||||
var response: any /** TODO #9100 */ = null;
|
||||
xhr.when(url, response);
|
||||
expectResponse(xhr.get(url), url, response, () => async.done());
|
||||
xhr.flush();
|
||||
resourceLoader.when(url, response);
|
||||
expectResponse(resourceLoader.get(url), url, response, () => async.done());
|
||||
resourceLoader.flush();
|
||||
}));
|
||||
|
||||
it('should return a response from the expectations',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
var url = '/foo';
|
||||
var response = 'bar';
|
||||
xhr.expect(url, response);
|
||||
expectResponse(xhr.get(url), url, response, () => async.done());
|
||||
xhr.flush();
|
||||
resourceLoader.expect(url, response);
|
||||
expectResponse(resourceLoader.get(url), url, response, () => async.done());
|
||||
resourceLoader.flush();
|
||||
}));
|
||||
|
||||
it('should return an error from the expectations',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
var url = '/foo';
|
||||
var response: any /** TODO #9100 */ = null;
|
||||
xhr.expect(url, response);
|
||||
expectResponse(xhr.get(url), url, response, () => async.done());
|
||||
xhr.flush();
|
||||
resourceLoader.expect(url, response);
|
||||
expectResponse(resourceLoader.get(url), url, response, () => async.done());
|
||||
resourceLoader.flush();
|
||||
}));
|
||||
|
||||
it('should not reuse expectations', () => {
|
||||
var url = '/foo';
|
||||
var response = 'bar';
|
||||
xhr.expect(url, response);
|
||||
xhr.get(url);
|
||||
xhr.get(url);
|
||||
expect(() => { xhr.flush(); }).toThrowError('Unexpected request /foo');
|
||||
resourceLoader.expect(url, response);
|
||||
resourceLoader.get(url);
|
||||
resourceLoader.get(url);
|
||||
expect(() => { resourceLoader.flush(); }).toThrowError('Unexpected request /foo');
|
||||
});
|
||||
|
||||
it('should return expectations before definitions',
|
||||
inject([AsyncTestCompleter], (async: AsyncTestCompleter) => {
|
||||
var url = '/foo';
|
||||
xhr.when(url, 'when');
|
||||
xhr.expect(url, 'expect');
|
||||
expectResponse(xhr.get(url), url, 'expect');
|
||||
expectResponse(xhr.get(url), url, 'when', () => async.done());
|
||||
xhr.flush();
|
||||
resourceLoader.when(url, 'when');
|
||||
resourceLoader.expect(url, 'expect');
|
||||
expectResponse(resourceLoader.get(url), url, 'expect');
|
||||
expectResponse(resourceLoader.get(url), url, 'when', () => async.done());
|
||||
resourceLoader.flush();
|
||||
}));
|
||||
|
||||
it('should throw when there is no definitions or expectations', () => {
|
||||
xhr.get('/foo');
|
||||
expect(() => { xhr.flush(); }).toThrowError('Unexpected request /foo');
|
||||
resourceLoader.get('/foo');
|
||||
expect(() => { resourceLoader.flush(); }).toThrowError('Unexpected request /foo');
|
||||
});
|
||||
|
||||
it('should throw when flush is called without any pending requests',
|
||||
() => { expect(() => { xhr.flush(); }).toThrowError('No pending requests to flush'); });
|
||||
it('should throw when flush is called without any pending requests', () => {
|
||||
expect(() => { resourceLoader.flush(); }).toThrowError('No pending requests to flush');
|
||||
});
|
||||
|
||||
it('should throw on unsatisfied expectations', () => {
|
||||
xhr.expect('/foo', 'bar');
|
||||
xhr.when('/bar', 'foo');
|
||||
xhr.get('/bar');
|
||||
expect(() => { xhr.flush(); }).toThrowError('Unsatisfied requests: /foo');
|
||||
resourceLoader.expect('/foo', 'bar');
|
||||
resourceLoader.when('/bar', 'foo');
|
||||
resourceLoader.get('/bar');
|
||||
expect(() => { resourceLoader.flush(); }).toThrowError('Unsatisfied requests: /foo');
|
||||
});
|
||||
});
|
||||
}
|
@ -6,7 +6,7 @@
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {DirectiveResolver, XHR} from '@angular/compiler';
|
||||
import {DirectiveResolver, ResourceLoader} from '@angular/compiler';
|
||||
import {MockDirectiveResolver} from '@angular/compiler/testing';
|
||||
import {Compiler, Component, ComponentFactory, Injectable, Injector, Input, NgModule, NgModuleFactory, Type} from '@angular/core';
|
||||
import {ComponentFixture, TestBed, fakeAsync, tick} from '@angular/core/testing';
|
||||
@ -16,7 +16,7 @@ import {expect} from '@angular/platform-browser/testing/matchers';
|
||||
import {ViewMetadata} from '../core_private';
|
||||
import {stringify} from '../src/facade/lang';
|
||||
|
||||
import {SpyXHR} from './spies';
|
||||
import {SpyResourceLoader} from './spies';
|
||||
|
||||
@Component({selector: 'child-cmp', template: 'childComp'})
|
||||
class ChildComp {
|
||||
@ -33,21 +33,23 @@ class SomeCompWithUrlTemplate {
|
||||
export function main() {
|
||||
describe('RuntimeCompiler', () => {
|
||||
let compiler: Compiler;
|
||||
let xhr: SpyXHR;
|
||||
let resourceLoader: SpyResourceLoader;
|
||||
let tcb: TestComponentBuilder;
|
||||
let dirResolver: MockDirectiveResolver;
|
||||
let injector: Injector;
|
||||
|
||||
beforeEach(
|
||||
() => { TestBed.configureCompiler({providers: [{provide: XHR, useClass: SpyXHR}]}); });
|
||||
beforeEach(() => {
|
||||
TestBed.configureCompiler(
|
||||
{providers: [{provide: ResourceLoader, useClass: SpyResourceLoader}]});
|
||||
});
|
||||
|
||||
beforeEach(fakeAsync(inject(
|
||||
[Compiler, TestComponentBuilder, XHR, DirectiveResolver, Injector],
|
||||
(_compiler: Compiler, _tcb: TestComponentBuilder, _xhr: SpyXHR,
|
||||
[Compiler, TestComponentBuilder, ResourceLoader, DirectiveResolver, Injector],
|
||||
(_compiler: Compiler, _tcb: TestComponentBuilder, _resourceLoader: SpyResourceLoader,
|
||||
_dirResolver: MockDirectiveResolver, _injector: Injector) => {
|
||||
compiler = _compiler;
|
||||
tcb = _tcb;
|
||||
xhr = _xhr;
|
||||
resourceLoader = _resourceLoader;
|
||||
dirResolver = _dirResolver;
|
||||
injector = _injector;
|
||||
})));
|
||||
@ -55,13 +57,13 @@ export function main() {
|
||||
describe('clearCacheFor', () => {
|
||||
it('should support changing the content of a template referenced via templateUrl',
|
||||
fakeAsync(() => {
|
||||
xhr.spy('get').andCallFake(() => Promise.resolve('init'));
|
||||
resourceLoader.spy('get').andCallFake(() => Promise.resolve('init'));
|
||||
let compFixture =
|
||||
tcb.overrideView(SomeComp, new ViewMetadata({templateUrl: '/myComp.html'}))
|
||||
.createFakeAsync(SomeComp);
|
||||
expect(compFixture.nativeElement).toHaveText('init');
|
||||
|
||||
xhr.spy('get').andCallFake(() => Promise.resolve('new content'));
|
||||
resourceLoader.spy('get').andCallFake(() => Promise.resolve('new content'));
|
||||
// Note: overrideView is calling .clearCacheFor...
|
||||
compFixture = tcb.overrideView(SomeComp, new ViewMetadata({templateUrl: '/myComp.html'}))
|
||||
.createFakeAsync(SomeComp);
|
||||
@ -91,7 +93,7 @@ export function main() {
|
||||
|
||||
describe('compileComponentSync', () => {
|
||||
it('should throw when using a templateUrl that has not been compiled before', () => {
|
||||
xhr.spy('get').andCallFake(() => Promise.resolve(''));
|
||||
resourceLoader.spy('get').andCallFake(() => Promise.resolve(''));
|
||||
expect(() => tcb.createSync(SomeCompWithUrlTemplate))
|
||||
.toThrowError(
|
||||
`Can't compile synchronously as ${stringify(SomeCompWithUrlTemplate)} is still being loaded!`);
|
||||
@ -99,7 +101,7 @@ export function main() {
|
||||
|
||||
it('should throw when using a templateUrl in a nested component that has not been compiled before',
|
||||
() => {
|
||||
xhr.spy('get').andCallFake(() => Promise.resolve(''));
|
||||
resourceLoader.spy('get').andCallFake(() => Promise.resolve(''));
|
||||
let localTcb =
|
||||
tcb.overrideView(SomeComp, new ViewMetadata({template: '', directives: [ChildComp]}))
|
||||
.overrideView(ChildComp, new ViewMetadata({templateUrl: '/someTpl.html'}));
|
||||
@ -110,7 +112,7 @@ export function main() {
|
||||
|
||||
it('should allow to use templateUrl components that have been loaded before',
|
||||
fakeAsync(() => {
|
||||
xhr.spy('get').andCallFake(() => Promise.resolve('hello'));
|
||||
resourceLoader.spy('get').andCallFake(() => Promise.resolve('hello'));
|
||||
tcb.createFakeAsync(SomeCompWithUrlTemplate);
|
||||
let compFixture = tcb.createSync(SomeCompWithUrlTemplate);
|
||||
expect(compFixture.nativeElement).toHaveText('hello');
|
||||
@ -126,7 +128,7 @@ export function main() {
|
||||
class SomeModule {
|
||||
}
|
||||
|
||||
xhr.spy('get').andCallFake(() => Promise.resolve('hello'));
|
||||
resourceLoader.spy('get').andCallFake(() => Promise.resolve('hello'));
|
||||
let ngModuleFactory: NgModuleFactory<any>;
|
||||
compiler.compileModuleAsync(SomeModule).then((f) => ngModuleFactory = f);
|
||||
tick();
|
||||
@ -141,7 +143,7 @@ export function main() {
|
||||
class SomeModule {
|
||||
}
|
||||
|
||||
xhr.spy('get').andCallFake(() => Promise.resolve(''));
|
||||
resourceLoader.spy('get').andCallFake(() => Promise.resolve(''));
|
||||
expect(() => compiler.compileModuleSync(SomeModule))
|
||||
.toThrowError(
|
||||
`Can't compile synchronously as ${stringify(SomeCompWithUrlTemplate)} is still being loaded!`);
|
||||
@ -153,7 +155,7 @@ export function main() {
|
||||
class SomeModule {
|
||||
}
|
||||
|
||||
xhr.spy('get').andCallFake(() => Promise.resolve(''));
|
||||
resourceLoader.spy('get').andCallFake(() => Promise.resolve(''));
|
||||
dirResolver.setView(SomeComp, new ViewMetadata({template: '', directives: [ChildComp]}));
|
||||
dirResolver.setView(ChildComp, new ViewMetadata({templateUrl: '/someTpl.html'}));
|
||||
expect(() => compiler.compileModuleSync(SomeModule))
|
||||
@ -170,7 +172,7 @@ export function main() {
|
||||
class SomeModule {
|
||||
}
|
||||
|
||||
xhr.spy('get').andCallFake(() => Promise.resolve('hello'));
|
||||
resourceLoader.spy('get').andCallFake(() => Promise.resolve('hello'));
|
||||
compiler.compileModuleAsync(SomeModule);
|
||||
tick();
|
||||
|
||||
|
@ -6,10 +6,10 @@
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {XHR} from '@angular/compiler/src/xhr';
|
||||
import {ResourceLoader} from '@angular/compiler/src/resource_loader';
|
||||
|
||||
import {SpyObject, proxy} from '@angular/core/testing/testing_internal';
|
||||
|
||||
export class SpyXHR extends SpyObject {
|
||||
constructor() { super(XHR); }
|
||||
export class SpyResourceLoader extends SpyObject {
|
||||
constructor() { super(ResourceLoader); }
|
||||
}
|
||||
|
@ -6,13 +6,13 @@
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import {ElementSchemaRegistry, UrlResolver, XHR} from '@angular/compiler';
|
||||
import {ElementSchemaRegistry, ResourceLoader, UrlResolver} from '@angular/compiler';
|
||||
import {createUrlResolverWithoutPackagePrefix} from '@angular/compiler/src/url_resolver';
|
||||
import {MockSchemaRegistry} from '@angular/compiler/testing';
|
||||
import {MockXHR} from '@angular/compiler/testing/xhr_mock';
|
||||
import {MockResourceLoader} from '@angular/compiler/testing/resource_loader_mock';
|
||||
|
||||
export var TEST_COMPILER_PROVIDERS: any[] = [
|
||||
{provide: ElementSchemaRegistry, useValue: new MockSchemaRegistry({}, {})},
|
||||
{provide: XHR, useClass: MockXHR},
|
||||
{provide: ResourceLoader, useClass: MockResourceLoader},
|
||||
{provide: UrlResolver, useFactory: createUrlResolverWithoutPackagePrefix}
|
||||
];
|
||||
|
@ -8,16 +8,16 @@
|
||||
|
||||
import {BaseException} from '@angular/core';
|
||||
|
||||
import {XHR} from '../index';
|
||||
import {ResourceLoader} from '../index';
|
||||
import {ListWrapper, Map} from '../src/facade/collection';
|
||||
import {isBlank, normalizeBlank} from '../src/facade/lang';
|
||||
|
||||
|
||||
/**
|
||||
* A mock implementation of {@link XHR} that allows outgoing requests to be mocked
|
||||
* A mock implementation of {@link ResourceLoader} that allows outgoing requests to be mocked
|
||||
* and responded to within a single test, without going to the network.
|
||||
*/
|
||||
export class MockXHR extends XHR {
|
||||
export class MockResourceLoader extends ResourceLoader {
|
||||
private _expectations: _Expectation[] = [];
|
||||
private _definitions = new Map<string, string>();
|
||||
private _requests: _PendingRequest[] = [];
|
Reference in New Issue
Block a user