1
- import { NO_ERRORS_SCHEMA } from '@angular/core' ;
2
1
import { waitForAsync , ComponentFixture , TestBed } from '@angular/core/testing' ;
3
- import { By } from '@angular/platform-browser ' ;
2
+ import { RouterTestingModule } from '@angular/router/testing ' ;
4
3
5
4
import { AuthService } from './services/auth.service' ;
6
5
import { AppComponent } from './app.component' ;
@@ -11,71 +10,61 @@ describe('Component: App', () => {
11
10
let component : AppComponent ;
12
11
let fixture : ComponentFixture < AppComponent > ;
13
12
let authService : AuthService ;
13
+ let compiled : HTMLElement ;
14
14
15
15
beforeEach ( waitForAsync ( ( ) => {
16
16
TestBed . configureTestingModule ( {
17
+ imports : [ RouterTestingModule ] ,
17
18
declarations : [ AppComponent ] ,
18
19
providers : [ { provide : AuthService , useClass : AuthServiceMock } ] ,
19
- schemas : [ NO_ERRORS_SCHEMA ]
20
- } )
21
- . compileComponents ( ) . then ( ( ) => {
22
- fixture = TestBed . createComponent ( AppComponent ) ;
23
- component = fixture . componentInstance ;
24
- authService = fixture . debugElement . injector . get ( AuthService ) ;
25
- fixture . detectChanges ( ) ;
26
- } ) ;
20
+ } ) . compileComponents ( ) ;
27
21
} ) ) ;
28
22
23
+ beforeEach ( ( ) => {
24
+ fixture = TestBed . createComponent ( AppComponent ) ;
25
+ component = fixture . componentInstance ;
26
+ authService = fixture . debugElement . injector . get ( AuthService ) ;
27
+ fixture . detectChanges ( ) ;
28
+ compiled = fixture . nativeElement as HTMLElement ;
29
+ } ) ;
30
+
29
31
it ( 'should create the app' , waitForAsync ( ( ) => {
30
32
expect ( component ) . toBeTruthy ( ) ;
31
33
} ) ) ;
32
34
33
35
it ( 'should display the navigation bar correctly for guests' , ( ) => {
34
- const de = fixture . debugElement . queryAll ( By . css ( 'a' ) ) ;
35
- expect ( de . length ) . toBe ( 4 ) ;
36
- expect ( de [ 0 ] . nativeElement . textContent ) . toContain ( 'Home' ) ;
37
- expect ( de [ 1 ] . nativeElement . textContent ) . toContain ( 'Cats' ) ;
38
- expect ( de [ 2 ] . nativeElement . textContent ) . toContain ( 'Login' ) ;
39
- expect ( de [ 3 ] . nativeElement . textContent ) . toContain ( 'Register' ) ;
40
- expect ( de [ 0 ] . attributes . routerLink ) . toBe ( '/' ) ;
41
- expect ( de [ 1 ] . attributes . routerLink ) . toBe ( '/cats' ) ;
42
- expect ( de [ 2 ] . attributes . routerLink ) . toBe ( '/login' ) ;
43
- expect ( de [ 3 ] . attributes . routerLink ) . toBe ( '/register' ) ;
36
+ const elems = compiled . querySelectorAll ( '.nav-link' ) ;
37
+ expect ( elems . length ) . toBe ( 4 ) ;
38
+ expect ( elems [ 0 ] . textContent ) . toContain ( 'Home' ) ;
39
+ expect ( elems [ 1 ] . textContent ) . toContain ( 'Cats' ) ;
40
+ expect ( elems [ 2 ] . textContent ) . toContain ( 'Login' ) ;
41
+ expect ( elems [ 3 ] . textContent ) . toContain ( 'Register' ) ;
44
42
} ) ;
45
43
46
44
it ( 'should display the navigation bar correctly for logged users' , ( ) => {
47
45
authService . loggedIn = true ;
48
- authService . currentUser = { username : 'Tester' } ;
46
+ authService . currentUser = { _id : '123' , username : 'Tester' , role : 'user ' } ;
49
47
fixture . detectChanges ( ) ;
50
- const de = fixture . debugElement . queryAll ( By . css ( 'a' ) ) ;
51
- expect ( de . length ) . toBe ( 4 ) ;
52
- expect ( de [ 0 ] . nativeElement . textContent ) . toContain ( 'Home' ) ;
53
- expect ( de [ 1 ] . nativeElement . textContent ) . toContain ( 'Cats' ) ;
54
- expect ( de [ 2 ] . nativeElement . textContent ) . toContain ( 'Account (Tester)' ) ;
55
- expect ( de [ 3 ] . nativeElement . textContent ) . toContain ( 'Logout' ) ;
56
- expect ( de [ 0 ] . attributes . routerLink ) . toBe ( '/' ) ;
57
- expect ( de [ 1 ] . attributes . routerLink ) . toBe ( '/cats' ) ;
58
- expect ( de [ 2 ] . attributes . routerLink ) . toBe ( '/account' ) ;
59
- expect ( de [ 3 ] . attributes . routerLink ) . toBe ( '/logout' ) ;
48
+ const elems = compiled . querySelectorAll ( '.nav-link' ) ;
49
+ expect ( elems . length ) . toBe ( 4 ) ;
50
+ expect ( elems [ 0 ] . textContent ) . toContain ( 'Home' ) ;
51
+ expect ( elems [ 1 ] . textContent ) . toContain ( 'Cats' ) ;
52
+ expect ( elems [ 2 ] . textContent ) . toContain ( 'Account (Tester)' ) ;
53
+ expect ( elems [ 3 ] . textContent ) . toContain ( 'Logout' ) ;
60
54
} ) ;
61
55
62
56
it ( 'should display the navigation bar correctly for admin users' , ( ) => {
63
57
authService . loggedIn = true ;
64
58
authService . isAdmin = true ;
65
- authService . currentUser = { username : 'Tester' } ;
59
+ authService . currentUser = { _id : '123' , username : 'Tester' , role : 'admin ' } ;
66
60
fixture . detectChanges ( ) ;
67
- const de = fixture . debugElement . queryAll ( By . css ( 'a' ) ) ;
68
- expect ( de . length ) . toBe ( 5 ) ;
69
- expect ( de [ 0 ] . nativeElement . textContent ) . toContain ( 'Home' ) ;
70
- expect ( de [ 1 ] . nativeElement . textContent ) . toContain ( 'Cats' ) ;
71
- expect ( de [ 2 ] . nativeElement . textContent ) . toContain ( 'Account (Tester)' ) ;
72
- expect ( de [ 3 ] . nativeElement . textContent ) . toContain ( 'Admin' ) ;
73
- expect ( de [ 4 ] . nativeElement . textContent ) . toContain ( 'Logout' ) ;
74
- expect ( de [ 0 ] . attributes . routerLink ) . toBe ( '/' ) ;
75
- expect ( de [ 1 ] . attributes . routerLink ) . toBe ( '/cats' ) ;
76
- expect ( de [ 2 ] . attributes . routerLink ) . toBe ( '/account' ) ;
77
- expect ( de [ 3 ] . attributes . routerLink ) . toBe ( '/admin' ) ;
78
- expect ( de [ 4 ] . attributes . routerLink ) . toBe ( '/logout' ) ;
61
+ const elems = compiled . querySelectorAll ( '.nav-link' ) ;
62
+ expect ( elems . length ) . toBe ( 5 ) ;
63
+ expect ( elems [ 0 ] . textContent ) . toContain ( 'Home' ) ;
64
+ expect ( elems [ 1 ] . textContent ) . toContain ( 'Cats' ) ;
65
+ expect ( elems [ 2 ] . textContent ) . toContain ( 'Account (Tester)' ) ;
66
+ expect ( elems [ 3 ] . textContent ) . toContain ( 'Admin' ) ;
67
+ expect ( elems [ 4 ] . textContent ) . toContain ( 'Logout' ) ;
79
68
} ) ;
80
69
81
70
} ) ;
0 commit comments