Skip to content

Commit

Permalink
fix(app-loader): should navigate when reroute active app #267024
Browse files Browse the repository at this point in the history
  • Loading branch information
why520crazy committed Oct 9, 2019
1 parent 0fb9aae commit 34bdc1a
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
27 changes: 26 additions & 1 deletion examples/portal/src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<a
thyNavLink
thyTooltip="App1"
thyTooltipPlacement="right"
[routerLink]="['/app1']"
routerLinkActive="active"
thyTooltip="App1"
Expand All @@ -27,6 +26,32 @@
thyTooltipPlacement="right"
><thy-icon thyIconName="settings"></thy-icon
></a>
<a
thyNavLink
href="javascript:;"
[thyPopover]="more"
thyPlacement="right"
thyTooltip="More"
thyTooltipPlacement="right"
>
<thy-icon thyIconName="more"></thy-icon>
</a>
<ng-template #more>
<thy-action-menu>
<a thyActionMenuItem routerLinkActive="active" [routerLink]="['/app1/dashboard']">
<span thyActionMenuItemIcon>
<thy-icon thyIconName="house"></thy-icon>
</span>
<span thyActionMenuItemName>To App1's dashboard</span>
</a>
<a thyActionMenuItem routerLinkActive="active" [routerLink]="['/app1/users']">
<span thyActionMenuItemIcon>
<thy-icon thyIconName="user-group"></thy-icon>
</span>
<span thyActionMenuItemName>To App1's users</span>
</a>
</thy-action-menu>
</ng-template>
</thy-nav>
</thy-sidebar>
<router-outlet></router-outlet>
Expand Down
3 changes: 2 additions & 1 deletion examples/portal/src/app/settings/settings.component.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<section-card title="Settings">
<label thyCheckbox name="Preload">App1 Preload</label>
<label thyCheckbox name="app1-preload" thyLabelText="App1 Preload"></label>
<label thyCheckbox name="app2-preload" thyLabelText="App2 Preload"></label>
</section-card>
30 changes: 30 additions & 0 deletions packages/planet/src/application/planet-application-loader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class PlanetApplicationRefFaker {
planetAppRef: PlanetApplicationRef;
destroySpy: jasmine.Spy;
bootstrapSpy: jasmine.Spy;
navigateByUrlSpy: jasmine.Spy;

bootstrap$: Subject<PlanetApplicationRef>;

constructor(appName: string) {
Expand All @@ -57,6 +59,7 @@ class PlanetApplicationRefFaker {
this.bootstrap$ = new Subject<PlanetApplicationRef>();
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;
}

Expand Down Expand Up @@ -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');
Expand Down
9 changes: 9 additions & 0 deletions packages/planet/src/application/planet-application-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -204,6 +212,7 @@ export class PlanetApplicationLoader {
if (this.startRouteChangeEvent === event) {
this.ngZone.runOutsideAngular(() => {
forkJoin(apps$).subscribe(() => {
this.setLoadingDone();
this.ensurePreloadApps(apps);
});
});
Expand Down

0 comments on commit 34bdc1a

Please sign in to comment.