From cf9fdf33aa884fd5fd0cab5efa40c00176a478ce Mon Sep 17 00:00:00 2001 From: Tiago Melo Date: Mon, 5 Nov 2018 10:59:13 +0000 Subject: [PATCH] mgr/dashboard: Remove unit tests warnings Recent Angular update is causing a warning to be shown when you call "router.navigate" outside an angular zone. This is not affecting the code itself, just the unit tests. Signed-off-by: Tiago Melo --- .../breadcrumbs/breadcrumbs.component.spec.ts | 20 ++++++++++++++----- .../services/auth-guard.service.spec.ts | 8 ++++++-- .../module-status-guard.service.spec.ts | 10 +++++++--- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/src/pybind/mgr/dashboard/frontend/src/app/core/navigation/breadcrumbs/breadcrumbs.component.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/core/navigation/breadcrumbs/breadcrumbs.component.spec.ts index bfc86a7766c..f593d268ec8 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/core/navigation/breadcrumbs/breadcrumbs.component.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/core/navigation/breadcrumbs/breadcrumbs.component.spec.ts @@ -65,7 +65,9 @@ describe('BreadcrumbsComponent', () => { }); it('should run postProcess and split the breadcrumbs when navigating to hosts', fakeAsync(() => { - router.navigateByUrl('/hosts'); + fixture.ngZone.run(() => { + router.navigateByUrl('/hosts'); + }); tick(); expect(component.crumbs).toEqual([ { path: null, text: 'Cluster' }, @@ -74,7 +76,9 @@ describe('BreadcrumbsComponent', () => { })); it('should display empty breadcrumb when navigating to perf_counters from unknown path', fakeAsync(() => { - router.navigateByUrl('/perf_counters'); + fixture.ngZone.run(() => { + router.navigateByUrl('/perf_counters'); + }); tick(); expect(component.crumbs).toEqual([ { path: null, text: 'Cluster' }, @@ -84,7 +88,9 @@ describe('BreadcrumbsComponent', () => { })); it('should display Monitor breadcrumb when navigating to perf_counters from Monitors', fakeAsync(() => { - router.navigate(['/perf_counters'], { queryParams: { fromLink: '/monitor' } }); + fixture.ngZone.run(() => { + router.navigate(['/perf_counters'], { queryParams: { fromLink: '/monitor' } }); + }); tick(); expect(component.crumbs).toEqual([ { path: null, text: 'Cluster' }, @@ -94,7 +100,9 @@ describe('BreadcrumbsComponent', () => { })); it('should display Hosts breadcrumb when navigating to perf_counters from Hosts', fakeAsync(() => { - router.navigate(['/perf_counters'], { queryParams: { fromLink: '/hosts' } }); + fixture.ngZone.run(() => { + router.navigate(['/perf_counters'], { queryParams: { fromLink: '/hosts' } }); + }); tick(); expect(component.crumbs).toEqual([ { path: null, text: 'Cluster' }, @@ -104,7 +112,9 @@ describe('BreadcrumbsComponent', () => { })); it('should show all 3 breadcrumbs when navigating to RBD Add', fakeAsync(() => { - router.navigateByUrl('/block/rbd/add'); + fixture.ngZone.run(() => { + router.navigateByUrl('/block/rbd/add'); + }); tick(); expect(component.crumbs).toEqual([ { path: null, text: 'Block' }, diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/auth-guard.service.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/auth-guard.service.spec.ts index ff101219112..bf8c71c34d8 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/auth-guard.service.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/auth-guard.service.spec.ts @@ -1,4 +1,4 @@ -import { Component } from '@angular/core'; +import { Component, NgZone } from '@angular/core'; import { fakeAsync, TestBed, tick } from '@angular/core/testing'; import { Router, Routes } from '@angular/router'; import { RouterTestingModule } from '@angular/router/testing'; @@ -10,6 +10,7 @@ import { AuthStorageService } from './auth-storage.service'; describe('AuthGuardService', () => { let service: AuthGuardService; let authStorageService: AuthStorageService; + let ngZone: NgZone; @Component({ selector: 'cd-login', template: '' }) class LoginComponent {} @@ -25,6 +26,7 @@ describe('AuthGuardService', () => { beforeEach(() => { service = TestBed.get(AuthGuardService); authStorageService = TestBed.get(AuthStorageService); + ngZone = TestBed.get(NgZone); }); it('should be created', () => { @@ -38,7 +40,9 @@ describe('AuthGuardService', () => { it('should prevent user if not loggedIn and redirect to login page', fakeAsync(() => { const router = TestBed.get(Router); - expect(service.canActivate(null, null)).toBe(false); + ngZone.run(() => { + expect(service.canActivate(null, null)).toBe(false); + }); tick(); expect(router.url).toBe('/login'); })); diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/module-status-guard.service.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/module-status-guard.service.spec.ts index a4d5eb8f411..112a194f12a 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/module-status-guard.service.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/module-status-guard.service.spec.ts @@ -1,5 +1,5 @@ import { HttpClient } from '@angular/common/http'; -import { Component } from '@angular/core'; +import { Component, NgZone } from '@angular/core'; import { fakeAsync, TestBed, tick } from '@angular/core/testing'; import { ActivatedRouteSnapshot, Router, Routes } from '@angular/router'; import { RouterTestingModule } from '@angular/router/testing'; @@ -14,6 +14,7 @@ describe('ModuleStatusGuardService', () => { let httpClient: HttpClient; let router: Router; let route: ActivatedRouteSnapshot; + let ngZone: NgZone; @Component({ selector: 'cd-foo', template: '' }) class FooComponent {} @@ -27,8 +28,10 @@ describe('ModuleStatusGuardService', () => { const testCanActivate = (getResult: {}, activateResult: boolean, urlResult: string) => { let result: boolean; spyOn(httpClient, 'get').and.returnValue(observableOf(getResult)); - service.canActivateChild(route, null).subscribe((resp) => { - result = resp; + ngZone.run(() => { + service.canActivateChild(route, null).subscribe((resp) => { + result = resp; + }); }); tick(); @@ -56,6 +59,7 @@ describe('ModuleStatusGuardService', () => { redirectTo: '/foo' } }; + ngZone = TestBed.get(NgZone); }); it('should be created', () => { -- 2.39.5