feat(TestComponentBuilder): add #overrideBindings and #overrideViewBindings

Closes #4052
This commit is contained in:
Igor Minar
2015-09-08 10:14:57 -07:00
parent 39a6f85e95
commit f91c087c46
4 changed files with 192 additions and 22 deletions

View File

@ -6,6 +6,7 @@ import {ListWrapper, MapWrapper} from 'angular2/src/core/facade/collection';
import {ViewMetadata} from '../core/metadata';
import {DirectiveResolver} from 'angular2/src/core/compiler/directive_resolver';
import {ViewResolver} from 'angular2/src/core/compiler/view_resolver';
import {AppView} from 'angular2/src/core/compiler/view';
import {internalView, ViewRef} from 'angular2/src/core/compiler/view_ref';
@ -47,16 +48,19 @@ var _nextRootElementId = 0;
*/
@Injectable()
export class TestComponentBuilder {
_injector: Injector;
_viewOverrides: Map<Type, ViewMetadata>;
_bindingsOverrides: Map<Type, any[]>;
_directiveOverrides: Map<Type, Map<Type, Type>>;
_templateOverrides: Map<Type, string>;
_viewBindingsOverrides: Map<Type, any[]>;
_viewOverrides: Map<Type, ViewMetadata>;
constructor(injector: Injector) {
this._injector = injector;
this._viewOverrides = new Map();
constructor(private _injector: Injector) {
this._bindingsOverrides = new Map();
this._directiveOverrides = new Map();
this._templateOverrides = new Map();
this._viewBindingsOverrides = new Map();
this._viewOverrides = new Map();
}
_clone(): TestComponentBuilder {
@ -116,12 +120,53 @@ export class TestComponentBuilder {
return clone;
}
/**
* Overrides one or more injectables configured via `bindings` metadata property of a directive or
* component.
* Very useful when certain bindings need to be mocked out.
*
* The bindings specified via this method are appended to the existing `bindings` causing the
* duplicated bindings to
* be overridden.
*
* @param {Type} component
* @param {any[]} bindings
*
* @return {TestComponentBuilder}
*/
overrideBindings(type: Type, bindings: any[]): TestComponentBuilder {
var clone = this._clone();
clone._bindingsOverrides.set(type, bindings);
return clone;
}
/**
* Overrides one or more injectables configured via `bindings` metadata property of a directive or
* component.
* Very useful when certain bindings need to be mocked out.
*
* The bindings specified via this method are appended to the existing `bindings` causing the
* duplicated bindings to
* be overridden.
*
* @param {Type} component
* @param {any[]} bindings
*
* @return {TestComponentBuilder}
*/
overrideViewBindings(type: Type, bindings: any[]): TestComponentBuilder {
var clone = this._clone();
clone._viewBindingsOverrides.set(type, bindings);
return clone;
}
/**
* Builds and returns a RootTestComponent.
*
* @return {Promise<RootTestComponent>}
*/
createAsync(rootComponentType: Type): Promise<RootTestComponent> {
var mockDirectiveResolver = this._injector.get(DirectiveResolver);
var mockViewResolver = this._injector.get(ViewResolver);
MapWrapper.forEach(this._viewOverrides,
(view, type) => { mockViewResolver.setView(type, view); });
@ -133,6 +178,11 @@ export class TestComponentBuilder {
});
});
this._bindingsOverrides.forEach((bindings, type) =>
mockDirectiveResolver.setBindingsOverride(type, bindings));
this._viewBindingsOverrides.forEach(
(bindings, type) => mockDirectiveResolver.setViewBindingsOverride(type, bindings));
var rootElId = `root${_nextRootElementId++}`;
var rootEl = el(`<div id="${rootElId}"></div>`);
var doc = this._injector.get(DOCUMENT);

View File

@ -36,6 +36,7 @@ import {
EVENT_MANAGER_PLUGINS
} from 'angular2/src/core/render/dom/events/event_manager';
import {MockDirectiveResolver} from 'angular2/src/mock/directive_resolver_mock';
import {MockViewResolver} from 'angular2/src/mock/view_resolver_mock';
import {MockXHR} from 'angular2/src/core/render/xhr_mock';
import {MockLocationStrategy} from 'angular2/src/mock/mock_location_strategy';
@ -125,6 +126,7 @@ function _getAppBindings() {
bind(APP_VIEW_POOL_CAPACITY).toValue(500),
Compiler,
CompilerCache,
bind(DirectiveResolver).toClass(MockDirectiveResolver),
bind(ViewResolver).toClass(MockViewResolver),
DEFAULT_PIPES,
bind(IterableDiffers).toValue(defaultIterableDiffers),
@ -133,7 +135,6 @@ function _getAppBindings() {
Log,
ViewLoader,
DynamicComponentLoader,
DirectiveResolver,
PipeResolver,
Parser,
Lexer,