forked from DSpace/dspace-angular
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request DSpace#2529 from atmire/fix-mutliple-api-calls-on-…
…route-change-8.0.0-next [Port main] Fix multiple api calls on route change
- Loading branch information
Showing
7 changed files
with
187 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,86 @@ | ||
import { ServerCheckGuard } from './server-check.guard'; | ||
import { Router } from '@angular/router'; | ||
import { Router, NavigationStart, UrlTree, NavigationEnd, RouterEvent } from '@angular/router'; | ||
|
||
import { of } from 'rxjs'; | ||
import { take } from 'rxjs/operators'; | ||
|
||
import { getPageInternalServerErrorRoute } from '../../app-routing-paths'; | ||
import { of, ReplaySubject } from 'rxjs'; | ||
import { RootDataService } from '../data/root-data.service'; | ||
import { TestScheduler } from 'rxjs/testing'; | ||
import SpyObj = jasmine.SpyObj; | ||
|
||
describe('ServerCheckGuard', () => { | ||
let guard: ServerCheckGuard; | ||
let router: SpyObj<Router>; | ||
let router: Router; | ||
const eventSubject = new ReplaySubject<RouterEvent>(1); | ||
let rootDataServiceStub: SpyObj<RootDataService>; | ||
|
||
rootDataServiceStub = jasmine.createSpyObj('RootDataService', { | ||
checkServerAvailability: jasmine.createSpy('checkServerAvailability'), | ||
invalidateRootCache: jasmine.createSpy('invalidateRootCache') | ||
}); | ||
router = jasmine.createSpyObj('Router', { | ||
navigateByUrl: jasmine.createSpy('navigateByUrl') | ||
}); | ||
let testScheduler: TestScheduler; | ||
let redirectUrlTree: UrlTree; | ||
|
||
beforeEach(() => { | ||
testScheduler = new TestScheduler((actual, expected) => { | ||
expect(actual).toEqual(expected); | ||
}); | ||
rootDataServiceStub = jasmine.createSpyObj('RootDataService', { | ||
checkServerAvailability: jasmine.createSpy('checkServerAvailability'), | ||
invalidateRootCache: jasmine.createSpy('invalidateRootCache'), | ||
findRoot: jasmine.createSpy('findRoot') | ||
}); | ||
redirectUrlTree = new UrlTree(); | ||
router = { | ||
events: eventSubject.asObservable(), | ||
navigateByUrl: jasmine.createSpy('navigateByUrl'), | ||
parseUrl: jasmine.createSpy('parseUrl').and.returnValue(redirectUrlTree) | ||
} as any; | ||
guard = new ServerCheckGuard(router, rootDataServiceStub); | ||
}); | ||
|
||
afterEach(() => { | ||
router.navigateByUrl.calls.reset(); | ||
rootDataServiceStub.invalidateRootCache.calls.reset(); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(guard).toBeTruthy(); | ||
}); | ||
|
||
describe('when root endpoint has succeeded', () => { | ||
describe('when root endpoint request has succeeded', () => { | ||
beforeEach(() => { | ||
rootDataServiceStub.checkServerAvailability.and.returnValue(of(true)); | ||
}); | ||
|
||
it('should not redirect to error page', () => { | ||
guard.canActivateChild({} as any, {} as any).pipe( | ||
take(1) | ||
).subscribe((canActivate: boolean) => { | ||
expect(canActivate).toEqual(true); | ||
expect(rootDataServiceStub.invalidateRootCache).not.toHaveBeenCalled(); | ||
expect(router.navigateByUrl).not.toHaveBeenCalled(); | ||
it('should return true', () => { | ||
testScheduler.run(({ expectObservable }) => { | ||
const result$ = guard.canActivateChild({} as any, {} as any); | ||
expectObservable(result$).toBe('(a|)', { a: true }); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('when root endpoint has not succeeded', () => { | ||
describe('when root endpoint request has not succeeded', () => { | ||
beforeEach(() => { | ||
rootDataServiceStub.checkServerAvailability.and.returnValue(of(false)); | ||
}); | ||
|
||
it('should redirect to error page', () => { | ||
guard.canActivateChild({} as any, {} as any).pipe( | ||
take(1) | ||
).subscribe((canActivate: boolean) => { | ||
expect(canActivate).toEqual(false); | ||
expect(rootDataServiceStub.invalidateRootCache).toHaveBeenCalled(); | ||
expect(router.navigateByUrl).toHaveBeenCalledWith(getPageInternalServerErrorRoute()); | ||
it('should return a UrlTree with the route to the 500 error page', () => { | ||
testScheduler.run(({ expectObservable }) => { | ||
const result$ = guard.canActivateChild({} as any, {} as any); | ||
expectObservable(result$).toBe('(b|)', { b: redirectUrlTree }); | ||
}); | ||
expect(router.parseUrl).toHaveBeenCalledWith('/500'); | ||
}); | ||
}); | ||
|
||
describe(`listenForRouteChanges`, () => { | ||
it(`should retrieve the root endpoint, without using the cache, when the method is first called`, () => { | ||
testScheduler.run(() => { | ||
guard.listenForRouteChanges(); | ||
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(3); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.