mirror of
https://github.com/DSpace/dspace-angular.git
synced 2025-10-07 01:54:15 +00:00
81 lines
3.0 KiB
TypeScript
81 lines
3.0 KiB
TypeScript
import { ServerCheckGuard } from './server-check.guard';
|
|
import { Router, NavigationStart, UrlTree, NavigationEnd, RouterEvent } from '@angular/router';
|
|
|
|
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: Router;
|
|
const eventSubject = new ReplaySubject<RouterEvent>(1);
|
|
let rootDataServiceStub: SpyObj<RootDataService>;
|
|
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: eventSubject.asObservable(),
|
|
navigateByUrl: jasmine.createSpy('navigateByUrl'),
|
|
parseUrl: jasmine.createSpy('parseUrl').and.returnValue(redirectUrlTree)
|
|
} as any;
|
|
guard = new ServerCheckGuard(router, rootDataServiceStub);
|
|
});
|
|
|
|
it('should be created', () => {
|
|
expect(guard).toBeTruthy();
|
|
});
|
|
|
|
describe('when root endpoint request has succeeded', () => {
|
|
beforeEach(() => {
|
|
rootDataServiceStub.checkServerAvailability.and.returnValue(of(true));
|
|
});
|
|
|
|
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 request has not succeeded', () => {
|
|
beforeEach(() => {
|
|
rootDataServiceStub.checkServerAvailability.and.returnValue(of(false));
|
|
});
|
|
|
|
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 invalidate the root cache when the method is first called, and then on every NavigationStart event`, () => {
|
|
testScheduler.run(() => {
|
|
guard.listenForRouteChanges();
|
|
expect(rootDataServiceStub.invalidateRootCache).toHaveBeenCalledTimes(1);
|
|
|
|
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);
|
|
});
|
|
});
|
|
});
|