Merge branch 'fix-mutliple-api-calls-on-route-change-7.6' into fix-mutliple-api-calls-on-route-change

This commit is contained in:
Art Lowel
2023-09-26 14:44:39 +02:00
2 changed files with 14 additions and 6 deletions

View File

@@ -20,7 +20,8 @@ describe('ServerCheckGuard', () => {
}); });
rootDataServiceStub = jasmine.createSpyObj('RootDataService', { rootDataServiceStub = jasmine.createSpyObj('RootDataService', {
checkServerAvailability: jasmine.createSpy('checkServerAvailability'), checkServerAvailability: jasmine.createSpy('checkServerAvailability'),
invalidateRootCache: jasmine.createSpy('invalidateRootCache') invalidateRootCache: jasmine.createSpy('invalidateRootCache'),
findRoot: jasmine.createSpy('findRoot')
}); });
redirectUrlTree = new UrlTree(); redirectUrlTree = new UrlTree();
router = { router = {
@@ -63,18 +64,23 @@ describe('ServerCheckGuard', () => {
}); });
describe(`listenForRouteChanges`, () => { 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(() => { testScheduler.run(() => {
guard.listenForRouteChanges(); 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 NavigationStart(1,''));
eventSubject.next(new NavigationEnd(1,'', '')); eventSubject.next(new NavigationEnd(1,'', ''));
eventSubject.next(new NavigationStart(2,'')); eventSubject.next(new NavigationStart(2,''));
eventSubject.next(new NavigationEnd(2,'', '')); eventSubject.next(new NavigationEnd(2,'', ''));
eventSubject.next(new NavigationStart(3,'')); eventSubject.next(new NavigationStart(3,''));
}); });
expect(rootDataServiceStub.invalidateRootCache).toHaveBeenCalledTimes(4); expect(rootDataServiceStub.invalidateRootCache).toHaveBeenCalledTimes(3);
}); });
}); });
}); });

View File

@@ -53,8 +53,10 @@ export class ServerCheckGuard implements CanActivateChild {
*/ */
listenForRouteChanges(): void { listenForRouteChanges(): void {
// we'll always be too late for the first NavigationStart event with the router subscribe below, // 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 // so this statement is for the very first route operation. A `find` without using the cache,
this.rootDataService.invalidateRootCache(); // rather than an invalidateRootCache, because invalidating as the app is bootstrapping can
// break other features
this.rootDataService.findRoot(false);
this.router.events.pipe( this.router.events.pipe(
filter(event => event instanceof NavigationStart), filter(event => event instanceof NavigationStart),