Skip to content

Commit

Permalink
Merge branch 'fix-mutliple-api-calls-on-route-change-7.6' into fix-mu…
Browse files Browse the repository at this point in the history
…tliple-api-calls-on-route-change
  • Loading branch information
artlowel committed Sep 26, 2023
2 parents 7a8e220 + 32fc28e commit d02c539
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
14 changes: 10 additions & 4 deletions src/app/core/server-check/server-check.guard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ describe('ServerCheckGuard', () => {
});
rootDataServiceStub = jasmine.createSpyObj('RootDataService', {
checkServerAvailability: jasmine.createSpy('checkServerAvailability'),
invalidateRootCache: jasmine.createSpy('invalidateRootCache')
invalidateRootCache: jasmine.createSpy('invalidateRootCache'),
findRoot: jasmine.createSpy('findRoot')
});
redirectUrlTree = new UrlTree();
router = {
Expand Down Expand Up @@ -63,18 +64,23 @@ describe('ServerCheckGuard', () => {
});

describe(`listenForRouteChanges`, () => {
it(`should invalidate the root cache when the method is first called, and then on every NavigationStart event`, () => {
it(`should retrieve the root endpoint, without using the cache, when the method is first called`, () => {
testScheduler.run(() => {
guard.listenForRouteChanges();
expect(rootDataServiceStub.invalidateRootCache).toHaveBeenCalledTimes(1);
expect(rootDataServiceStub.findRoot).toHaveBeenCalledWith(false);
});
});

it(`should invalidate the root cache on every NavigationStart event`, () => {
testScheduler.run(() => {
guard.listenForRouteChanges();
eventSubject.next(new NavigationStart(1,''));
eventSubject.next(new NavigationEnd(1,'', ''));
eventSubject.next(new NavigationStart(2,''));
eventSubject.next(new NavigationEnd(2,'', ''));
eventSubject.next(new NavigationStart(3,''));
});
expect(rootDataServiceStub.invalidateRootCache).toHaveBeenCalledTimes(4);
expect(rootDataServiceStub.invalidateRootCache).toHaveBeenCalledTimes(3);
});
});
});
6 changes: 4 additions & 2 deletions src/app/core/server-check/server-check.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ export class ServerCheckGuard implements CanActivateChild {
*/
listenForRouteChanges(): void {
// we'll always be too late for the first NavigationStart event with the router subscribe below,
// so this statement is for the very first route operation
this.rootDataService.invalidateRootCache();
// so this statement is for the very first route operation. A `find` without using the cache,
// rather than an invalidateRootCache, because invalidating as the app is bootstrapping can
// break other features
this.rootDataService.findRoot(false);

this.router.events.pipe(
filter(event => event instanceof NavigationStart),
Expand Down

0 comments on commit d02c539

Please sign in to comment.