@@ -8,14 +8,17 @@ import {
88 ViewMetadata ,
99 EmbeddedViewRef ,
1010 ViewResolver ,
11- provide
11+ provide ,
12+ Provider
1213} from 'angular2/core' ;
1314
14- import { Type , isPresent , isBlank } from 'angular2/src/facade/lang' ;
15- import { Promise } from 'angular2/src/facade/async' ;
15+ import { Type , isPresent , isBlank , CONST_EXPR } from 'angular2/src/facade/lang' ;
16+ import { Promise , PromiseWrapper } from 'angular2/src/facade/async' ;
1617import { ListWrapper , MapWrapper } from 'angular2/src/facade/collection' ;
18+ import { Compiler , Compiler_ } from 'angular2/src/core/linker/compiler' ;
19+ import { ViewFactoryProxy } from 'angular2/src/core/linker/view_listener' ;
1720
18- import { ViewRef_ } from 'angular2/src/core/linker/view_ref' ;
21+ import { ViewRef_ , HostViewFactoryRef_ } from 'angular2/src/core/linker/view_ref' ;
1922import { AppView } from 'angular2/src/core/linker/view' ;
2023
2124import { el } from './utils' ;
@@ -25,7 +28,6 @@ import {DOM} from 'angular2/src/platform/dom/dom_adapter';
2528
2629import { DebugElement_ } from 'angular2/src/core/debug/debug_element' ;
2730
28-
2931/**
3032 * Fixture for debugging and testing a component.
3133 */
@@ -82,6 +84,20 @@ export class ComponentFixture_ extends ComponentFixture {
8284
8385var _nextRootElementId = 0 ;
8486
87+ @Injectable ( )
88+ export class TestViewFactoryProxy implements ViewFactoryProxy {
89+ private _componentFactoryOverrides : Map < Type , Function > = new Map < Type , Function > ( ) ;
90+
91+ getComponentViewFactory ( component : Type , originalViewFactory : Function ) : Function {
92+ var override = this . _componentFactoryOverrides . get ( component ) ;
93+ return isPresent ( override ) ? override : originalViewFactory ;
94+ }
95+
96+ setComponentViewFactory ( component : Type , viewFactory : Function ) {
97+ this . _componentFactoryOverrides . set ( component , viewFactory ) ;
98+ }
99+ }
100+
85101/**
86102 * Builds a ComponentFixture for use in component level tests.
87103 */
@@ -97,7 +113,8 @@ export class TestComponentBuilder {
97113 _viewBindingsOverrides = new Map < Type , any [ ] > ( ) ;
98114 /** @internal */
99115 _viewOverrides = new Map < Type , ViewMetadata > ( ) ;
100-
116+ /** @internal */
117+ _componentOverrides = new Map < Type , Type > ( ) ;
101118
102119 constructor ( private _injector : Injector ) { }
103120
@@ -107,6 +124,23 @@ export class TestComponentBuilder {
107124 clone . _viewOverrides = MapWrapper . clone ( this . _viewOverrides ) ;
108125 clone . _directiveOverrides = MapWrapper . clone ( this . _directiveOverrides ) ;
109126 clone . _templateOverrides = MapWrapper . clone ( this . _templateOverrides ) ;
127+ clone . _componentOverrides = MapWrapper . clone ( this . _componentOverrides ) ;
128+ return clone ;
129+ }
130+
131+ /**
132+ * Overrides a component with another component.
133+ * This also works with precompiled templates if they were generated
134+ * in development mode.
135+ *
136+ * @param {Type } original component
137+ * @param {Type } mock component
138+ *
139+ * @return {TestComponentBuilder }
140+ */
141+ overrideComponent ( componentType : Type , mockType : Type ) : TestComponentBuilder {
142+ var clone = this . _clone ( ) ;
143+ clone . _componentOverrides . set ( componentType , mockType ) ;
110144 return clone ;
111145 }
112146
@@ -246,10 +280,31 @@ export class TestComponentBuilder {
246280 DOM . remove ( oldRoots [ i ] ) ;
247281 }
248282 DOM . appendChild ( doc . body , rootEl ) ;
249-
250-
251- return this . _injector . get ( DynamicComponentLoader )
252- . loadAsRoot ( rootComponentType , `#${ rootElId } ` , this . _injector )
253- . then ( ( componentRef ) => { return new ComponentFixture_ ( componentRef ) ; } ) ;
283+ var originalCompTypes = [ ] ;
284+ var mockHostViewFactoryPromises = [ ] ;
285+ var compiler : Compiler_ = this . _injector . get ( Compiler ) ;
286+ var viewFactoryProxy : TestViewFactoryProxy = this . _injector . get ( TestViewFactoryProxy ) ;
287+ this . _componentOverrides . forEach ( ( mockCompType , originalCompType ) => {
288+ originalCompTypes . push ( originalCompType ) ;
289+ mockHostViewFactoryPromises . push ( compiler . compileInHost ( mockCompType ) ) ;
290+ } ) ;
291+ return PromiseWrapper . all ( mockHostViewFactoryPromises )
292+ . then ( ( mockHostViewFactories : HostViewFactoryRef_ [ ] ) => {
293+ for ( var i = 0 ; i < mockHostViewFactories . length ; i ++ ) {
294+ var originalCompType = originalCompTypes [ i ] ;
295+ viewFactoryProxy . setComponentViewFactory (
296+ originalCompType ,
297+ mockHostViewFactories [ i ] . internalHostViewFactory . componentViewFactory ) ;
298+ }
299+ return this . _injector . get ( DynamicComponentLoader )
300+ . loadAsRoot ( rootComponentType , `#${ rootElId } ` , this . _injector )
301+ . then ( ( componentRef ) => { return new ComponentFixture_ ( componentRef ) ; } ) ;
302+ } ) ;
254303 }
255304}
305+
306+ export const TEST_COMPONENT_BUILDER_PROVIDERS = CONST_EXPR ( [
307+ TestViewFactoryProxy ,
308+ CONST_EXPR ( new Provider ( ViewFactoryProxy , { useExisting : TestViewFactoryProxy } ) ) ,
309+ TestComponentBuilder
310+ ] ) ;
0 commit comments