From edbd69d91ca383a93f9874f8d275de2d8570357a Mon Sep 17 00:00:00 2001 From: jayfyle <115472256+jayfyle@users.noreply.github.com> Date: Mon, 25 Sep 2023 13:35:23 +0530 Subject: [PATCH] coverage at 100 (#2445) Co-authored-by: Jay Budhadev --- src/app/core/guards/auth.guard.spec.ts | 60 ++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 3 deletions(-) diff --git a/src/app/core/guards/auth.guard.spec.ts b/src/app/core/guards/auth.guard.spec.ts index 2cfa8e9a62..772b787ede 100644 --- a/src/app/core/guards/auth.guard.spec.ts +++ b/src/app/core/guards/auth.guard.spec.ts @@ -1,16 +1,70 @@ import { TestBed } from '@angular/core/testing'; - import { AuthGuard } from './auth.guard'; +import { AuthService } from '../services/auth.service'; +import { ActivatedRoute, Router } from '@angular/router'; +import { RouterTestingModule } from '@angular/router/testing'; +import { apiEouRes } from '../mock-data/extended-org-user.data'; -xdescribe('AuthGuard', () => { +describe('AuthGuard', () => { let guard: AuthGuard; + let authService: jasmine.SpyObj; + let router: jasmine.SpyObj; + let activatedRoute: jasmine.SpyObj; beforeEach(() => { - TestBed.configureTestingModule({}); + const authServiceSpy = jasmine.createSpyObj('AuthService', ['getEou']); + const routerSpy = jasmine.createSpyObj('Router', ['navigate']); + + TestBed.configureTestingModule({ + imports: [RouterTestingModule], + providers: [ + { + provide: AuthService, + useValue: authServiceSpy, + }, + { + provide: Router, + useValue: routerSpy, + }, + { + provide: ActivatedRoute, + useValue: { + snapshot: { + data: { + url: '/enterprise/dashboard', + root: null, + }, + }, + }, + }, + ], + }); guard = TestBed.inject(AuthGuard); + authService = TestBed.inject(AuthService) as jasmine.SpyObj; + activatedRoute = TestBed.inject(ActivatedRoute) as jasmine.SpyObj; + router = TestBed.inject(Router) as jasmine.SpyObj; }); it('should be created', () => { expect(guard).toBeTruthy(); }); + + describe('canActivate():', () => { + it('should return the user org', async () => { + authService.getEou.and.resolveTo(apiEouRes); + + const result = await guard.canActivate(activatedRoute.snapshot, { url: '/test', root: null }); + + expect(result).toBeTrue(); + expect(authService.getEou).toHaveBeenCalledTimes(1); + }); + + it('should navigate to sign in page if org is not present', async () => { + authService.getEou.and.resolveTo(null); + + await guard.canActivate(activatedRoute.snapshot, { url: '/test', root: null }); + + expect(router.navigate).toHaveBeenCalledOnceWith(['/', 'auth', 'sign_in']); + }); + }); });