2016-04-28 20:50:03 -04:00
|
|
|
import {Map} from '../src/facade/collection';
|
|
|
|
import {Type, isPresent} from '../src/facade/lang';
|
|
|
|
import {DirectiveMetadata, ComponentMetadata, Injectable} from '@angular/core';
|
|
|
|
import {DirectiveResolver} from '../src/directive_resolver';
|
2015-09-08 13:14:57 -04:00
|
|
|
|
2015-12-03 18:49:09 -05:00
|
|
|
/**
|
|
|
|
* An implementation of {@link DirectiveResolver} that allows overriding
|
|
|
|
* various properties of directives.
|
|
|
|
*/
|
2015-12-03 12:06:42 -05:00
|
|
|
@Injectable()
|
2015-09-08 13:14:57 -04:00
|
|
|
export class MockDirectiveResolver extends DirectiveResolver {
|
2015-10-11 01:11:13 -04:00
|
|
|
private _providerOverrides = new Map<Type, any[]>();
|
|
|
|
private viewProviderOverrides = new Map<Type, any[]>();
|
2015-09-08 13:14:57 -04:00
|
|
|
|
|
|
|
resolve(type: Type): DirectiveMetadata {
|
|
|
|
var dm = super.resolve(type);
|
|
|
|
|
2015-10-11 01:11:13 -04:00
|
|
|
var providerOverrides = this._providerOverrides.get(type);
|
|
|
|
var viewProviderOverrides = this.viewProviderOverrides.get(type);
|
2015-09-08 13:14:57 -04:00
|
|
|
|
2015-10-11 01:11:13 -04:00
|
|
|
var providers = dm.providers;
|
|
|
|
if (isPresent(providerOverrides)) {
|
2016-04-28 20:50:03 -04:00
|
|
|
var originalViewProviders: any[] = isPresent(dm.providers) ? dm.providers : [];
|
2016-01-06 17:13:44 -05:00
|
|
|
providers = originalViewProviders.concat(providerOverrides);
|
2015-09-08 13:14:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (dm instanceof ComponentMetadata) {
|
2015-10-11 01:11:13 -04:00
|
|
|
var viewProviders = dm.viewProviders;
|
|
|
|
if (isPresent(viewProviderOverrides)) {
|
2016-04-28 20:50:03 -04:00
|
|
|
var originalViewProviders: any[] = isPresent(dm.viewProviders) ? dm.viewProviders : [];
|
2016-01-06 17:13:44 -05:00
|
|
|
viewProviders = originalViewProviders.concat(viewProviderOverrides);
|
2015-09-08 13:14:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return new ComponentMetadata({
|
|
|
|
selector: dm.selector,
|
2015-09-30 23:59:23 -04:00
|
|
|
inputs: dm.inputs,
|
|
|
|
outputs: dm.outputs,
|
2015-09-08 13:14:57 -04:00
|
|
|
host: dm.host,
|
|
|
|
exportAs: dm.exportAs,
|
2015-09-14 18:59:09 -04:00
|
|
|
moduleId: dm.moduleId,
|
2015-09-17 21:45:14 -04:00
|
|
|
queries: dm.queries,
|
2015-09-08 13:14:57 -04:00
|
|
|
changeDetection: dm.changeDetection,
|
2015-10-11 01:11:13 -04:00
|
|
|
providers: providers,
|
|
|
|
viewProviders: viewProviders
|
2015-09-08 13:14:57 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return new DirectiveMetadata({
|
|
|
|
selector: dm.selector,
|
2015-09-30 23:59:23 -04:00
|
|
|
inputs: dm.inputs,
|
|
|
|
outputs: dm.outputs,
|
2015-09-08 13:14:57 -04:00
|
|
|
host: dm.host,
|
2015-10-11 01:11:13 -04:00
|
|
|
providers: providers,
|
2015-09-08 13:14:57 -04:00
|
|
|
exportAs: dm.exportAs,
|
2015-09-17 21:45:14 -04:00
|
|
|
queries: dm.queries
|
2015-09-08 13:14:57 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-11-01 19:37:24 -05:00
|
|
|
setProvidersOverride(type: Type, providers: any[]): void {
|
|
|
|
this._providerOverrides.set(type, providers);
|
2015-10-11 01:11:13 -04:00
|
|
|
}
|
|
|
|
|
2015-11-01 19:37:24 -05:00
|
|
|
setViewProvidersOverride(type: Type, viewProviders: any[]): void {
|
|
|
|
this.viewProviderOverrides.set(type, viewProviders);
|
2015-09-08 13:14:57 -04:00
|
|
|
}
|
|
|
|
}
|