|
1 | 1 | import { ServerCheckGuard } from './server-check.guard'; |
2 | | -import { Router } from '@angular/router'; |
| 2 | +import { Router, NavigationStart, UrlTree, NavigationEnd, RouterEvent } from '@angular/router'; |
3 | 3 |
|
4 | | -import { of } from 'rxjs'; |
5 | | -import { take } from 'rxjs/operators'; |
6 | | - |
7 | | -import { getPageInternalServerErrorRoute } from '../../app-routing-paths'; |
| 4 | +import { of, ReplaySubject } from 'rxjs'; |
8 | 5 | import { RootDataService } from '../data/root-data.service'; |
| 6 | +import { TestScheduler } from 'rxjs/testing'; |
9 | 7 | import SpyObj = jasmine.SpyObj; |
10 | 8 |
|
11 | 9 | describe('ServerCheckGuard', () => { |
12 | 10 | let guard: ServerCheckGuard; |
13 | | - let router: SpyObj<Router>; |
| 11 | + let router: Router; |
| 12 | + const eventSubject = new ReplaySubject<RouterEvent>(1); |
14 | 13 | let rootDataServiceStub: SpyObj<RootDataService>; |
15 | | - |
16 | | - rootDataServiceStub = jasmine.createSpyObj('RootDataService', { |
17 | | - checkServerAvailability: jasmine.createSpy('checkServerAvailability'), |
18 | | - invalidateRootCache: jasmine.createSpy('invalidateRootCache') |
19 | | - }); |
20 | | - router = jasmine.createSpyObj('Router', { |
21 | | - navigateByUrl: jasmine.createSpy('navigateByUrl') |
22 | | - }); |
| 14 | + let testScheduler: TestScheduler; |
| 15 | + let redirectUrlTree: UrlTree; |
23 | 16 |
|
24 | 17 | beforeEach(() => { |
| 18 | + testScheduler = new TestScheduler((actual, expected) => { |
| 19 | + expect(actual).toEqual(expected); |
| 20 | + }); |
| 21 | + rootDataServiceStub = jasmine.createSpyObj('RootDataService', { |
| 22 | + checkServerAvailability: jasmine.createSpy('checkServerAvailability'), |
| 23 | + invalidateRootCache: jasmine.createSpy('invalidateRootCache'), |
| 24 | + findRoot: jasmine.createSpy('findRoot') |
| 25 | + }); |
| 26 | + redirectUrlTree = new UrlTree(); |
| 27 | + router = { |
| 28 | + events: eventSubject.asObservable(), |
| 29 | + navigateByUrl: jasmine.createSpy('navigateByUrl'), |
| 30 | + parseUrl: jasmine.createSpy('parseUrl').and.returnValue(redirectUrlTree) |
| 31 | + } as any; |
25 | 32 | guard = new ServerCheckGuard(router, rootDataServiceStub); |
26 | 33 | }); |
27 | 34 |
|
28 | | - afterEach(() => { |
29 | | - router.navigateByUrl.calls.reset(); |
30 | | - rootDataServiceStub.invalidateRootCache.calls.reset(); |
31 | | - }); |
32 | | - |
33 | 35 | it('should be created', () => { |
34 | 36 | expect(guard).toBeTruthy(); |
35 | 37 | }); |
36 | 38 |
|
37 | | - describe('when root endpoint has succeeded', () => { |
| 39 | + describe('when root endpoint request has succeeded', () => { |
38 | 40 | beforeEach(() => { |
39 | 41 | rootDataServiceStub.checkServerAvailability.and.returnValue(of(true)); |
40 | 42 | }); |
41 | 43 |
|
42 | | - it('should not redirect to error page', () => { |
43 | | - guard.canActivateChild({} as any, {} as any).pipe( |
44 | | - take(1) |
45 | | - ).subscribe((canActivate: boolean) => { |
46 | | - expect(canActivate).toEqual(true); |
47 | | - expect(rootDataServiceStub.invalidateRootCache).not.toHaveBeenCalled(); |
48 | | - expect(router.navigateByUrl).not.toHaveBeenCalled(); |
| 44 | + it('should return true', () => { |
| 45 | + testScheduler.run(({ expectObservable }) => { |
| 46 | + const result$ = guard.canActivateChild({} as any, {} as any); |
| 47 | + expectObservable(result$).toBe('(a|)', { a: true }); |
49 | 48 | }); |
50 | 49 | }); |
51 | 50 | }); |
52 | 51 |
|
53 | | - describe('when root endpoint has not succeeded', () => { |
| 52 | + describe('when root endpoint request has not succeeded', () => { |
54 | 53 | beforeEach(() => { |
55 | 54 | rootDataServiceStub.checkServerAvailability.and.returnValue(of(false)); |
56 | 55 | }); |
57 | 56 |
|
58 | | - it('should redirect to error page', () => { |
59 | | - guard.canActivateChild({} as any, {} as any).pipe( |
60 | | - take(1) |
61 | | - ).subscribe((canActivate: boolean) => { |
62 | | - expect(canActivate).toEqual(false); |
63 | | - expect(rootDataServiceStub.invalidateRootCache).toHaveBeenCalled(); |
64 | | - expect(router.navigateByUrl).toHaveBeenCalledWith(getPageInternalServerErrorRoute()); |
| 57 | + it('should return a UrlTree with the route to the 500 error page', () => { |
| 58 | + testScheduler.run(({ expectObservable }) => { |
| 59 | + const result$ = guard.canActivateChild({} as any, {} as any); |
| 60 | + expectObservable(result$).toBe('(b|)', { b: redirectUrlTree }); |
65 | 61 | }); |
| 62 | + expect(router.parseUrl).toHaveBeenCalledWith('/500'); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + describe(`listenForRouteChanges`, () => { |
| 67 | + it(`should retrieve the root endpoint, without using the cache, when the method is first called`, () => { |
| 68 | + testScheduler.run(() => { |
| 69 | + guard.listenForRouteChanges(); |
| 70 | + expect(rootDataServiceStub.findRoot).toHaveBeenCalledWith(false); |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + it(`should invalidate the root cache on every NavigationStart event`, () => { |
| 75 | + testScheduler.run(() => { |
| 76 | + guard.listenForRouteChanges(); |
| 77 | + eventSubject.next(new NavigationStart(1,'')); |
| 78 | + eventSubject.next(new NavigationEnd(1,'', '')); |
| 79 | + eventSubject.next(new NavigationStart(2,'')); |
| 80 | + eventSubject.next(new NavigationEnd(2,'', '')); |
| 81 | + eventSubject.next(new NavigationStart(3,'')); |
| 82 | + }); |
| 83 | + expect(rootDataServiceStub.invalidateRootCache).toHaveBeenCalledTimes(3); |
66 | 84 | }); |
67 | 85 | }); |
68 | 86 | }); |
0 commit comments