1- import {
2- APP_ID ,
3- APPLICATION_COMMON_PROVIDERS ,
4- AppViewManager ,
5- DirectiveResolver ,
6- DynamicComponentLoader ,
7- Injector ,
8- NgZone ,
9- Renderer ,
10- Provider ,
11- ViewResolver ,
12- provide
13- } from 'angular2/core' ;
14- import { AnimationBuilder } from 'angular2/src/animate/animation_builder' ;
15- import { MockAnimationBuilder } from 'angular2/src/mock/animation_builder_mock' ;
16-
17- import { ResolvedMetadataCache } from 'angular2/src/core/linker/resolved_metadata_cache' ;
18- import { Reflector , reflector } from 'angular2/src/core/reflection/reflection' ;
19- import {
20- IterableDiffers ,
21- defaultIterableDiffers ,
22- KeyValueDiffers ,
23- defaultKeyValueDiffers ,
24- ChangeDetectorGenConfig
25- } from 'angular2/src/core/change_detection/change_detection' ;
1+ import { Injector , Provider , PLATFORM_INITIALIZER } from 'angular2/core' ;
262import { BaseException , ExceptionHandler } from 'angular2/src/facade/exceptions' ;
27- import { PipeResolver } from 'angular2/src/core/linker/pipe_resolver' ;
28- import { XHR } from 'angular2/src/compiler/xhr' ;
29-
30- import { DOM } from 'angular2/src/platform/dom/dom_adapter' ;
31-
32- import { MockDirectiveResolver } from 'angular2/src/mock/directive_resolver_mock' ;
33- import { MockViewResolver } from 'angular2/src/mock/view_resolver_mock' ;
34- import { MockLocationStrategy } from 'angular2/src/mock/mock_location_strategy' ;
35- import { LocationStrategy } from 'angular2/src/router/location_strategy' ;
36- import { MockNgZone } from 'angular2/src/mock/ng_zone_mock' ;
37-
38- import { TestComponentBuilder } from './test_component_builder' ;
39-
40- import {
41- EventManager ,
42- EVENT_MANAGER_PLUGINS ,
43- ELEMENT_PROBE_PROVIDERS
44- } from 'angular2/platform/common_dom' ;
45-
463import { ListWrapper } from 'angular2/src/facade/collection' ;
47- import { FunctionWrapper , Type } from 'angular2/src/facade/lang' ;
48-
49- import { RootRenderer } from 'angular2/src/core/render/api' ;
50-
51- import { DOCUMENT } from 'angular2/src/platform/dom/dom_tokens' ;
52- import { DomRootRenderer , DomRootRenderer_ } from 'angular2/src/platform/dom/dom_renderer' ;
53- import { DomSharedStylesHost } from 'angular2/src/platform/dom/shared_styles_host' ;
54- import { SharedStylesHost } from 'angular2/src/platform/dom/shared_styles_host' ;
55- import { DomEventsPlugin } from 'angular2/src/platform/dom/events/dom_events' ;
56-
57- import { Serializer } from "angular2/src/web_workers/shared/serializer" ;
58- import { Log } from './utils' ;
59- import { COMPILER_PROVIDERS } from 'angular2/src/compiler/compiler' ;
60- import { DynamicComponentLoader_ } from "angular2/src/core/linker/dynamic_component_loader" ;
61- import { AppViewManager_ } from "angular2/src/core/linker/view_manager" ;
62-
63- /**
64- * Returns the root injector providers.
65- *
66- * This must be kept in sync with the _rootBindings in application.js
67- *
68- * @returns {any[] }
69- */
70- function _getRootProviders ( ) {
71- return [ provide ( Reflector , { useValue : reflector } ) ] ;
72- }
73-
74- /**
75- * Returns the application injector providers.
76- *
77- * This must be kept in sync with _injectorBindings() in application.js
78- *
79- * @returns {any[] }
80- */
81- function _getAppBindings ( ) {
82- var appDoc ;
83-
84- // The document is only available in browser environment
85- try {
86- appDoc = DOM . defaultDoc ( ) ;
87- } catch ( e ) {
88- appDoc = null ;
89- }
90-
91- return [
92- APPLICATION_COMMON_PROVIDERS ,
93- provide ( ChangeDetectorGenConfig , { useValue : new ChangeDetectorGenConfig ( true , false , false ) } ) ,
94- provide ( DOCUMENT , { useValue : appDoc } ) ,
95- provide ( DomRootRenderer , { useClass : DomRootRenderer_ } ) ,
96- provide ( RootRenderer , { useExisting : DomRootRenderer } ) ,
97- provide ( APP_ID , { useValue : 'a' } ) ,
98- DomSharedStylesHost ,
99- provide ( SharedStylesHost , { useExisting : DomSharedStylesHost } ) ,
100- provide ( AppViewManager , { useClass : AppViewManager_ } ) ,
101- Serializer ,
102- ELEMENT_PROBE_PROVIDERS ,
103- ResolvedMetadataCache ,
104- provide ( DirectiveResolver , { useClass : MockDirectiveResolver } ) ,
105- provide ( ViewResolver , { useClass : MockViewResolver } ) ,
106- provide ( IterableDiffers , { useValue : defaultIterableDiffers } ) ,
107- provide ( KeyValueDiffers , { useValue : defaultKeyValueDiffers } ) ,
108- Log ,
109- provide ( DynamicComponentLoader , { useClass : DynamicComponentLoader_ } ) ,
110- PipeResolver ,
111- provide ( ExceptionHandler , { useValue : new ExceptionHandler ( DOM ) } ) ,
112- provide ( LocationStrategy , { useClass : MockLocationStrategy } ) ,
113- provide ( XHR , { useClass : DOM . getXHR ( ) } ) ,
114- TestComponentBuilder ,
115- provide ( NgZone , { useClass : MockNgZone } ) ,
116- provide ( AnimationBuilder , { useClass : MockAnimationBuilder } ) ,
117- EventManager ,
118- new Provider ( EVENT_MANAGER_PLUGINS , { useClass : DomEventsPlugin , multi : true } )
119- ] ;
120- }
121-
122- function _runtimeCompilerBindings ( ) {
123- return [
124- provide ( XHR , { useClass : DOM . getXHR ( ) } ) ,
125- COMPILER_PROVIDERS ,
126- ] ;
127- }
4+ import { FunctionWrapper , isPresent , Type } from 'angular2/src/facade/lang' ;
1285
1296export class TestInjector {
1307 private _instantiated : boolean = false ;
@@ -139,6 +16,10 @@ export class TestInjector {
13916 this . _instantiated = false ;
14017 }
14118
19+ platformProviders : Array < Type | Provider | any [ ] > = [ ] ;
20+
21+ applicationProviders : Array < Type | Provider | any [ ] > = [ ] ;
22+
14223 addProviders ( providers : Array < Type | Provider | any [ ] > ) {
14324 if ( this . _instantiated ) {
14425 throw new BaseException ( 'Cannot add providers after test injector is instantiated' ) ;
@@ -147,9 +28,9 @@ export class TestInjector {
14728 }
14829
14930 createInjector ( ) {
150- var rootInjector = Injector . resolveAndCreate ( _getRootProviders ( ) ) ;
151- this . _injector = rootInjector . resolveAndCreateChild ( ListWrapper . concat (
152- ListWrapper . concat ( _getAppBindings ( ) , _runtimeCompilerBindings ( ) ) , this . _providers ) ) ;
31+ var rootInjector = Injector . resolveAndCreate ( this . platformProviders ) ;
32+ this . _injector = rootInjector . resolveAndCreateChild (
33+ ListWrapper . concat ( this . applicationProviders , this . _providers ) ) ;
15334 this . _instantiated = true ;
15435 return this . _injector ;
15536 }
@@ -172,19 +53,40 @@ export function getTestInjector() {
17253}
17354
17455/**
175- * @deprecated Use TestInjector#createInjector() instead.
56+ * Set the providers that the test injector should use. These should be providers
57+ * common to every test in the suite.
58+ *
59+ * This may only be called once, to set up the common providers for the current test
60+ * suite on teh current platform. If you absolutely need to change the providers,
61+ * first use `resetBaseTestProviders`.
62+ *
63+ * Test Providers for individual platforms are available from
64+ * 'angular2/platform/testing/<platform_name>'.
17665 */
177- export function createTestInjector ( providers : Array < Type | Provider | any [ ] > ) : Injector {
178- var rootInjector = Injector . resolveAndCreate ( _getRootProviders ( ) ) ;
179- return rootInjector . resolveAndCreateChild ( ListWrapper . concat ( _getAppBindings ( ) , providers ) ) ;
66+ export function setBaseTestProviders ( platformProviders : Array < Type | Provider | any [ ] > ,
67+ applicationProviders : Array < Type | Provider | any [ ] > ) {
68+ var testInjector = getTestInjector ( ) ;
69+ if ( testInjector . platformProviders . length > 0 || testInjector . applicationProviders . length > 0 ) {
70+ throw new BaseException ( 'Cannot set base providers because it has already been called' ) ;
71+ }
72+ testInjector . platformProviders = platformProviders ;
73+ testInjector . applicationProviders = applicationProviders ;
74+ var injector = testInjector . createInjector ( ) ;
75+ let inits : Function [ ] = injector . getOptional ( PLATFORM_INITIALIZER ) ;
76+ if ( isPresent ( inits ) ) {
77+ inits . forEach ( init => init ( ) ) ;
78+ }
79+ testInjector . reset ( ) ;
18080}
18181
18282/**
183- * @deprecated Use TestInjector#createInjector() instead .
83+ * Reset the providers for the test injector .
18484 */
185- export function createTestInjectorWithRuntimeCompiler (
186- providers : Array < Type | Provider | any [ ] > ) : Injector {
187- return createTestInjector ( ListWrapper . concat ( _runtimeCompilerBindings ( ) , providers ) ) ;
85+ export function resetBaseTestProviders ( ) {
86+ var testInjector = getTestInjector ( ) ;
87+ testInjector . platformProviders = [ ] ;
88+ testInjector . applicationProviders = [ ] ;
89+ testInjector . reset ( ) ;
18890}
18991
19092/**
0 commit comments