Skip to content

Commit d6b84b2

Browse files
thegreatyamoriAngeluz-07
authored andcommitted
test: TT-155 added unit tests to user-info.service
1 parent 2e67284 commit d6b84b2

File tree

3 files changed

+68
-14
lines changed

3 files changed

+68
-14
lines changed
Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,60 @@
11
import { TestBed } from '@angular/core/testing';
2-
2+
import { MockStore, provideMockStore } from '@ngrx/store/testing';
3+
import { of } from 'rxjs';
4+
import { getUserGroups } from '../store/user.selectors';
35
import { UserInfoService } from './user-info.service';
46

57
describe('UserInfoService', () => {
68
let service: UserInfoService;
9+
let store: MockStore;
10+
let mockGetUserGroupsSelector: any;
11+
const initialState = {
12+
name: 'Unknown Name',
13+
14+
roles: [],
15+
groups: ['fake-admin', 'fake-tester'],
16+
id: 'dummy_id_load',
17+
tenant_id: 'dummy_tenant_id_load',
18+
deleted: '',
19+
};
720

821
beforeEach(() => {
9-
TestBed.configureTestingModule({});
22+
TestBed.configureTestingModule({
23+
providers: [provideMockStore({ initialState })],
24+
});
1025
service = TestBed.inject(UserInfoService);
26+
store = TestBed.inject(MockStore);
27+
mockGetUserGroupsSelector = store.overrideSelector(getUserGroups, initialState.groups);
1128
});
1229

1330
it('should be created', () => {
1431
expect(service).toBeTruthy();
1532
});
1633

17-
it('should verify if an user belongs to a certain group in the UI', () => {
18-
const input = 'time-tracker-admin';
34+
it('should call groups selector', () => {
35+
const expectedGroups = ['fake-admin', 'fake-tester'];
36+
37+
service.groups().subscribe((value) => {
38+
expect(value).toEqual(expectedGroups);
39+
});
40+
});
41+
42+
const params = [
43+
{ groupName: 'fake-admin', expectedValue: true, groups: ['fake-admin', 'fake-tester'] },
44+
{ groupName: 'fake-owner', expectedValue: false, groups: ['fake-admin', 'fake-tester'] },
45+
];
46+
47+
params.map((param) => {
48+
it(`given group ${param.groupName} and groups [${param.groups.toString()}], isMemberOf() should return ${
49+
param.expectedValue
50+
}`, () => {
51+
const groups$ = of(param.groups);
52+
53+
spyOn(service, 'groups').and.returnValue(groups$);
1954

20-
expect(service.verifyGroup(input)).toBeTruthy();
55+
service.isMemberOf(param.groupName).subscribe((value) => {
56+
expect(value).toEqual(param.expectedValue);
57+
});
58+
});
2159
});
2260
});
Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,33 @@
11
import { Injectable } from '@angular/core';
22
import { select, Store } from '@ngrx/store';
3-
import { getUserGroupsInfo } from '../store/user.selectors';
3+
import { Observable } from 'rxjs';
4+
import { map } from 'rxjs/operators';
5+
import { getUserGroups } from '../store/user.selectors';
6+
import { GROUPS } from '../../../../environments/environment';
47

58
@Injectable({
6-
providedIn: 'root'
9+
providedIn: 'root',
710
})
811
export class UserInfoService {
12+
constructor(private store: Store) {}
913

10-
constructor(private store: Store) { }
14+
groups(): Observable<string[]> {
15+
return this.store.pipe(select(getUserGroups));
16+
}
1117

12-
verifyGroup(groupName: string): boolean {
13-
let isGroupBelongsToUser: boolean;
18+
isMemberOf(groupName: string): Observable<boolean> {
19+
return this.groups().pipe(
20+
map((groups: string[]) => {
21+
return groups.includes(groupName);
22+
})
23+
);
24+
}
1425

15-
const groupsStored = this.store.pipe(select(getUserGroupsInfo)).subscribe((groups) => {
16-
isGroupBelongsToUser = groups.includes(groupName);
17-
});
26+
isAdmin(): Observable<boolean> {
27+
return this.isMemberOf(GROUPS.ADMIN);
28+
}
1829

19-
return isGroupBelongsToUser;
30+
isTester(): Observable<boolean> {
31+
return this.isMemberOf(GROUPS.TESTER);
2032
}
2133
}

src/environments/environment.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ export const STACK_EXCHANGE_ACCESS_TOKEN = keys.STACK_EXCHANGE_ACCESS_TOKEN;
1818
export const AZURE_APP_CONFIGURATION_CONNECTION_STRING = keys.AZURE_APP_CONFIGURATION_CONNECTION_STRING;
1919
export const DATE_FORMAT = 'yyyy-MM-dd';
2020
export const DATE_FORMAT_YEAR = 'YYYY-MM-DD';
21+
export const GROUPS = {
22+
ADMIN: 'time-tracker-admin',
23+
TESTER: 'time-tracker-tester',
24+
};
2125

2226
/*
2327
* For easier debugging in development mode, you can import the following file

0 commit comments

Comments
 (0)