Skip to content

Commit

Permalink
fix(planet): should reroute once when start planet
Browse files Browse the repository at this point in the history
  • Loading branch information
why520crazy committed Oct 15, 2019
1 parent 4a43110 commit 7f1bb3b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
8 changes: 4 additions & 4 deletions packages/planet/src/planet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,21 +114,21 @@ describe('Planet', () => {
expect(setOptionsSpy).toHaveBeenCalledWith(options);
});

it('should reroute when navigateByUrl', fakeAsync(() => {
it('should reroute when start or navigateByUrl', fakeAsync(() => {
const router: Router = TestBed.get(Router);
const ngZone: NgZone = TestBed.get(NgZone);
const rerouteSpy = spyOn(planetApplicationLoader, 'reroute');
expect(rerouteSpy).not.toHaveBeenCalled();
planet.start();
expect(rerouteSpy).not.toHaveBeenCalled();
expect(rerouteSpy).toHaveBeenCalledTimes(1);

ngZone.run(() => {
router.navigateByUrl('/app1/dashboard');
});

tick();

expect(rerouteSpy).toHaveBeenCalledTimes(1);
expect(rerouteSpy).toHaveBeenCalledTimes(2);
expect(rerouteSpy).toHaveBeenCalledWith({
url: '/app1/dashboard'
});
Expand All @@ -138,7 +138,7 @@ describe('Planet', () => {
});
tick();

expect(rerouteSpy).toHaveBeenCalledTimes(2);
expect(rerouteSpy).toHaveBeenCalledTimes(3);
expect(rerouteSpy).toHaveBeenCalledWith({
url: '/app1/users'
});
Expand Down
20 changes: 15 additions & 5 deletions packages/planet/src/planet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
AppStatusChangeEvent
} from './application/planet-application-loader';
import { Observable } from 'rxjs';
import { filter, startWith, distinctUntilChanged, map } from 'rxjs/operators';

@Injectable({
providedIn: 'root'
Expand Down Expand Up @@ -62,12 +63,21 @@ export class Planet {
}

start() {
this.router.events.subscribe((event: RouterEvent) => {
if (event instanceof NavigationEnd) {
this.router.events
.pipe(
filter(event => {
return event instanceof NavigationEnd;
}),
map((event: NavigationEnd) => {
return event.url;
}),
startWith(location.pathname),
distinctUntilChanged()
)
.subscribe((url: string) => {
this.planetApplicationLoader.reroute({
url: event.url
url: url
});
}
});
});
}
}

0 comments on commit 7f1bb3b

Please sign in to comment.