1
+ import { FeatureManagerService } from 'src/app/modules/shared/feature-toggles/feature-toggle-manager.service' ;
1
2
import { waitForAsync , ComponentFixture , TestBed } from '@angular/core/testing' ;
2
3
import { MockStore , provideMockStore } from '@ngrx/store/testing' ;
3
-
4
4
import { NgxPaginationModule } from 'ngx-pagination' ;
5
5
import { UsersListComponent } from './users-list.component' ;
6
- import { UserActionTypes , UserState , LoadUsers , GrantRoleUser , RevokeRoleUser } from '../../store' ;
6
+ import {
7
+ UserActionTypes ,
8
+ UserState ,
9
+ LoadUsers ,
10
+ GrantRoleUser ,
11
+ RevokeRoleUser ,
12
+ AddUserToGroup ,
13
+ RemoveUserFromGroup ,
14
+ } from '../../store' ;
7
15
import { ActionsSubject } from '@ngrx/store' ;
8
16
import { DataTablesModule } from 'angular-datatables' ;
17
+ import { Observable , of } from 'rxjs' ;
18
+ import { FeatureToggleProvider } from 'src/app/modules/shared/feature-toggles/feature-toggle-provider.service' ;
19
+ import { AppConfigurationClient } from '@azure/app-configuration' ;
20
+ import { FeatureFilterProvider } from '../../../shared/feature-toggles/filters/feature-filter-provider.service' ;
21
+ import { AzureAdB2CService } from '../../../login/services/azure.ad.b2c.service' ;
9
22
10
23
describe ( 'UsersListComponent' , ( ) => {
11
24
let component : UsersListComponent ;
12
25
let fixture : ComponentFixture < UsersListComponent > ;
13
26
let store : MockStore < UserState > ;
14
27
const actionSub : ActionsSubject = new ActionsSubject ( ) ;
28
+ const fakeAppConfigurationConnectionString = 'Endpoint=http://fake.foo;Id=fake.id;Secret=fake.secret' ;
29
+ let service : FeatureManagerService ;
30
+ let fakeFeatureToggleProvider ;
15
31
16
32
const state : UserState = {
17
33
data : [
18
34
{
19
35
name : 'name' ,
20
36
email : 'email' ,
21
37
roles : [ 'admin' , 'test' ] ,
38
+ groups : [ 'time-tracker-admin' , 'time-tracker-tester' ] ,
22
39
id : 'id' ,
23
40
tenant_id : 'tenant id' ,
24
41
deleted : 'delete' ,
@@ -30,10 +47,20 @@ describe('UsersListComponent', () => {
30
47
31
48
beforeEach (
32
49
waitForAsync ( ( ) => {
50
+ fakeFeatureToggleProvider = new FeatureToggleProvider (
51
+ new AppConfigurationClient ( fakeAppConfigurationConnectionString ) ,
52
+ new FeatureFilterProvider ( new AzureAdB2CService ( ) )
53
+ ) ;
54
+ service = new FeatureManagerService ( fakeFeatureToggleProvider ) ;
55
+
33
56
TestBed . configureTestingModule ( {
34
57
imports : [ NgxPaginationModule , DataTablesModule ] ,
35
58
declarations : [ UsersListComponent ] ,
36
- providers : [ provideMockStore ( { initialState : state } ) , { provide : ActionsSubject , useValue : actionSub } ] ,
59
+ providers : [
60
+ provideMockStore ( { initialState : state } ) ,
61
+ { provide : ActionsSubject , useValue : actionSub } ,
62
+ { provide : FeatureManagerService , useValue : service }
63
+ ] ,
37
64
} ) . compileComponents ( ) ;
38
65
} )
39
66
) ;
@@ -91,6 +118,27 @@ describe('UsersListComponent', () => {
91
118
} ) ;
92
119
} ) ;
93
120
121
+ const actionsGroupsParams = [
122
+ { actionType : UserActionTypes . ADD_USER_TO_GROUP_SUCCESS } ,
123
+ { actionType : UserActionTypes . REMOVE_USER_FROM_GROUP_SUCCESS } ,
124
+ ] ;
125
+
126
+ actionsGroupsParams . map ( ( param ) => {
127
+ it ( `When action ${ param . actionType } is dispatched should triggered load Users action` , ( ) => {
128
+ spyOn ( store , 'dispatch' ) ;
129
+
130
+ const actionSubject = TestBed . inject ( ActionsSubject ) as ActionsSubject ;
131
+ const action = {
132
+ type : param . actionType ,
133
+ payload : state . data ,
134
+ } ;
135
+
136
+ actionSubject . next ( action ) ;
137
+
138
+ expect ( store . dispatch ) . toHaveBeenCalledWith ( new LoadUsers ( ) ) ;
139
+ } ) ;
140
+ } ) ;
141
+
94
142
const grantRoleTypes = [
95
143
{ roleId : 'admin' , roleValue : 'time-tracker-admin' } ,
96
144
{ roleId : 'test' , roleValue : 'time-tracker-tester' } ,
@@ -111,6 +159,25 @@ describe('UsersListComponent', () => {
111
159
} ) ;
112
160
} ) ;
113
161
162
+ const AddGroupTypes = [
163
+ { groupName : 'time-tracker-admin' } ,
164
+ { groupName : 'time-tracker-tester' }
165
+ ] ;
166
+
167
+ AddGroupTypes . map ( ( param ) => {
168
+ it ( `When user switchGroup to ${ param . groupName } and doesn't belong to any group, should add ${ param . groupName } group to user` , ( ) => {
169
+ const groupName = param . groupName ;
170
+ const userGroups = [ ] ;
171
+ const userId = 'userId' ;
172
+
173
+ spyOn ( store , 'dispatch' ) ;
174
+
175
+ component . switchGroup ( userId , userGroups , groupName ) ;
176
+
177
+ expect ( store . dispatch ) . toHaveBeenCalledWith ( new AddUserToGroup ( userId , groupName ) ) ;
178
+ } ) ;
179
+ } ) ;
180
+
114
181
const revokeRoleTypes = [
115
182
{ roleId : 'admin' , roleValue : 'time-tracker-admin' , userRoles : [ 'time-tracker-admin' ] } ,
116
183
{ roleId : 'test' , roleValue : 'time-tracker-tester' , userRoles : [ 'time-tracker-tester' ] } ,
@@ -131,6 +198,25 @@ describe('UsersListComponent', () => {
131
198
} ) ;
132
199
} ) ;
133
200
201
+ const removeGroupTypes = [
202
+ { groupName : 'time-tracker-admin' , userGroups : [ 'time-tracker-admin' ] } ,
203
+ { groupName : 'time-tracker-tester' , userGroups : [ 'time-tracker-tester' ] } ,
204
+ ] ;
205
+
206
+ removeGroupTypes . map ( ( param ) => {
207
+ it ( `When user switchGroup to ${ param . groupName } and belongs to group, should remove ${ param . groupName } group from user` , ( ) => {
208
+ const groupName = param . groupName ;
209
+ const userGroups = param . userGroups ;
210
+ const userId = 'userId' ;
211
+
212
+ spyOn ( store , 'dispatch' ) ;
213
+
214
+ component . switchGroup ( userId , userGroups , groupName ) ;
215
+
216
+ expect ( store . dispatch ) . toHaveBeenCalledWith ( new RemoveUserFromGroup ( userId , groupName ) ) ;
217
+ } ) ;
218
+ } ) ;
219
+
134
220
it ( 'on success load users, the data of roles should be an array' , ( ) => {
135
221
const actionSubject = TestBed . inject ( ActionsSubject ) as ActionsSubject ;
136
222
const action = {
@@ -145,6 +231,20 @@ describe('UsersListComponent', () => {
145
231
} ) ;
146
232
} ) ;
147
233
234
+ it ( 'on success load users, the data of groups should be an array' , ( ) => {
235
+ const actionSubject = TestBed . inject ( ActionsSubject ) as ActionsSubject ;
236
+ const action = {
237
+ type : UserActionTypes . LOAD_USERS_SUCCESS ,
238
+ payload : state . data ,
239
+ } ;
240
+
241
+ actionSubject . next ( action ) ;
242
+
243
+ component . users . map ( ( user ) => {
244
+ expect ( user . groups ) . toEqual ( [ 'time-tracker-admin' , 'time-tracker-tester' ] ) ;
245
+ } ) ;
246
+ } ) ;
247
+
148
248
it ( 'on success load users, the datatable should be reloaded' , async ( ) => {
149
249
const actionSubject = TestBed . inject ( ActionsSubject ) ;
150
250
const action = {
@@ -158,6 +258,27 @@ describe('UsersListComponent', () => {
158
258
expect ( component . dtElement . dtInstance . then ) . toHaveBeenCalled ( ) ;
159
259
} ) ;
160
260
261
+ it ( 'When Component is created, should call the feature toggle method' , ( ) => {
262
+ spyOn ( component , 'isFeatureToggleActivated' ) . and . returnValue ( of ( true ) ) ;
263
+
264
+ component . ngOnInit ( ) ;
265
+
266
+ expect ( component . isFeatureToggleActivated ) . toHaveBeenCalled ( ) ;
267
+ expect ( component . isUserGroupsToggleOn ) . toBe ( true ) ;
268
+ } ) ;
269
+
270
+ const toggleValues = [ true , false ] ;
271
+ toggleValues . map ( ( toggleValue ) => {
272
+ it ( `when FeatureToggle is ${ toggleValue } should return ${ toggleValue } ` , ( ) => {
273
+ spyOn ( service , 'isToggleEnabledForUser' ) . and . returnValue ( of ( toggleValue ) ) ;
274
+
275
+ const isFeatureToggleActivated : Observable < boolean > = component . isFeatureToggleActivated ( ) ;
276
+
277
+ expect ( service . isToggleEnabledForUser ) . toHaveBeenCalled ( ) ;
278
+ isFeatureToggleActivated . subscribe ( ( value ) => expect ( value ) . toEqual ( toggleValue ) ) ;
279
+ } ) ;
280
+ } ) ;
281
+
161
282
afterEach ( ( ) => {
162
283
component . dtTrigger . unsubscribe ( ) ;
163
284
component . loadUsersSubscription . unsubscribe ( ) ;
0 commit comments