-
Notifications
You must be signed in to change notification settings - Fork 433
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix issue where more than one api call was made on every route change
- Loading branch information
Showing
6 changed files
with
120 additions
and
68 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,63 @@ | ||
import { ServerCheckGuard } from './server-check.guard'; | ||
import { Router } from '@angular/router'; | ||
import { Router, NavigationStart, UrlTree } from '@angular/router'; | ||
|
||
import { of } from 'rxjs'; | ||
import { take } from 'rxjs/operators'; | ||
|
||
import { getPageInternalServerErrorRoute } from '../../app-routing-paths'; | ||
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 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') | ||
}); | ||
redirectUrlTree = new UrlTree(); | ||
router = { | ||
events: of(new NavigationStart(1, '')), | ||
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'); | ||
}); | ||
}); | ||
}); |
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