From 34bdc1a22f42f4d270519b98bd799908e0f2b8d4 Mon Sep 17 00:00:00 2001 From: why520crazy Date: Wed, 9 Oct 2019 19:54:08 +0800 Subject: [PATCH] fix(app-loader): should navigate when reroute active app #267024 --- examples/portal/src/app/app.component.html | 27 ++++++++++++++++- .../src/app/settings/settings.component.html | 3 +- .../planet-application-loader.spec.ts | 30 +++++++++++++++++++ .../application/planet-application-loader.ts | 9 ++++++ 4 files changed, 67 insertions(+), 2 deletions(-) diff --git a/examples/portal/src/app/app.component.html b/examples/portal/src/app/app.component.html index a8a035d..fac5269 100644 --- a/examples/portal/src/app/app.component.html +++ b/examples/portal/src/app/app.component.html @@ -5,7 +5,6 @@ + + + + + + + + + + To App1's dashboard + + + + + + To App1's users + + + diff --git a/examples/portal/src/app/settings/settings.component.html b/examples/portal/src/app/settings/settings.component.html index 3f549e5..ffd13b4 100644 --- a/examples/portal/src/app/settings/settings.component.html +++ b/examples/portal/src/app/settings/settings.component.html @@ -1,3 +1,4 @@ - + + diff --git a/packages/planet/src/application/planet-application-loader.spec.ts b/packages/planet/src/application/planet-application-loader.spec.ts index a27b498..1d9635d 100644 --- a/packages/planet/src/application/planet-application-loader.spec.ts +++ b/packages/planet/src/application/planet-application-loader.spec.ts @@ -49,6 +49,8 @@ class PlanetApplicationRefFaker { planetAppRef: PlanetApplicationRef; destroySpy: jasmine.Spy; bootstrapSpy: jasmine.Spy; + navigateByUrlSpy: jasmine.Spy; + bootstrap$: Subject; constructor(appName: string) { @@ -57,6 +59,7 @@ class PlanetApplicationRefFaker { this.bootstrap$ = new Subject(); this.bootstrapSpy.and.returnValues(this.bootstrap$, this.bootstrap$); this.destroySpy = spyOn(this.planetAppRef, 'destroy'); + this.navigateByUrlSpy = spyOn(this.planetAppRef, 'navigateByUrl'); (window as any).planet.apps[appName] = this.planetAppRef; } @@ -211,6 +214,33 @@ describe('PlanetApplicationLoader', () => { tick(); })); + it(`should call app1 navigateByUrl when app1 is active`, fakeAsync(() => { + const loadAppAssets$ = new Subject(); + const assetsLoaderSpy = spyOn(assetsLoader, 'loadAppAssets'); + assetsLoaderSpy.and.returnValue(loadAppAssets$); + + const appStatusChangeFaker = AppStatusChangeFaker.create(planetApplicationLoader); + const app1RefFaker = PlanetApplicationRefFaker.create(app1.name); + + planetApplicationLoader.reroute({ url: '/app1/dashboard' }); + loadAppAssets$.next(); + loadAppAssets$.complete(); + flush(); + app1RefFaker.haveBeenBootstrap(); + app1RefFaker.bootstrap$.next(); + + // 判断是否在宿主元素中创建了应用根节点 + expectApp1Element(); + + expect(appStatusChangeFaker.spy).toHaveBeenCalledTimes(5); + expect(app1RefFaker.navigateByUrlSpy).not.toHaveBeenCalled(); + planetApplicationLoader.reroute({ url: '/app1/dashboard2' }); + flush(); + expect(app1RefFaker.navigateByUrlSpy).toHaveBeenCalledTimes(1); + expect(app1RefFaker.navigateByUrlSpy).toHaveBeenCalledWith('/app1/dashboard2'); + tick(); + })); + it(`should start load app1 once when reroute same url: /app1/dashboard`, fakeAsync(() => { const loadAppAssets$ = new Subject(); const assetsLoaderSpy = spyOn(assetsLoader, 'loadAppAssets'); diff --git a/packages/planet/src/application/planet-application-loader.ts b/packages/planet/src/application/planet-application-loader.ts index f35d417..4b5f533 100644 --- a/packages/planet/src/application/planet-application-loader.ts +++ b/packages/planet/src/application/planet-application-loader.ts @@ -189,6 +189,14 @@ export class PlanetApplicationLoader { ) ); } else if (appStatus === ApplicationStatus.active) { + apps$.push( + of(app).pipe( + tap(() => { + const appRef = getPlanetApplicationRef(app.name); + appRef.navigateByUrl(event.url); + }) + ) + ); } else { throw new Error( `app(${app.name})'s status is ${appStatus}, can't be show or bootstrap` @@ -204,6 +212,7 @@ export class PlanetApplicationLoader { if (this.startRouteChangeEvent === event) { this.ngZone.runOutsideAngular(() => { forkJoin(apps$).subscribe(() => { + this.setLoadingDone(); this.ensurePreloadApps(apps); }); });