1+ import { FeatureManagerService } from 'src/app/modules/shared/feature-toggles/feature-toggle-manager.service' ;
12import { waitForAsync , ComponentFixture , TestBed } from '@angular/core/testing' ;
23import { MockStore , provideMockStore } from '@ngrx/store/testing' ;
3-
44import { NgxPaginationModule } from 'ngx-pagination' ;
55import { 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' ;
715import { ActionsSubject } from '@ngrx/store' ;
816import { 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' ;
922
1023describe ( 'UsersListComponent' , ( ) => {
1124 let component : UsersListComponent ;
1225 let fixture : ComponentFixture < UsersListComponent > ;
1326 let store : MockStore < UserState > ;
1427 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 ;
1531
1632 const state : UserState = {
1733 data : [
1834 {
1935 name : 'name' ,
2036 email : 'email' ,
2137 roles : [ 'admin' , 'test' ] ,
38+ groups : [ 'time-tracker-admin' , 'time-tracker-tester' ] ,
2239 id : 'id' ,
2340 tenant_id : 'tenant id' ,
2441 deleted : 'delete' ,
@@ -30,10 +47,20 @@ describe('UsersListComponent', () => {
3047
3148 beforeEach (
3249 waitForAsync ( ( ) => {
50+ fakeFeatureToggleProvider = new FeatureToggleProvider (
51+ new AppConfigurationClient ( fakeAppConfigurationConnectionString ) ,
52+ new FeatureFilterProvider ( new AzureAdB2CService ( ) )
53+ ) ;
54+ service = new FeatureManagerService ( fakeFeatureToggleProvider ) ;
55+
3356 TestBed . configureTestingModule ( {
3457 imports : [ NgxPaginationModule , DataTablesModule ] ,
3558 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+ ] ,
3764 } ) . compileComponents ( ) ;
3865 } )
3966 ) ;
@@ -91,6 +118,27 @@ describe('UsersListComponent', () => {
91118 } ) ;
92119 } ) ;
93120
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+
94142 const grantRoleTypes = [
95143 { roleId : 'admin' , roleValue : 'time-tracker-admin' } ,
96144 { roleId : 'test' , roleValue : 'time-tracker-tester' } ,
@@ -111,6 +159,25 @@ describe('UsersListComponent', () => {
111159 } ) ;
112160 } ) ;
113161
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+
114181 const revokeRoleTypes = [
115182 { roleId : 'admin' , roleValue : 'time-tracker-admin' , userRoles : [ 'time-tracker-admin' ] } ,
116183 { roleId : 'test' , roleValue : 'time-tracker-tester' , userRoles : [ 'time-tracker-tester' ] } ,
@@ -131,6 +198,25 @@ describe('UsersListComponent', () => {
131198 } ) ;
132199 } ) ;
133200
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+
134220 it ( 'on success load users, the data of roles should be an array' , ( ) => {
135221 const actionSubject = TestBed . inject ( ActionsSubject ) as ActionsSubject ;
136222 const action = {
@@ -145,6 +231,20 @@ describe('UsersListComponent', () => {
145231 } ) ;
146232 } ) ;
147233
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+
148248 it ( 'on success load users, the datatable should be reloaded' , async ( ) => {
149249 const actionSubject = TestBed . inject ( ActionsSubject ) ;
150250 const action = {
@@ -158,6 +258,27 @@ describe('UsersListComponent', () => {
158258 expect ( component . dtElement . dtInstance . then ) . toHaveBeenCalled ( ) ;
159259 } ) ;
160260
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+
161282 afterEach ( ( ) => {
162283 component . dtTrigger . unsubscribe ( ) ;
163284 component . loadUsersSubscription . unsubscribe ( ) ;
0 commit comments