feat(compiler): introduce `MockDirectiveResolver.setDirective`

This commit is contained in:
Tobias Bosch 2016-07-28 06:45:22 -07:00
parent 3dbc66c1ac
commit acc6c8d0b7
2 changed files with 53 additions and 26 deletions

View File

@ -20,6 +20,21 @@ export function main() {
dirResolver = new MockDirectiveResolver(injector); dirResolver = new MockDirectiveResolver(injector);
})); }));
describe('Directive overriding', () => {
it('should fallback to the default DirectiveResolver when templates are not overridden',
() => {
var ngModule = dirResolver.resolve(SomeComponent);
expect(ngModule.selector).toEqual('cmp');
});
it('should allow overriding the @NgModule', () => {
dirResolver.setDirective(
SomeComponent, new ComponentMetadata({selector: 'someOtherSelector'}));
var metadata = dirResolver.resolve(SomeComponent);
expect(metadata.selector).toEqual('someOtherSelector');
});
});
describe('View overriding', () => { describe('View overriding', () => {
it('should fallback to the default ViewResolver when templates are not overridden', () => { it('should fallback to the default ViewResolver when templates are not overridden', () => {
var view = <ComponentMetadata>dirResolver.resolve(SomeComponent); var view = <ComponentMetadata>dirResolver.resolve(SomeComponent);

View File

@ -20,8 +20,9 @@ import {Type, isArray, isPresent, stringify} from '../src/facade/lang';
*/ */
@Injectable() @Injectable()
export class MockDirectiveResolver extends DirectiveResolver { export class MockDirectiveResolver extends DirectiveResolver {
private _directives = new Map<Type, DirectiveMetadata>();
private _providerOverrides = new Map<Type, any[]>(); private _providerOverrides = new Map<Type, any[]>();
private viewProviderOverrides = new Map<Type, any[]>(); private _viewProviderOverrides = new Map<Type, any[]>();
private _views = new Map<Type, ViewMetadata>(); private _views = new Map<Type, ViewMetadata>();
private _inlineTemplates = new Map<Type, string>(); private _inlineTemplates = new Map<Type, string>();
private _animations = new Map<Type, AnimationEntryMetadata[]>(); private _animations = new Map<Type, AnimationEntryMetadata[]>();
@ -34,30 +35,33 @@ export class MockDirectiveResolver extends DirectiveResolver {
private _clearCacheFor(component: Type) { this._compiler.clearCacheFor(component); } private _clearCacheFor(component: Type) { this._compiler.clearCacheFor(component); }
resolve(type: Type, throwIfNotFound = true): DirectiveMetadata { resolve(type: Type, throwIfNotFound = true): DirectiveMetadata {
const dm = super.resolve(type, throwIfNotFound); let metadata = this._directives.get(type);
if (!dm) { if (!metadata) {
metadata = super.resolve(type, throwIfNotFound);
}
if (!metadata) {
return null; return null;
} }
const providerOverrides = this._providerOverrides.get(type); const providerOverrides = this._providerOverrides.get(type);
const viewProviderOverrides = this.viewProviderOverrides.get(type); const viewProviderOverrides = this._viewProviderOverrides.get(type);
let providers = dm.providers; let providers = metadata.providers;
if (isPresent(providerOverrides)) { if (isPresent(providerOverrides)) {
const originalViewProviders: any[] = isPresent(dm.providers) ? dm.providers : []; const originalViewProviders: any[] = isPresent(metadata.providers) ? metadata.providers : [];
providers = originalViewProviders.concat(providerOverrides); providers = originalViewProviders.concat(providerOverrides);
} }
if (dm instanceof ComponentMetadata) { if (metadata instanceof ComponentMetadata) {
let viewProviders = dm.viewProviders; let viewProviders = metadata.viewProviders;
if (isPresent(viewProviderOverrides)) { if (isPresent(viewProviderOverrides)) {
const originalViewProviders: any[] = isPresent(dm.viewProviders) ? dm.viewProviders : []; const originalViewProviders: any[] = isPresent(metadata.viewProviders) ? metadata.viewProviders : [];
viewProviders = originalViewProviders.concat(viewProviderOverrides); viewProviders = originalViewProviders.concat(viewProviderOverrides);
} }
let view = this._views.get(type); let view = this._views.get(type);
if (!view) { if (!view) {
view = dm; view = metadata;
} }
const directives: any[] = []; const directives: any[] = [];
@ -92,17 +96,17 @@ export class MockDirectiveResolver extends DirectiveResolver {
} }
return new ComponentMetadata({ return new ComponentMetadata({
selector: dm.selector, selector: metadata.selector,
inputs: dm.inputs, inputs: metadata.inputs,
outputs: dm.outputs, outputs: metadata.outputs,
host: dm.host, host: metadata.host,
exportAs: dm.exportAs, exportAs: metadata.exportAs,
moduleId: dm.moduleId, moduleId: metadata.moduleId,
queries: dm.queries, queries: metadata.queries,
changeDetection: dm.changeDetection, changeDetection: metadata.changeDetection,
providers: providers, providers: providers,
viewProviders: viewProviders, viewProviders: viewProviders,
entryComponents: dm.entryComponents, entryComponents: metadata.entryComponents,
template: inlineTemplate, template: inlineTemplate,
templateUrl: templateUrl, templateUrl: templateUrl,
directives: directives.length > 0 ? directives : null, directives: directives.length > 0 ? directives : null,
@ -116,23 +120,31 @@ export class MockDirectiveResolver extends DirectiveResolver {
} }
return new DirectiveMetadata({ return new DirectiveMetadata({
selector: dm.selector, selector: metadata.selector,
inputs: dm.inputs, inputs: metadata.inputs,
outputs: dm.outputs, outputs: metadata.outputs,
host: dm.host, host: metadata.host,
providers: providers, providers: providers,
exportAs: dm.exportAs, exportAs: metadata.exportAs,
queries: dm.queries queries: metadata.queries
}); });
} }
/**
* Overrides the {@link DirectiveMetadata} for a directive.
*/
setDirective(type: Type, metadata: DirectiveMetadata): void {
this._directives.set(type, metadata);
this._clearCacheFor(type);
}
setProvidersOverride(type: Type, providers: any[]): void { setProvidersOverride(type: Type, providers: any[]): void {
this._providerOverrides.set(type, providers); this._providerOverrides.set(type, providers);
this._clearCacheFor(type); this._clearCacheFor(type);
} }
setViewProvidersOverride(type: Type, viewProviders: any[]): void { setViewProvidersOverride(type: Type, viewProviders: any[]): void {
this.viewProviderOverrides.set(type, viewProviders); this._viewProviderOverrides.set(type, viewProviders);
this._clearCacheFor(type); this._clearCacheFor(type);
} }