|
1 | 1 | 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'; |
3 | 5 | import { UserInfoService } from './user-info.service';
|
4 | 6 |
|
5 | 7 | describe('UserInfoService', () => {
|
6 | 8 | 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 | + }; |
7 | 20 |
|
8 | 21 | beforeEach(() => {
|
9 |
| - TestBed.configureTestingModule({}); |
| 22 | + TestBed.configureTestingModule({ |
| 23 | + providers: [provideMockStore({ initialState })], |
| 24 | + }); |
10 | 25 | service = TestBed.inject(UserInfoService);
|
| 26 | + store = TestBed.inject(MockStore); |
| 27 | + mockGetUserGroupsSelector = store.overrideSelector(getUserGroups, initialState.groups); |
11 | 28 | });
|
12 | 29 |
|
13 | 30 | it('should be created', () => {
|
14 | 31 | expect(service).toBeTruthy();
|
15 | 32 | });
|
16 | 33 |
|
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$); |
19 | 54 |
|
20 |
| - expect(service.verifyGroup(input)).toBeTruthy(); |
| 55 | + service.isMemberOf(param.groupName).subscribe((value) => { |
| 56 | + expect(value).toEqual(param.expectedValue); |
| 57 | + }); |
| 58 | + }); |
21 | 59 | });
|
22 | 60 | });
|
0 commit comments