]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
1 | import { Component, NgZone } from '@angular/core'; |
2 | import { fakeAsync, TestBed, tick } from '@angular/core/testing'; | |
f6b5b4d7 | 3 | import { ActivatedRouteSnapshot, Router, RouterStateSnapshot, Routes } from '@angular/router'; |
11fdf7f2 TL |
4 | import { RouterTestingModule } from '@angular/router/testing'; |
5 | ||
f67539c2 | 6 | import { configureTestBed } from '~/testing/unit-test-helper'; |
11fdf7f2 TL |
7 | import { AuthGuardService } from './auth-guard.service'; |
8 | import { AuthStorageService } from './auth-storage.service'; | |
9 | ||
10 | describe('AuthGuardService', () => { | |
11 | let service: AuthGuardService; | |
12 | let authStorageService: AuthStorageService; | |
13 | let ngZone: NgZone; | |
f6b5b4d7 TL |
14 | let route: ActivatedRouteSnapshot; |
15 | let state: RouterStateSnapshot; | |
11fdf7f2 TL |
16 | |
17 | @Component({ selector: 'cd-login', template: '' }) | |
18 | class LoginComponent {} | |
19 | ||
20 | const routes: Routes = [{ path: 'login', component: LoginComponent }]; | |
21 | ||
22 | configureTestBed({ | |
23 | imports: [RouterTestingModule.withRoutes(routes)], | |
24 | providers: [AuthGuardService, AuthStorageService], | |
25 | declarations: [LoginComponent] | |
26 | }); | |
27 | ||
28 | beforeEach(() => { | |
f67539c2 TL |
29 | service = TestBed.inject(AuthGuardService); |
30 | authStorageService = TestBed.inject(AuthStorageService); | |
31 | ngZone = TestBed.inject(NgZone); | |
11fdf7f2 TL |
32 | }); |
33 | ||
34 | it('should be created', () => { | |
35 | expect(service).toBeTruthy(); | |
36 | }); | |
37 | ||
38 | it('should allow the user if loggedIn', () => { | |
f6b5b4d7 TL |
39 | route = null; |
40 | state = { url: '/', root: null }; | |
11fdf7f2 | 41 | spyOn(authStorageService, 'isLoggedIn').and.returnValue(true); |
f6b5b4d7 | 42 | expect(service.canActivate(route, state)).toBe(true); |
11fdf7f2 TL |
43 | }); |
44 | ||
45 | it('should prevent user if not loggedIn and redirect to login page', fakeAsync(() => { | |
f67539c2 | 46 | const router = TestBed.inject(Router); |
f6b5b4d7 | 47 | state = { url: '/pool', root: null }; |
11fdf7f2 | 48 | ngZone.run(() => { |
f6b5b4d7 | 49 | expect(service.canActivate(route, state)).toBe(false); |
11fdf7f2 TL |
50 | }); |
51 | tick(); | |
f6b5b4d7 | 52 | expect(router.url).toBe('/login?returnUrl=%2Fpool'); |
11fdf7f2 TL |
53 | })); |
54 | }); |