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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { NO_ERRORS_SCHEMA } from '@angular/core';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { AuthService } from './services/auth.service';
describe('Component: App', () => {
let component: AppComponent;
let fixture: ComponentFixture<AppComponent>;
let authService: AuthService;
let authServiceStub: {
loggedIn: boolean,
isAdmin: boolean,
currentUser: any
};
beforeEach(async(() => {
authServiceStub = {
loggedIn: false,
isAdmin: false,
currentUser: { username: 'Tester' }
};
TestBed.configureTestingModule({
declarations: [ AppComponent ],
providers: [ { provide: AuthService, useValue: authServiceStub } ],
schemas: [ NO_ERRORS_SCHEMA ]
})
.compileComponents().then(() => {
fixture = TestBed.createComponent(AppComponent);
component = fixture.componentInstance;
authService = fixture.debugElement.injector.get(AuthService);
fixture.detectChanges();
});
}));
it('should create the app', async(() => {
expect(component).toBeTruthy();
}));
it('should display the navigation bar correctly for guests', () => {
const de = fixture.debugElement.queryAll(By.css('a'));
expect(de.length).toBe(6);
expect(de[0].nativeElement.textContent).toContain('Home');
expect(de[1].nativeElement.textContent).toContain('Cats');
expect(de[2].nativeElement.textContent).toContain('Login');
expect(de[3].nativeElement.textContent).toContain('Register');
expect(de[4].nativeElement.textContent).toContain('Dogs'); //add
expect(de[5].nativeElement.textContent).toContain('Pigs'); //add
expect(de[0].attributes['routerLink']).toBe('/');
expect(de[1].attributes['routerLink']).toBe('/cats');
expect(de[2].attributes['routerLink']).toBe('/login');
expect(de[3].attributes['routerLink']).toBe('/register');
expect(de[4].attributes['routerLink']).toBe('/dogs');//add
expect(de[5].attributes['routerLink']).toBe('/Pigs');//add
});
it('should display the navigation bar correctly for logged users', () => {
authService.loggedIn = true;
fixture.detectChanges();
const de = fixture.debugElement.queryAll(By.css('a'));
expect(de.length).toBe(6);
expect(de[0].nativeElement.textContent).toContain('Home');
expect(de[1].nativeElement.textContent).toContain('Cats');
expect(de[2].nativeElement.textContent).toContain('Account (Tester)');
expect(de[3].nativeElement.textContent).toContain('Logout');
expect(de[4].nativeElement.textContent).toContain('Dogs');//add
expect(de[5].nativeElement.textContent).toContain('Pigs');//add
expect(de[0].attributes['routerLink']).toBe('/');
expect(de[1].attributes['routerLink']).toBe('/cats');
expect(de[2].attributes['routerLink']).toBe('/account');
expect(de[3].attributes['routerLink']).toBe('/logout');
expect(de[4].attributes['routerLink']).toBe('/dogs');//add
expect(de[5].attributes['routerLink']).toBe('/pigs');//add
});
it('should display the navigation bar correctly for admin users', () => {
authService.loggedIn = true;
authService.isAdmin = true;
fixture.detectChanges();
const de = fixture.debugElement.queryAll(By.css('a'));
expect(de.length).toBe(7);
expect(de[0].nativeElement.textContent).toContain('Home');
expect(de[1].nativeElement.textContent).toContain('Cats');
expect(de[2].nativeElement.textContent).toContain('Account (Tester)');
expect(de[3].nativeElement.textContent).toContain('Admin');
expect(de[4].nativeElement.textContent).toContain('Logout');
expect(de[5].nativeElement.textContent).toContain('Dogs'); //add
expect(de[6].nativeElement.textContent).toContain('Pigs'); //add
expect(de[0].attributes['routerLink']).toBe('/');
expect(de[1].attributes['routerLink']).toBe('/cats');
expect(de[2].attributes['routerLink']).toBe('/account');
expect(de[3].attributes['routerLink']).toBe('/admin');
expect(de[4].attributes['routerLink']).toBe('/logout');
expect(de[5].attributes['routerLink']).toBe('/dogs');//add
expect(de[6].attributes['routerLink']).toBe('/pigs');//add
});
});