@@ -3,9 +3,10 @@ import { MockStore, provideMockStore } from '@ngrx/store/testing';
33
44import { NgxPaginationModule } from 'ngx-pagination' ;
55import { UsersListComponent } from './users-list.component' ;
6- import { UserActionTypes , UserState , LoadUsers } from '../../store' ;
6+ import { UserActionTypes , UserState , LoadUsers , GrantRoleUser , RevokeRoleUser } from '../../store' ;
77import { ActionsSubject } from '@ngrx/store' ;
88import { DataTablesModule } from 'angular-datatables' ;
9+ import { of } from 'rxjs' ;
910
1011describe ( 'UsersListComponent' , ( ) => {
1112 let component : UsersListComponent ;
@@ -29,13 +30,15 @@ describe('UsersListComponent', () => {
2930 message : '' ,
3031 } ;
3132
32- beforeEach ( waitForAsync ( ( ) => {
33- TestBed . configureTestingModule ( {
34- imports : [ NgxPaginationModule , DataTablesModule ] ,
35- declarations : [ UsersListComponent ] ,
36- providers : [ provideMockStore ( { initialState : state } ) , { provide : ActionsSubject , useValue : actionSub } ] ,
37- } ) . compileComponents ( ) ;
38- } ) ) ;
33+ beforeEach (
34+ waitForAsync ( ( ) => {
35+ TestBed . configureTestingModule ( {
36+ imports : [ NgxPaginationModule , DataTablesModule ] ,
37+ declarations : [ UsersListComponent ] ,
38+ providers : [ provideMockStore ( { initialState : state } ) , { provide : ActionsSubject , useValue : actionSub } ] ,
39+ } ) . compileComponents ( ) ;
40+ } )
41+ ) ;
3942
4043 beforeEach ( ( ) => {
4144 fixture = TestBed . createComponent ( UsersListComponent ) ;
@@ -68,9 +71,76 @@ describe('UsersListComponent', () => {
6871
6972 expect ( component . users ) . toEqual ( state . data ) ;
7073 } ) ;
71- /*
72- TODO: block commented on purpose so that when the tests pass and the Feature toggle is removed,
73- the table will be rendered again with dtInstance and not with dtOptions
74+
75+ it ( 'When Component is created, should call the feature toggle method' , ( ) => {
76+ spyOn ( component , 'isFeatureToggleActivated' ) . and . returnValue ( of ( true ) ) ;
77+
78+ component . ngOnInit ( ) ;
79+
80+ expect ( component . isFeatureToggleActivated ) . toHaveBeenCalled ( ) ;
81+ expect ( component . isFlagOn ) . toBe ( true ) ;
82+ } ) ;
83+
84+ const actionsParams = [
85+ { actionType : UserActionTypes . GRANT_USER_ROLE_SUCCESS } ,
86+ { actionType : UserActionTypes . REVOKE_USER_ROLE_SUCCESS } ,
87+ ] ;
88+
89+ actionsParams . map ( ( param ) => {
90+ it ( `When action ${ param . actionType } is dispatched should triggered load Users action` , ( ) => {
91+ spyOn ( store , 'dispatch' ) ;
92+
93+ const actionSubject = TestBed . inject ( ActionsSubject ) as ActionsSubject ;
94+ const action = {
95+ type : param . actionType ,
96+ payload : state . data ,
97+ } ;
98+
99+ actionSubject . next ( action ) ;
100+
101+ expect ( store . dispatch ) . toHaveBeenCalledWith ( new LoadUsers ( ) ) ;
102+ } ) ;
103+ } ) ;
104+
105+ const grantRoleTypes = [
106+ { roleId : 'admin' , roleValue : 'time-tracker-admin' } ,
107+ { roleId : 'test' , roleValue : 'time-tracker-tester' } ,
108+ ] ;
109+
110+ grantRoleTypes . map ( ( param ) => {
111+ it ( `When user switchRole to ${ param . roleId } and don't have any role, should grant ${ param . roleValue } Role` , ( ) => {
112+ const roleId = param . roleId ;
113+ const roleValue = param . roleValue ;
114+ const userRoles = [ ] ;
115+ const userId = 'userId' ;
116+
117+ spyOn ( store , 'dispatch' ) ;
118+
119+ component . switchRole ( userId , userRoles , roleId , roleValue ) ;
120+
121+ expect ( store . dispatch ) . toHaveBeenCalledWith ( new GrantRoleUser ( userId , roleId ) ) ;
122+ } ) ;
123+ } ) ;
124+
125+ const revokeRoleTypes = [
126+ { roleId : 'admin' , roleValue : 'time-tracker-admin' , userRoles : [ 'time-tracker-admin' ] } ,
127+ { roleId : 'test' , roleValue : 'time-tracker-tester' , userRoles : [ 'time-tracker-tester' ] } ,
128+ ] ;
129+
130+ revokeRoleTypes . map ( ( param ) => {
131+ it ( `When user switchRole to ${ param . roleId } and have that rol asigned, should revoke ${ param . roleValue } Role` , ( ) => {
132+ const roleId = param . roleId ;
133+ const roleValue = param . roleValue ;
134+ const userRoles = param . userRoles ;
135+ const userId = 'userId' ;
136+
137+ spyOn ( store , 'dispatch' ) ;
138+
139+ component . switchRole ( userId , userRoles , roleId , roleValue ) ;
140+
141+ expect ( store . dispatch ) . toHaveBeenCalledWith ( new RevokeRoleUser ( userId , roleId ) ) ;
142+ } ) ;
143+ } ) ;
74144
75145 it ( 'on success load users, the data of roles should be an array and role null' , ( ) => {
76146 const actionSubject = TestBed . inject ( ActionsSubject ) as ActionsSubject ;
@@ -117,7 +187,11 @@ describe('UsersListComponent', () => {
117187 } ) ;
118188 } ) ;
119189
120- it('on success load users, the datatable should be reloaded', async () => {
190+ /*
191+ TODO: block commented on purpose so that when the tests pass and the Feature toggle is removed,
192+ the table will be rendered again with dtInstance and not with dtOptions
193+
194+ it('on success load users, the datatable should be reloaded', async () => {
121195 const actionSubject = TestBed.inject(ActionsSubject);
122196 const action = {
123197 type: UserActionTypes.LOAD_USERS_SUCCESS,
0 commit comments