@@ -11,13 +11,14 @@ import {
1111 AsyncTestCompleter ,
1212 fakeAsync ,
1313 tick ,
14- inject
14+ inject ,
15+ SpyObject
1516} from 'angular2/testing_internal' ;
1617import { SpyChangeDetector } from './spies' ;
17- import { ApplicationRef_ , PlatformRef_ } from "angular2/src/core/application_ref" ;
18- import { Injector , Provider } from "angular2/core" ;
18+ import { ApplicationRef_ , ApplicationRef , PlatformRef_ } from "angular2/src/core/application_ref" ;
19+ import { Injector , Provider , APP_INITIALIZER } from "angular2/core" ;
1920import { ChangeDetectorRef_ } from "angular2/src/core/change_detection/change_detector_ref" ;
20- import { PromiseWrapper } from "angular2/src/facade/async" ;
21+ import { PromiseWrapper , PromiseCompleter , TimerWrapper } from "angular2/src/facade/async" ;
2122import { ListWrapper } from "angular2/src/facade/collection" ;
2223
2324export function main ( ) {
@@ -33,22 +34,114 @@ export function main() {
3334
3435 describe ( "PlatformRef" , ( ) => {
3536 describe ( "asyncApplication" , ( ) => {
36- it ( "should merge synchronous and asynchronous providers" ,
37+ function expectProviders ( injector : Injector , providers : Array < any > ) : void {
38+ for ( let i = 0 ; i < providers . length ; i ++ ) {
39+ let provider = providers [ i ] ;
40+ expect ( injector . get ( provider . token ) ) . toBe ( provider . useValue ) ;
41+ }
42+ }
43+
44+ it ( "should merge syncronous and asyncronous providers" ,
3745 inject ( [ AsyncTestCompleter , Injector ] , ( async , injector ) => {
3846 let ref = new PlatformRef_ ( injector , null ) ;
3947 let ASYNC_PROVIDERS = [ new Provider ( Foo , { useValue : new Foo ( ) } ) ] ;
4048 let SYNC_PROVIDERS = [ new Provider ( Bar , { useValue : new Bar ( ) } ) ] ;
4149 ref . asyncApplication ( ( zone ) => PromiseWrapper . resolve ( ASYNC_PROVIDERS ) , SYNC_PROVIDERS )
4250 . then ( ( appRef ) => {
4351 var providers = ListWrapper . concat ( ASYNC_PROVIDERS , SYNC_PROVIDERS ) ;
44- for ( var i = 0 ; i < providers . length ; i ++ ) {
45- var provider = providers [ i ] ;
46- expect ( appRef . injector . get ( provider . token ) ) . toBe ( provider . useValue ) ;
47- }
52+ expectProviders ( appRef . injector , providers ) ;
53+ async . done ( ) ;
54+ } ) ;
55+ } ) ) ;
56+
57+ it ( "should allow function to be null" ,
58+ inject ( [ AsyncTestCompleter , Injector ] , ( async , injector ) => {
59+ let ref = new PlatformRef_ ( injector , null ) ;
60+ let SYNC_PROVIDERS = [ new Provider ( Bar , { useValue : new Bar ( ) } ) ] ;
61+ ref . asyncApplication ( null , SYNC_PROVIDERS )
62+ . then ( ( appRef ) => {
63+ expectProviders ( appRef . injector , SYNC_PROVIDERS ) ;
64+ async . done ( ) ;
65+ } ) ;
66+ } ) ) ;
67+
68+ function mockAsyncAppInitializer ( completer , providers : Array < any > = null ,
69+ injector ?: Injector ) {
70+ return ( ) => {
71+ if ( providers != null ) {
72+ expectProviders ( injector , providers ) ;
73+ }
74+ TimerWrapper . setTimeout ( ( ) => completer . resolve ( true ) , 1 ) ;
75+ return completer . promise ;
76+ } ;
77+ }
78+
79+ function createSpyPromiseCompleter ( ) : SpyObject {
80+ let completer = PromiseWrapper . completer ( ) ;
81+ let completerSpy = < any > new SpyObject ( ) ;
82+ // Note that in TypeScript we need to provide a value for the promise attribute
83+ // whereas in dart we need to override the promise getter
84+ completerSpy . promise = completer . promise ;
85+ completerSpy . spy ( "get:promise" ) . andReturn ( completer . promise ) ;
86+ completerSpy . spy ( "resolve" ) . andCallFake ( completer . resolve ) ;
87+ completerSpy . spy ( "reject" ) . andCallFake ( completer . reject ) ;
88+ return completerSpy ;
89+ }
90+
91+ it ( "should wait for asyncronous app initializers" ,
92+ inject ( [ AsyncTestCompleter , Injector ] , ( async , injector ) => {
93+ let ref = new PlatformRef_ ( injector , null ) ;
94+
95+ let completer = createSpyPromiseCompleter ( ) ;
96+ let SYNC_PROVIDERS = [
97+ new Provider ( Bar , { useValue : new Bar ( ) } ) ,
98+ new Provider ( APP_INITIALIZER ,
99+ { useValue : mockAsyncAppInitializer ( completer ) , multi : true } )
100+ ] ;
101+ ref . asyncApplication ( null , SYNC_PROVIDERS )
102+ . then ( ( appRef ) => {
103+ expectProviders ( appRef . injector ,
104+ SYNC_PROVIDERS . slice ( 0 , SYNC_PROVIDERS . length - 1 ) ) ;
105+ expect ( completer . spy ( "resolve" ) ) . toHaveBeenCalled ( ) ;
106+ async . done ( ) ;
107+ } ) ;
108+ } ) ) ;
109+
110+ it ( "should wait for async providers and then async app initializers" ,
111+ inject ( [ AsyncTestCompleter , Injector ] , ( async , injector ) => {
112+ let ref = new PlatformRef_ ( injector , null ) ;
113+ let ASYNC_PROVIDERS = [ new Provider ( Foo , { useValue : new Foo ( ) } ) ] ;
114+ let completer = createSpyPromiseCompleter ( ) ;
115+ let SYNC_PROVIDERS = [
116+ new Provider ( Bar , { useValue : new Bar ( ) } ) ,
117+ new Provider ( APP_INITIALIZER ,
118+ {
119+ useFactory : ( injector ) => mockAsyncAppInitializer (
120+ completer , ASYNC_PROVIDERS , injector ) ,
121+ multi : true ,
122+ deps : [ Injector ]
123+ } )
124+ ] ;
125+ ref . asyncApplication ( ( zone ) => PromiseWrapper . resolve ( ASYNC_PROVIDERS ) , SYNC_PROVIDERS )
126+ . then ( ( appRef ) => {
127+ expectProviders ( appRef . injector ,
128+ SYNC_PROVIDERS . slice ( 0 , SYNC_PROVIDERS . length - 1 ) ) ;
129+ expect ( completer . spy ( "resolve" ) ) . toHaveBeenCalled ( ) ;
48130 async . done ( ) ;
49131 } ) ;
50132 } ) ) ;
51133 } ) ;
134+
135+ describe ( "application" , ( ) => {
136+ it ( "should throw if an APP_INITIIALIZER returns a promise" , inject ( [ Injector ] , ( injector ) => {
137+ let ref = new PlatformRef_ ( injector , null ) ;
138+ let appInitializer = new Provider (
139+ APP_INITIALIZER , { useValue : ( ) => PromiseWrapper . resolve ( [ ] ) , multi : true } ) ;
140+ expect ( ( ) => ref . application ( [ appInitializer ] ) )
141+ . toThrowError (
142+ "Cannot use asyncronous app initializers with application. Use asyncApplication instead." ) ;
143+ } ) ) ;
144+ } ) ;
52145 } ) ;
53146}
54147
0 commit comments