11import { HttpClient } from '@angular/common/http' ;
22import { 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' ;
44import { MockStore , provideMockStore } from '@ngrx/store/testing' ;
5-
65import { NgxPaginationModule } from 'ngx-pagination' ;
76import { 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' ;
916import { ActionsSubject } from '@ngrx/store' ;
1017import { DataTablesModule } from 'angular-datatables' ;
1118import { 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
1325describe ( '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} ) ;
0 commit comments