Skip to content

Commit c3a1047

Browse files
committed
feat: TT-190 test
1 parent acf79d6 commit c3a1047

File tree

3 files changed

+109
-21
lines changed

3 files changed

+109
-21
lines changed

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

Lines changed: 1 addition & 1 deletion
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">Roles</th>
13+
<th class="col-3">{{ isUserGroupsToggleOn ? "Groups" : "Roles" }}</th>
1414
</tr>
1515
</thead>
1616
<app-loading-bar *ngIf="isLoading$ | async"></app-loading-bar>

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

Lines changed: 105 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,43 @@
11
import { HttpClient } from '@angular/common/http';
22
import { FeatureManagerService } from 'src/app/modules/shared/feature-toggles/feature-toggle-manager.service';
3-
import { waitForAsync, ComponentFixture, TestBed } from '@angular/core/testing';
3+
import { waitForAsync, ComponentFixture, TestBed, inject } from '@angular/core/testing';
44
import { MockStore, provideMockStore } from '@ngrx/store/testing';
5-
65
import { NgxPaginationModule } from 'ngx-pagination';
76
import { UsersListComponent } from './users-list.component';
8-
import { UserActionTypes, UserState, LoadUsers, GrantRoleUser, RevokeRoleUser } from '../../store';
7+
import {
8+
UserActionTypes,
9+
UserState,
10+
LoadUsers,
11+
GrantRoleUser,
12+
RevokeRoleUser,
13+
AddUserToGroup,
14+
RemoveUserFromGroup,
15+
} from '../../store';
916
import { ActionsSubject } from '@ngrx/store';
1017
import { DataTablesModule } from 'angular-datatables';
1118
import { 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';
23+
import { FeatureToggleModel } from 'src/app/modules/shared/feature-toggles/feature-toggle.model';
1224

1325
describe('UsersListComponent', () => {
1426
let component: UsersListComponent;
1527
let fixture: ComponentFixture<UsersListComponent>;
1628
let store: MockStore<UserState>;
1729
const actionSub: ActionsSubject = new ActionsSubject();
18-
let featureManagerService: FeatureManagerService;
30+
const fakeAppConfigurationConnectionString = 'Endpoint=http://fake.foo;Id=fake.id;Secret=fake.secret';
31+
let service: FeatureManagerService;
32+
let fakeFeatureToggleProvider;
1933

2034
const state: UserState = {
2135
data: [
2236
{
2337
name: 'name',
2438
email: 'email',
2539
roles: ['admin', 'test'],
40+
groups: ['time-tracker-admin', 'time-tracker-tester'],
2641
id: 'id',
2742
tenant_id: 'tenant id',
2843
deleted: 'delete',
@@ -34,12 +49,21 @@ describe('UsersListComponent', () => {
3449

3550
beforeEach(
3651
waitForAsync(() => {
52+
53+
fakeFeatureToggleProvider = new FeatureToggleProvider(
54+
new AppConfigurationClient(fakeAppConfigurationConnectionString),
55+
new FeatureFilterProvider(new AzureAdB2CService())
56+
);
57+
//spyOn(fakeFeatureToggleProvider, 'getFeatureToggle').and.returnValue(of(aFeatureToggle));
58+
service = new FeatureManagerService(fakeFeatureToggleProvider);
59+
3760
TestBed.configureTestingModule({
3861
imports: [NgxPaginationModule, DataTablesModule],
3962
declarations: [UsersListComponent],
40-
providers: [provideMockStore({ initialState: state }), { provide: ActionsSubject, useValue: actionSub }],
63+
providers: [provideMockStore({ initialState: state }),
64+
{ provide: ActionsSubject, useValue: actionSub },
65+
{ provide: FeatureManagerService, useValue: service}],
4166
}).compileComponents();
42-
featureManagerService = TestBed.inject(FeatureManagerService);
4367
})
4468
);
4569

@@ -96,6 +120,27 @@ describe('UsersListComponent', () => {
96120
});
97121
});
98122

123+
const actionsGroupsParams = [
124+
{ actionType: UserActionTypes.ADD_USER_TO_GROUP_SUCCESS },
125+
{ actionType: UserActionTypes.REMOVE_USER_FROM_GROUP_SUCCESS },
126+
];
127+
128+
actionsGroupsParams.map((param) => {
129+
it(`When action ${param.actionType} is dispatched should triggered load Users action`, () => {
130+
spyOn(store, 'dispatch');
131+
132+
const actionSubject = TestBed.inject(ActionsSubject) as ActionsSubject;
133+
const action = {
134+
type: param.actionType,
135+
payload: state.data,
136+
};
137+
138+
actionSubject.next(action);
139+
140+
expect(store.dispatch).toHaveBeenCalledWith(new LoadUsers());
141+
});
142+
});
143+
99144
const grantRoleTypes = [
100145
{ roleId: 'admin', roleValue: 'time-tracker-admin' },
101146
{ roleId: 'test', roleValue: 'time-tracker-tester' },
@@ -116,6 +161,25 @@ describe('UsersListComponent', () => {
116161
});
117162
});
118163

164+
const AddGroupTypes = [
165+
{ groupName: 'time-tracker-admin' },
166+
{ groupName: 'time-tracker-tester' }
167+
];
168+
169+
AddGroupTypes.map((param) => {
170+
it(`When user switchGroup to ${param.groupName} and doesn't belong to any group, should add ${param.groupName} group to user`, () => {
171+
const groupName = param.groupName;
172+
const userGroups = [];
173+
const userId = 'userId';
174+
175+
spyOn(store, 'dispatch');
176+
177+
component.switchGroup(userId, userGroups, groupName);
178+
179+
expect(store.dispatch).toHaveBeenCalledWith(new AddUserToGroup(userId, groupName));
180+
});
181+
});
182+
119183
const revokeRoleTypes = [
120184
{ roleId: 'admin', roleValue: 'time-tracker-admin', userRoles: ['time-tracker-admin'] },
121185
{ roleId: 'test', roleValue: 'time-tracker-tester', userRoles: ['time-tracker-tester'] },
@@ -136,6 +200,25 @@ describe('UsersListComponent', () => {
136200
});
137201
});
138202

203+
const removeGroupTypes = [
204+
{ groupName: 'time-tracker-admin', userGroups: ['time-tracker-admin'] },
205+
{ groupName: 'time-tracker-tester', userGroups: ['time-tracker-tester'] },
206+
];
207+
208+
removeGroupTypes.map((param) => {
209+
it(`When user switchGroup to ${param.groupName} and belongs to group, should remove ${param.groupName} group to user`, () => {
210+
const groupName = param.groupName;
211+
const userGroups = param.userGroups;
212+
const userId = 'userId';
213+
214+
spyOn(store, 'dispatch');
215+
216+
component.switchGroup(userId, userGroups, groupName);
217+
218+
expect(store.dispatch).toHaveBeenCalledWith(new RemoveUserFromGroup(userId, groupName));
219+
});
220+
});
221+
139222
it('on success load users, the data of roles should be an array', () => {
140223
const actionSubject = TestBed.inject(ActionsSubject) as ActionsSubject;
141224
const action = {
@@ -150,7 +233,21 @@ describe('UsersListComponent', () => {
150233
});
151234
});
152235

153-
it('on success load users, the datatable should be reloaded', async () => {
236+
fit('on success load users, the data of groups 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.groups).toEqual(['time-tracker-admin', 'time-tracker-tester']);
247+
});
248+
});
249+
250+
fit('on success load users, the datatable should be reloaded', async () => {
154251
const actionSubject = TestBed.inject(ActionsSubject);
155252
const action = {
156253
type: UserActionTypes.LOAD_USERS_SUCCESS,
@@ -163,16 +260,8 @@ describe('UsersListComponent', () => {
163260
expect(component.dtElement.dtInstance.then).toHaveBeenCalled();
164261
});
165262

166-
167263
fit('When Component is created, should call the feature toggle method', () => {
168-
169-
// spyOn(service, 'getAll').and.callFake( () => {
170-
// return of(mockUser);
171-
// });
172-
173-
spyOn(component, 'isFeatureToggleActivated').and.callFake(() => {
174-
return of(true);
175-
});
264+
spyOn(component, 'isFeatureToggleActivated').and.returnValue(of(true));
176265

177266
component.ngOnInit();
178267

@@ -185,5 +274,4 @@ describe('UsersListComponent', () => {
185274
component.loadUsersSubscription.unsubscribe();
186275
fixture.destroy();
187276
});
188-
189277
});

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
RevokeRoleUser,
1212
UserActionTypes,
1313
AddUserToGroup,
14-
RemoveUserToGroup,
14+
RemoveUserFromGroup,
1515
} from '../../store/user.actions';
1616
import { getIsLoading } from '../../store/user.selectors';
1717

@@ -59,7 +59,7 @@ export class UsersListComponent implements OnInit, OnDestroy, AfterViewInit {
5959
filter(
6060
(action: any) =>
6161
action.type === UserActionTypes.ADD_USER_TO_GROUP_SUCCESS ||
62-
action.type === UserActionTypes.REMOVE_USER_TO_GROUP_SUCCESS
62+
action.type === UserActionTypes.REMOVE_USER_FROM_GROUP_SUCCESS
6363
)
6464
)
6565
.subscribe((action) => {
@@ -109,7 +109,7 @@ export class UsersListComponent implements OnInit, OnDestroy, AfterViewInit {
109109

110110
switchGroup(userId: string, userGroups: string[], groupName: string) {
111111
userGroups.includes(groupName)
112-
? this.store.dispatch(new RemoveUserToGroup(userId, groupName))
112+
? this.store.dispatch(new RemoveUserFromGroup(userId, groupName))
113113
: this.store.dispatch(new AddUserToGroup(userId, groupName));
114114
}
115115

0 commit comments

Comments
 (0)