forked from tomalaforge/angular-challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.component.spec.ts
More file actions
54 lines (48 loc) · 1.75 KB
/
app.component.spec.ts
File metadata and controls
54 lines (48 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { TestBed } from '@angular/core/testing';
import { Router, provideRouter } from '@angular/router';
import { AppComponent } from './app.component';
import { AuthService } from './auth.service';
import { routes } from './app.routes';
describe('Functional Auth Guards', () => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [provideRouter(routes)],
});
TestBed.createComponent(AppComponent);
});
describe('authGuard', () => {
it('should redirect to / when not logged in', async () => {
const router = TestBed.inject(Router);
await router.navigate(['/dashboard']);
expect(router.url).toBe('/');
});
it('should allow navigation when logged in', async () => {
const authService = TestBed.inject(AuthService);
const router = TestBed.inject(Router);
authService.login('user');
await router.navigate(['/dashboard']);
expect(router.url).toBe('/dashboard');
});
});
describe('adminGuard', () => {
it('should redirect to / when not logged in', async () => {
const router = TestBed.inject(Router);
await router.navigate(['/admin']);
expect(router.url).toBe('/');
});
it('should redirect to / when logged in as user', async () => {
const authService = TestBed.inject(AuthService);
const router = TestBed.inject(Router);
authService.login('user');
await router.navigate(['/admin']);
expect(router.url).toBe('/');
});
it('should allow navigation when logged in as admin', async () => {
const authService = TestBed.inject(AuthService);
const router = TestBed.inject(Router);
authService.login('admin');
await router.navigate(['/admin']);
expect(router.url).toBe('/admin');
});
});
});