@@ -18,7 +18,7 @@ const app1 = {
18
18
routerPathPrefix : '/app1' ,
19
19
hostClass : 'app1-host' ,
20
20
preload : false ,
21
- switchMode : SwitchModes . coexist ,
21
+ switchMode : SwitchModes . default ,
22
22
resourcePathPrefix : '/static/app1' ,
23
23
styles : [ 'styles/main.css' ] ,
24
24
scripts : [ 'vendor.js' , 'main.js' ] ,
@@ -35,7 +35,7 @@ const app2 = {
35
35
selector : 'app2-root-container' ,
36
36
routerPathPrefix : '/app2' ,
37
37
hostClass : 'app2-host' ,
38
- preload : false ,
38
+ preload : true ,
39
39
switchMode : SwitchModes . coexist ,
40
40
resourcePathPrefix : '/static/app2' ,
41
41
styles : [ 'styles/main.css' ] ,
@@ -69,6 +69,11 @@ describe('PlanetApplicationLoader', () => {
69
69
70
70
planetApplicationService . register ( app1 ) ;
71
71
planetApplicationService . register ( app2 ) ;
72
+
73
+ // 创建宿主容器
74
+ const hostContainer = document . createElement ( 'DIV' ) ;
75
+ hostContainer . classList . add ( 'host-selector' ) ;
76
+ document . body . appendChild ( hostContainer ) ;
72
77
} ) ;
73
78
74
79
afterEach ( ( ) => {
@@ -80,11 +85,6 @@ describe('PlanetApplicationLoader', () => {
80
85
const assetsLoaderSpy = spyOn ( assetsLoader , 'loadAppAssets' ) ;
81
86
assetsLoaderSpy . and . returnValue ( loadAppAssets$ ) ;
82
87
83
- // 创建宿主容器
84
- const hostContainer = document . createElement ( 'DIV' ) ;
85
- hostContainer . classList . add ( 'host-selector' ) ;
86
- document . body . appendChild ( hostContainer ) ;
87
-
88
88
const planetAppRef = mockApplicationRef ( app1 . name ) ;
89
89
const bootstrapSpy = spyOn ( planetAppRef , 'bootstrap' ) ;
90
90
@@ -108,6 +108,7 @@ describe('PlanetApplicationLoader', () => {
108
108
expect ( bootstrapSpy ) . toHaveBeenCalled ( ) ;
109
109
110
110
expect ( appStatusChangeSpy ) . toHaveBeenCalledTimes ( 4 ) ;
111
+ expect ( appStatusChangeSpy ) . toHaveBeenCalledWith ( { app : app1 , status : ApplicationStatus . bootstrapping } ) ;
111
112
expect ( appStatusChangeSpy ) . toHaveBeenCalledWith ( { app : app1 , status : ApplicationStatus . bootstrapped } ) ;
112
113
113
114
// 判断是否在宿主元素中创建了应用根节点
@@ -117,7 +118,7 @@ describe('PlanetApplicationLoader', () => {
117
118
tick ( ) ;
118
119
} ) ) ;
119
120
120
- it ( `should cancel load app1 which has not loaded when next route (app2) change` , fakeAsync ( ( ) => {
121
+ it ( `should cancel load app1 which assets has not loaded when next route (app2) change` , fakeAsync ( ( ) => {
121
122
const loadApp1Assets$ = new Subject ( ) ;
122
123
const loadApp2Assets$ = new Subject ( ) ;
123
124
@@ -153,6 +154,7 @@ describe('PlanetApplicationLoader', () => {
153
154
ngZone . onStable . next ( ) ;
154
155
155
156
expect ( appStatusChangeSpy ) . toHaveBeenCalledTimes ( 5 ) ;
157
+ expect ( appStatusChangeSpy ) . toHaveBeenCalledWith ( { app : app2 , status : ApplicationStatus . bootstrapping } ) ;
156
158
expect ( appStatusChangeSpy ) . toHaveBeenCalledWith ( { app : app2 , status : ApplicationStatus . bootstrapped } ) ;
157
159
158
160
expect ( app1BootstrapSpy ) . not . toHaveBeenCalled ( ) ;
@@ -161,5 +163,113 @@ describe('PlanetApplicationLoader', () => {
161
163
tick ( ) ;
162
164
} ) ) ;
163
165
164
- it ( `should preload apps` , ( ) => { } ) ;
166
+ it ( `should cancel load app1 which has not bootstrapped when next route (app2) change` , fakeAsync ( ( ) => {
167
+ const loadApp1Assets$ = new Subject ( ) ;
168
+ const loadApp2Assets$ = new Subject ( ) ;
169
+
170
+ const planetApp1Ref = mockApplicationRef ( app1 . name ) ;
171
+ const app1BootstrapSpy = spyOn ( planetApp1Ref , 'bootstrap' ) ;
172
+
173
+ const planetApp2Ref = mockApplicationRef ( app2 . name ) ;
174
+ const app2BootstrapSpy = spyOn ( planetApp2Ref , 'bootstrap' ) ;
175
+
176
+ const assetsLoaderSpy = spyOn ( assetsLoader , 'loadAppAssets' ) ;
177
+ assetsLoaderSpy . and . returnValues ( loadApp1Assets$ , loadApp2Assets$ ) ;
178
+
179
+ const appStatusChangeSpy = jasmine . createSpy ( 'app status change spy' ) ;
180
+ planetApplicationLoader . appStatusChange . subscribe ( appStatusChangeSpy ) ;
181
+
182
+ expect ( appStatusChangeSpy ) . not . toHaveBeenCalled ( ) ;
183
+ planetApplicationLoader . reroute ( { url : '/app1' } ) ;
184
+ expect ( appStatusChangeSpy ) . toHaveBeenCalled ( ) ;
185
+ expect ( appStatusChangeSpy ) . toHaveBeenCalledWith ( { app : app1 , status : ApplicationStatus . assetsLoading } ) ;
186
+
187
+ loadApp1Assets$ . next ( ) ;
188
+ loadApp1Assets$ . complete ( ) ;
189
+
190
+ expect ( appStatusChangeSpy ) . toHaveBeenCalledTimes ( 2 ) ;
191
+ expect ( appStatusChangeSpy ) . toHaveBeenCalledWith ( { app : app1 , status : ApplicationStatus . assetsLoaded } ) ;
192
+
193
+ planetApplicationLoader . reroute ( { url : '/app2' } ) ;
194
+ expect ( appStatusChangeSpy ) . toHaveBeenCalledTimes ( 3 ) ;
195
+ expect ( appStatusChangeSpy ) . toHaveBeenCalledWith ( { app : app2 , status : ApplicationStatus . assetsLoading } ) ;
196
+
197
+ loadApp2Assets$ . next ( ) ;
198
+ loadApp2Assets$ . complete ( ) ;
199
+
200
+ expect ( appStatusChangeSpy ) . toHaveBeenCalledTimes ( 4 ) ;
201
+ expect ( appStatusChangeSpy ) . toHaveBeenCalledWith ( { app : app2 , status : ApplicationStatus . assetsLoaded } ) ;
202
+
203
+ ngZone . onStable . next ( ) ;
204
+
205
+ expect ( appStatusChangeSpy ) . toHaveBeenCalledTimes ( 6 ) ;
206
+ expect ( appStatusChangeSpy ) . toHaveBeenCalledWith ( { app : app2 , status : ApplicationStatus . bootstrapping } ) ;
207
+ expect ( appStatusChangeSpy ) . toHaveBeenCalledWith ( { app : app2 , status : ApplicationStatus . bootstrapped } ) ;
208
+
209
+ expect ( app1BootstrapSpy ) . not . toHaveBeenCalled ( ) ;
210
+ expect ( app2BootstrapSpy ) . toHaveBeenCalled ( ) ;
211
+
212
+ tick ( ) ;
213
+ } ) ) ;
214
+
215
+ describe ( 'preload' , ( ) => {
216
+ it ( `should preload load app2 when after loaded app1` , fakeAsync ( ( ) => {
217
+ const loadApp1Assets$ = new Subject ( ) ;
218
+ const loadApp2Assets$ = new Subject ( ) ;
219
+
220
+ const planetApp1Ref = mockApplicationRef ( app1 . name ) ;
221
+ const app1BootstrapSpy = spyOn ( planetApp1Ref , 'bootstrap' ) ;
222
+
223
+ const planetApp2Ref = mockApplicationRef ( app2 . name ) ;
224
+ const app2BootstrapSpy = spyOn ( planetApp2Ref , 'bootstrap' ) ;
225
+
226
+ const assetsLoaderSpy = spyOn ( assetsLoader , 'loadAppAssets' ) ;
227
+ assetsLoaderSpy . and . returnValues ( loadApp1Assets$ , loadApp2Assets$ ) ;
228
+
229
+ const appStatusChangeSpy = jasmine . createSpy ( 'app status change spy' ) ;
230
+ planetApplicationLoader . appStatusChange . subscribe ( appStatusChangeSpy ) ;
231
+
232
+ expect ( appStatusChangeSpy ) . not . toHaveBeenCalled ( ) ;
233
+ planetApplicationLoader . reroute ( { url : '/app1' } ) ;
234
+ expect ( appStatusChangeSpy ) . toHaveBeenCalled ( ) ;
235
+ expect ( appStatusChangeSpy ) . toHaveBeenCalledWith ( { app : app1 , status : ApplicationStatus . assetsLoading } ) ;
236
+
237
+ loadApp1Assets$ . next ( ) ;
238
+ loadApp1Assets$ . complete ( ) ;
239
+
240
+ expect ( appStatusChangeSpy ) . toHaveBeenCalledTimes ( 2 ) ;
241
+ expect ( appStatusChangeSpy ) . toHaveBeenCalledWith ( { app : app1 , status : ApplicationStatus . assetsLoaded } ) ;
242
+
243
+ ngZone . onStable . next ( ) ;
244
+
245
+ expect ( appStatusChangeSpy ) . toHaveBeenCalledTimes ( 4 ) ;
246
+ expect ( appStatusChangeSpy ) . toHaveBeenCalledWith ( { app : app1 , status : ApplicationStatus . bootstrapping } ) ;
247
+ expect ( appStatusChangeSpy ) . toHaveBeenCalledWith ( { app : app1 , status : ApplicationStatus . bootstrapped } ) ;
248
+
249
+ expect ( app1BootstrapSpy ) . toHaveBeenCalled ( ) ;
250
+
251
+ tick ( 300 ) ;
252
+ expect ( app2BootstrapSpy ) . not . toHaveBeenCalled ( ) ;
253
+
254
+ // 已经开始加载 App2 静态资源
255
+ expect ( appStatusChangeSpy ) . toHaveBeenCalledTimes ( 5 ) ;
256
+ expect ( appStatusChangeSpy ) . toHaveBeenCalledWith ( { app : app2 , status : ApplicationStatus . assetsLoading } ) ;
257
+
258
+ // App2 静态资源加载完毕
259
+ loadApp2Assets$ . next ( ) ;
260
+ loadApp2Assets$ . complete ( ) ;
261
+
262
+ expect ( appStatusChangeSpy ) . toHaveBeenCalledTimes ( 6 ) ;
263
+ expect ( appStatusChangeSpy ) . toHaveBeenCalledWith ( { app : app2 , status : ApplicationStatus . assetsLoaded } ) ;
264
+
265
+ // onStable 开始启动应用
266
+ ngZone . onStable . next ( ) ;
267
+ expect ( appStatusChangeSpy ) . toHaveBeenCalledTimes ( 8 ) ;
268
+ expect ( appStatusChangeSpy ) . toHaveBeenCalledWith ( { app : app2 , status : ApplicationStatus . bootstrapping } ) ;
269
+ expect ( appStatusChangeSpy ) . toHaveBeenCalledWith ( { app : app2 , status : ApplicationStatus . bootstrapped } ) ;
270
+ expect ( app2BootstrapSpy ) . toHaveBeenCalled ( ) ;
271
+ } ) ) ;
272
+
273
+ it ( `should preload app` , ( ) => { } ) ;
274
+ } ) ;
165
275
} ) ;
0 commit comments