-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsidebar.component.spec.ts
More file actions
125 lines (111 loc) · 3.96 KB
/
sidebar.component.spec.ts
File metadata and controls
125 lines (111 loc) · 3.96 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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import { AzureAdB2CService } from 'src/app/modules/login/services/azure.ad.b2c.service';
import { waitForAsync, ComponentFixture, TestBed } from '@angular/core/testing';
import { SidebarComponent } from './sidebar.component';
import { RouterTestingModule } from '@angular/router/testing';
import { Router, Routes } from '@angular/router';
import { TimeClockComponent } from '../../../time-clock/pages/time-clock.component';
import { of } from 'rxjs';
import { UserInfoService } from 'src/app/modules/user/services/user-info.service';
import { LoginService } from '../../../login/services/login.service';
describe('SidebarComponent', () => {
let component: SidebarComponent;
let fixture: ComponentFixture<SidebarComponent>;
let azureAdB2CServiceStubInjected;
let loginServiceStubInjected: LoginService;
let userInfoService: UserInfoService;
let router;
const routes: Routes = [{ path: 'time-clock', component: TimeClockComponent }];
const azureAdB2CServiceStub = {
isLogin() {
return true;
},
isAdmin() {
return true;
},
logout(){
return true;
}
};
const loginServiceStub = {
isLogin() {
return true;
},
isAdmin() {
return true;
},
logout(){
return true;
}
};
const userInfoServiceStub = {
isAdmin: () => of(true),
};
beforeEach(
waitForAsync(() => {
TestBed.configureTestingModule({
declarations: [SidebarComponent],
providers: [
{ provide: AzureAdB2CService, useValue: azureAdB2CServiceStub },
{ provide: UserInfoService, useValue: userInfoServiceStub },
{ provide: LoginService, useValue: loginServiceStub },
],
imports: [RouterTestingModule.withRoutes(routes)],
}).compileComponents();
router = TestBed.inject(Router);
})
);
beforeEach(() => {
fixture = TestBed.createComponent(SidebarComponent);
azureAdB2CServiceStubInjected = TestBed.inject(AzureAdB2CService);
loginServiceStubInjected = TestBed.inject(LoginService);
userInfoService = TestBed.inject(UserInfoService);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('component should be created', () => {
component.isProduction = true;
spyOn(azureAdB2CServiceStubInjected, 'isAdmin').and.returnValue(false);
expect(component).toBeTruthy();
});
it('admin users should have six menu items', () => {
component.getSidebarItems().subscribe(() => {
const menuItems = component.itemsSidebar;
expect(menuItems.length).toBe(6);
});
});
it('non admin users should have two menu items', () => {
spyOn(userInfoServiceStub, 'isAdmin').and.returnValue(of(false));
component.getSidebarItems().subscribe(() => {
const menuItems = component.itemsSidebar;
expect(menuItems.length).toBe(2);
});
});
it('when item is selected should be set as active and the others as inactive', () => {
const route = 'time-clock';
router.navigate([route]);
component.itemsSidebar.filter((item) => item.route === `/${route}`).map((item) => {
expect(item.active).toBeTrue();
});
component.itemsSidebar.filter((item) => item.route !== `/${route}`).map((item) => {
expect(item.active).toBeFalse();
});
});
it('should toggle the sidebar', () => {
component.toggleSideBar();
fixture.detectChanges();
const sidebarElement = fixture.debugElement.nativeElement.querySelector('#wrapper');
expect(sidebarElement.classList.contains('toggled')).toBeTrue();
});
it('should use the Azure service on logout', () => {
component.isProduction = true;
spyOn(azureAdB2CServiceStubInjected, 'logout');
component.logout();
expect(azureAdB2CServiceStubInjected.logout).toHaveBeenCalled();
});
it('should use the Login service on logout Locally', () => {
component.isProduction = false;
spyOn(loginServiceStubInjected, 'logout');
component.logout();
expect(loginServiceStubInjected.logout).toHaveBeenCalled();
});
});