Skip to content

Commit a2d388c

Browse files
fix: TT-229 remove code from users-list.component & user ngrx store
1 parent 1bbf92e commit a2d388c

File tree

9 files changed

+25
-475
lines changed

9 files changed

+25
-475
lines changed

src/app/modules/users/components/users-list/users-list.component.html

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<tr class="d-flex flex-wrap">
1111
<th class="col-4">User Email</th>
1212
<th class="col-5">Names</th>
13-
<th class="col-3">{{ isUserGroupsToggleOn ? 'Groups' : 'Roles' }}</th>
13+
<th class="col-3">Groups</th>
1414
</tr>
1515
</thead>
1616
<app-loading-bar *ngIf="isLoading$ | async"></app-loading-bar>
@@ -19,35 +19,18 @@
1919
<td class="col-4 text-break">{{ user.email }}</td>
2020
<td class="col-5 text-break">{{ user.name }}</td>
2121
<td class="col-3 text-center">
22-
<div *ngIf="isUserGroupsToggleOn">
23-
<ui-switch
24-
size="small"
25-
(change)="switchGroup('time-tracker-admin', user)"
26-
[checked]="user.groups.includes('time-tracker-admin')"
27-
></ui-switch>
28-
admin
29-
<ui-switch
30-
size="small"
31-
(change)="switchGroup('time-tracker-tester', user)"
32-
[checked]="user.groups.includes('time-tracker-tester')"
33-
></ui-switch>
34-
test
35-
</div>
36-
37-
<div *ngIf="!isUserGroupsToggleOn">
38-
<ui-switch
39-
size="small"
40-
(change)="switchRole(user.id, user.roles, 'admin', 'time-tracker-admin')"
41-
[checked]="user.roles.includes('time-tracker-admin')"
42-
></ui-switch>
43-
admin
44-
<ui-switch
45-
size="small"
46-
(change)="switchRole(user.id, user.roles, 'test', 'time-tracker-tester')"
47-
[checked]="user.roles.includes('time-tracker-tester')"
48-
></ui-switch>
49-
test
50-
</div>
22+
<ui-switch
23+
size="small"
24+
(change)="switchGroup('time-tracker-admin', user)"
25+
[checked]="user.groups.includes('time-tracker-admin')"
26+
></ui-switch>
27+
admin
28+
<ui-switch
29+
size="small"
30+
(change)="switchGroup('time-tracker-tester', user)"
31+
[checked]="user.groups.includes('time-tracker-tester')"
32+
></ui-switch>
33+
test
5134
</td>
5235
</tr>
5336
</tbody>

src/app/modules/users/components/users-list/users-list.component.spec.ts

Lines changed: 2 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { FeatureManagerService } from 'src/app/modules/shared/feature-toggles/feature-toggle-manager.service';
21
import { waitForAsync, ComponentFixture, TestBed } from '@angular/core/testing';
32
import { MockStore, provideMockStore } from '@ngrx/store/testing';
43
import { NgxPaginationModule } from 'ngx-pagination';
@@ -7,28 +6,17 @@ import {
76
UserActionTypes,
87
UserState,
98
LoadUsers,
10-
GrantRoleUser,
11-
RevokeRoleUser,
129
AddUserToGroup,
1310
RemoveUserFromGroup,
1411
} from '../../store';
15-
import { User } from '../../../user/models/user';
1612
import { ActionsSubject } from '@ngrx/store';
1713
import { DataTablesModule } from 'angular-datatables';
18-
import { Observable, of } from 'rxjs';
19-
import { FeatureToggleProvider } from 'src/app/modules/shared/feature-toggles/feature-toggle-provider.service';
20-
import { AppConfigurationClient } from '@azure/app-configuration';
21-
import { FeatureFilterProvider } from '../../../shared/feature-toggles/filters/feature-filter-provider.service';
22-
import { AzureAdB2CService } from '../../../login/services/azure.ad.b2c.service';
2314

2415
describe('UsersListComponent', () => {
2516
let component: UsersListComponent;
2617
let fixture: ComponentFixture<UsersListComponent>;
2718
let store: MockStore<UserState>;
2819
const actionSub: ActionsSubject = new ActionsSubject();
29-
const fakeAppConfigurationConnectionString = 'Endpoint=http://fake.foo;Id=fake.id;Secret=fake.secret';
30-
let service: FeatureManagerService;
31-
let fakeFeatureToggleProvider;
3220

3321
const state: UserState = {
3422
data: [
@@ -48,19 +36,12 @@ describe('UsersListComponent', () => {
4836

4937
beforeEach(
5038
waitForAsync(() => {
51-
fakeFeatureToggleProvider = new FeatureToggleProvider(
52-
new AppConfigurationClient(fakeAppConfigurationConnectionString),
53-
new FeatureFilterProvider(new AzureAdB2CService())
54-
);
55-
service = new FeatureManagerService(fakeFeatureToggleProvider);
56-
5739
TestBed.configureTestingModule({
5840
imports: [NgxPaginationModule, DataTablesModule],
5941
declarations: [UsersListComponent],
6042
providers: [
6143
provideMockStore({ initialState: state }),
6244
{ provide: ActionsSubject, useValue: actionSub },
63-
{ provide: FeatureManagerService, useValue: service }
6445
],
6546
}).compileComponents();
6647
})
@@ -98,27 +79,6 @@ describe('UsersListComponent', () => {
9879
expect(component.users).toEqual(state.data);
9980
});
10081

101-
const actionsParams = [
102-
{ actionType: UserActionTypes.GRANT_USER_ROLE_SUCCESS },
103-
{ actionType: UserActionTypes.REVOKE_USER_ROLE_SUCCESS },
104-
];
105-
106-
actionsParams.map((param) => {
107-
it(`When action ${param.actionType} is dispatched should triggered load Users action`, () => {
108-
spyOn(store, 'dispatch');
109-
110-
const actionSubject = TestBed.inject(ActionsSubject) as ActionsSubject;
111-
const action = {
112-
type: param.actionType,
113-
payload: state.data,
114-
};
115-
116-
actionSubject.next(action);
117-
118-
expect(store.dispatch).toHaveBeenCalledWith(new LoadUsers());
119-
});
120-
});
121-
12282
const actionGroupParams = [
12383
{ actionType: UserActionTypes.ADD_USER_TO_GROUP_SUCCESS },
12484
{ actionType: UserActionTypes.REMOVE_USER_FROM_GROUP_SUCCESS },
@@ -140,26 +100,6 @@ describe('UsersListComponent', () => {
140100
});
141101
});
142102

143-
const grantRoleTypes = [
144-
{ roleId: 'admin', roleValue: 'time-tracker-admin' },
145-
{ roleId: 'test', roleValue: 'time-tracker-tester' },
146-
];
147-
148-
grantRoleTypes.map((param) => {
149-
it(`When user switchRole to ${param.roleId} and don't have any role, should grant ${param.roleValue} Role`, () => {
150-
const roleId = param.roleId;
151-
const roleValue = param.roleValue;
152-
const userRoles = [];
153-
const userId = 'userId';
154-
155-
spyOn(store, 'dispatch');
156-
157-
component.switchRole(userId, userRoles, roleId, roleValue);
158-
159-
expect(store.dispatch).toHaveBeenCalledWith(new GrantRoleUser(userId, roleId));
160-
});
161-
});
162-
163103
const AddGroupTypes = [
164104
{ groupName: 'time-tracker-admin' },
165105
{ groupName: 'time-tracker-tester' }
@@ -176,7 +116,7 @@ describe('UsersListComponent', () => {
176116
id: 'id',
177117
tenant_id: 'tenant id',
178118
deleted: 'delete',
179-
} ;
119+
};
180120

181121
spyOn(store, 'dispatch');
182122

@@ -186,26 +126,6 @@ describe('UsersListComponent', () => {
186126
});
187127
});
188128

189-
const revokeRoleTypes = [
190-
{ roleId: 'admin', roleValue: 'time-tracker-admin', userRoles: ['time-tracker-admin'] },
191-
{ roleId: 'test', roleValue: 'time-tracker-tester', userRoles: ['time-tracker-tester'] },
192-
];
193-
194-
revokeRoleTypes.map((param) => {
195-
it(`When user switchRole to ${param.roleId} and have that rol asigned, should revoke ${param.roleValue} Role`, () => {
196-
const roleId = param.roleId;
197-
const roleValue = param.roleValue;
198-
const userRoles = param.userRoles;
199-
const userId = 'userId';
200-
201-
spyOn(store, 'dispatch');
202-
203-
component.switchRole(userId, userRoles, roleId, roleValue);
204-
205-
expect(store.dispatch).toHaveBeenCalledWith(new RevokeRoleUser(userId, roleId));
206-
});
207-
});
208-
209129
const removeGroupTypes = [
210130
{ groupName: 'time-tracker-admin', userGroups: ['time-tracker-admin'] },
211131
{ groupName: 'time-tracker-tester', userGroups: ['time-tracker-tester'] },
@@ -222,8 +142,7 @@ describe('UsersListComponent', () => {
222142
id: 'id',
223143
tenant_id: 'tenant id',
224144
deleted: 'delete',
225-
} ;
226-
145+
};
227146

228147
spyOn(store, 'dispatch');
229148

@@ -233,20 +152,6 @@ describe('UsersListComponent', () => {
233152
});
234153
});
235154

236-
it('on success load users, the data of roles should be an array', () => {
237-
const actionSubject = TestBed.inject(ActionsSubject) as ActionsSubject;
238-
const action = {
239-
type: UserActionTypes.LOAD_USERS_SUCCESS,
240-
payload: state.data,
241-
};
242-
243-
actionSubject.next(action);
244-
245-
component.users.map((user) => {
246-
expect(user.roles).toEqual(['admin', 'test']);
247-
});
248-
});
249-
250155
it('on success load users, the data of groups should be an array', () => {
251156
const actionSubject = TestBed.inject(ActionsSubject) as ActionsSubject;
252157
const action = {
@@ -274,27 +179,6 @@ describe('UsersListComponent', () => {
274179
expect(component.dtElement.dtInstance.then).toHaveBeenCalled();
275180
});
276181

277-
it('When Component is created, should call the feature toggle method', () => {
278-
spyOn(component, 'isFeatureToggleActivated').and.returnValue(of(true));
279-
280-
component.ngOnInit();
281-
282-
expect(component.isFeatureToggleActivated).toHaveBeenCalled();
283-
expect(component.isUserGroupsToggleOn).toBe(true);
284-
});
285-
286-
const toggleValues = [true, false];
287-
toggleValues.map((toggleValue) => {
288-
it(`when FeatureToggle is ${toggleValue} should return ${toggleValue}`, () => {
289-
spyOn(service, 'isToggleEnabledForUser').and.returnValue(of(toggleValue));
290-
291-
const isFeatureToggleActivated: Observable<boolean> = component.isFeatureToggleActivated();
292-
293-
expect(service.isToggleEnabledForUser).toHaveBeenCalled();
294-
isFeatureToggleActivated.subscribe((value) => expect(value).toEqual(toggleValue));
295-
});
296-
});
297-
298182
afterEach(() => {
299183
component.dtTrigger.unsubscribe();
300184
component.loadUsersSubscription.unsubscribe();

src/app/modules/users/components/users-list/users-list.component.ts

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@ import { AfterViewInit, Component, OnDestroy, OnInit, ViewChild } from '@angular
22
import { ActionsSubject, select, Store, Action } from '@ngrx/store';
33
import { DataTableDirective } from 'angular-datatables';
44
import { Observable, Subject, Subscription } from 'rxjs';
5-
import { delay, filter, map } from 'rxjs/operators';
6-
import { FeatureManagerService } from 'src/app/modules/shared/feature-toggles/feature-toggle-manager.service';
5+
import { delay, filter } from 'rxjs/operators';
76
import { User } from '../../models/users';
87
import {
9-
GrantRoleUser,
108
LoadUsers,
11-
RevokeRoleUser,
129
UserActionTypes,
1310
AddUserToGroup,
1411
RemoveUserFromGroup,
@@ -30,13 +27,10 @@ export class UsersListComponent implements OnInit, OnDestroy, AfterViewInit {
3027
dtElement: DataTableDirective;
3128
dtOptions: any = {};
3229
switchGroupsSubscription: Subscription;
33-
isEnableToggleSubscription: Subscription;
34-
isUserGroupsToggleOn: boolean;
3530

3631
constructor(
3732
private store: Store<User>,
3833
private actionsSubject$: ActionsSubject,
39-
private featureManagerService: FeatureManagerService
4034
) {
4135
this.isLoading$ = store.pipe(delay(0), select(getIsLoading));
4236
}
@@ -50,25 +44,9 @@ export class UsersListComponent implements OnInit, OnDestroy, AfterViewInit {
5044
this.rerenderDataTable();
5145
});
5246

53-
this.isEnableToggleSubscription = this.isFeatureToggleActivated().subscribe((flag) => {
54-
this.isUserGroupsToggleOn = flag;
55-
});
56-
5747
this.switchGroupsSubscription = this.filterUserGroup().subscribe((action) => {
5848
this.store.dispatch(new LoadUsers());
5949
});
60-
61-
this.switchRoleSubscription = this.actionsSubject$
62-
.pipe(
63-
filter(
64-
(action: any) =>
65-
action.type === UserActionTypes.GRANT_USER_ROLE_SUCCESS ||
66-
action.type === UserActionTypes.REVOKE_USER_ROLE_SUCCESS
67-
)
68-
)
69-
.subscribe((action) => {
70-
this.store.dispatch(new LoadUsers());
71-
});
7250
}
7351

7452
ngAfterViewInit(): void {
@@ -78,7 +56,6 @@ export class UsersListComponent implements OnInit, OnDestroy, AfterViewInit {
7856
ngOnDestroy() {
7957
this.loadUsersSubscription.unsubscribe();
8058
this.dtTrigger.unsubscribe();
81-
this.isEnableToggleSubscription.unsubscribe();
8259
}
8360

8461
private rerenderDataTable(): void {
@@ -92,12 +69,6 @@ export class UsersListComponent implements OnInit, OnDestroy, AfterViewInit {
9269
}
9370
}
9471

95-
switchRole(userId: string, userRoles: string[], roleId: string, roleValue: string) {
96-
userRoles.includes(roleValue)
97-
? this.store.dispatch(new RevokeRoleUser(userId, roleId))
98-
: this.store.dispatch(new GrantRoleUser(userId, roleId));
99-
}
100-
10172
switchGroup(groupName: string, user: User): void {
10273
this.store.dispatch(
10374
user.groups.includes(groupName)
@@ -106,11 +77,6 @@ export class UsersListComponent implements OnInit, OnDestroy, AfterViewInit {
10677
);
10778
}
10879

109-
isFeatureToggleActivated(): Observable<boolean> {
110-
return this.featureManagerService.isToggleEnabledForUser('switch-group')
111-
.pipe(map((enabled: boolean) => enabled));
112-
}
113-
11480
filterUserGroup(): Observable<Action> {
11581
return this.actionsSubject$.pipe(
11682
filter(

src/app/modules/users/store/user.actions.spec.ts

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,42 +17,6 @@ describe('UserActions', () => {
1717
expect(action.type).toEqual(actions.UserActionTypes.LOAD_USERS_FAIL);
1818
});
1919

20-
it('GrantRoleUser type is UserActionTypes.GRANT_USER_ROLE', () => {
21-
const UserId = 'UserId';
22-
const RoleId = 'RoleId';
23-
const action = new actions.GrantRoleUser(UserId, RoleId);
24-
expect(action.type).toEqual(actions.UserActionTypes.GRANT_USER_ROLE);
25-
});
26-
27-
it('GrantRoleUserSuccess type is UserActionTypes.GRANT_USER_ROLE_SUCCESS', () => {
28-
const payload: User = { id: 'id', email: 'email', name: 'name' };
29-
const action = new actions.GrantRoleUserSuccess(payload);
30-
expect(action.type).toEqual(actions.UserActionTypes.GRANT_USER_ROLE_SUCCESS);
31-
});
32-
33-
it('GrantRoleUserFail type is UserActionTypes.GRANT_USER_ROLE_FAIL', () => {
34-
const action = new actions.GrantRoleUserFail('error');
35-
expect(action.type).toEqual(actions.UserActionTypes.GRANT_USER_ROLE_FAIL);
36-
});
37-
38-
it('RevokeRoleUser type is UserActionTypes.REVOKE_USER_ROLE', () => {
39-
const UserId = 'UserId';
40-
const RoleId = 'RoleId';
41-
const action = new actions.RevokeRoleUser(UserId, RoleId);
42-
expect(action.type).toEqual(actions.UserActionTypes.REVOKE_USER_ROLE);
43-
});
44-
45-
it('RevokeRoleUserSuccess type is UserActionTypes.REVOKE_USER_ROLE_SUCCESS', () => {
46-
const payload: User = { id: 'id', email: 'email', name: 'name' };
47-
const action = new actions.RevokeRoleUserSuccess(payload);
48-
expect(action.type).toEqual(actions.UserActionTypes.REVOKE_USER_ROLE_SUCCESS);
49-
});
50-
51-
it('RevokeRoleUserFail type is UserActionTypes.REVOKE_USER_ROLE_FAIL', () => {
52-
const action = new actions.RevokeRoleUserFail('error');
53-
expect(action.type).toEqual(actions.UserActionTypes.REVOKE_USER_ROLE_FAIL);
54-
});
55-
5620
it('AddUserToGroup type is UserActionTypes.ADD_USER_TO_GROUP', () => {
5721
const userId = 'userId';
5822
const groupName = 'groupName';

0 commit comments

Comments
 (0)