/
permission.component.spec.ts
149 lines (132 loc) · 5.09 KB
/
permission.component.spec.ts
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { Pipe, PipeTransform } from '@angular/core';
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { MatDialogModule } from '@angular/material/dialog';
import { MatIconModule } from '@angular/material/icon';
import { MatSnackBarModule } from '@angular/material/snack-bar';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { ActivatedRoute } from '@angular/router';
import { RouterTestingModule } from '@angular/router/testing';
import { KnoraApiConnection, MockProjects, ProjectResponse, ReadProject } from '@dasch-swiss/dsp-js';
import { of } from 'rxjs';
import { AppInitService } from 'src/app/app-init.service';
import { CacheService } from 'src/app/main/cache/cache.service';
import { DspApiConfigToken, DspApiConnectionToken } from 'src/app/main/declarations/dsp-api-tokens';
import { DialogComponent } from 'src/app/main/dialog/dialog.component';
import { StatusComponent } from 'src/app/main/status/status.component';
import { GroupsListComponent } from 'src/app/system/groups/groups-list/groups-list.component';
import { TestConfig } from 'test.config';
import { AddGroupComponent } from './add-group/add-group.component';
import { PermissionComponent } from './permission.component';
/**
* mocked linkify pipe from main/pipes.
*/
@Pipe({ name: 'appLinkify' })
class MockPipe implements PipeTransform {
transform(value: string): string {
// do stuff here, if you want
return value;
}
}
describe('PermissionComponent', () => {
let component: PermissionComponent;
let fixture: ComponentFixture<PermissionComponent>;
beforeEach(waitForAsync(() => {
const cacheServiceSpy = jasmine.createSpyObj('CacheService', ['get', 'set']);
TestBed.configureTestingModule({
declarations: [
PermissionComponent,
AddGroupComponent,
GroupsListComponent,
DialogComponent,
StatusComponent,
MockPipe
],
imports: [
BrowserAnimationsModule,
MatDialogModule,
MatIconModule,
MatSnackBarModule,
HttpClientTestingModule,
RouterTestingModule
],
providers: [
{
provide: ActivatedRoute,
useValue: {
parent: {
paramMap: of({
get: (param: string) => {
if (param === 'shortcode') {
return TestConfig.ProjectCode;
}
}
}),
parent: {
snapshot: {
url: []
}
}
}
}
},
AppInitService,
{
provide: DspApiConfigToken,
useValue: TestConfig.ApiConfig
},
{
provide: DspApiConnectionToken,
useValue: new KnoraApiConnection(TestConfig.ApiConfig)
},
{
provide: CacheService,
useValue: cacheServiceSpy
}
]
})
.compileComponents();
}));
beforeEach(() => {
// mock cache service
const cacheSpy = TestBed.inject(CacheService);
(cacheSpy as jasmine.SpyObj<CacheService>).get.and.callFake(
() => {
const response: ProjectResponse = new ProjectResponse();
const mockProjects = MockProjects.mockProjects();
response.project = mockProjects.body.projects[0];
return of(response.project as ReadProject);
}
);
});
// mock localStorage
beforeEach(() => {
let store = {};
spyOn(localStorage, 'getItem').and.callFake(
(key: string): string => store[key] || null
);
spyOn(localStorage, 'removeItem').and.callFake(
(key: string): void => {
delete store[key];
}
);
spyOn(localStorage, 'setItem').and.callFake(
(key: string, value: string): string => (store[key] = <any>value)
);
spyOn(localStorage, 'clear').and.callFake(() => {
store = {};
});
});
beforeEach(() => {
localStorage.setItem('session', JSON.stringify(TestConfig.CurrentSession));
fixture = TestBed.createComponent(PermissionComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect<any>(localStorage.getItem('session')).toBe(
JSON.stringify(TestConfig.CurrentSession)
);
expect(component).toBeTruthy();
});
});