11import { waitForAsync , ComponentFixture , TestBed } from '@angular/core/testing' ;
2+ import { HttpClientTestingModule , HttpTestingController } from '@angular/common/http/testing' ;
23import { MockStore , provideMockStore } from '@ngrx/store/testing' ;
34import { NgxPaginationModule } from 'ngx-pagination' ;
45import { UsersListComponent } from './users-list.component' ;
@@ -7,12 +8,22 @@ import { ActionsSubject } from '@ngrx/store';
78import { DataTablesModule } from 'angular-datatables' ;
89import { GrantUserRole , RevokeUserRole } from '../../store/user.actions' ;
910import { ROLES } from '../../../../../environments/environment' ;
11+ import { LoginService } from '../../../login/services/login.service' ;
12+ import { of } from 'rxjs' ;
13+ import { UserInfoService } from 'src/app/modules/user/services/user-info.service' ;
14+
1015
1116describe ( 'UsersListComponent' , ( ) => {
1217 let component : UsersListComponent ;
1318 let fixture : ComponentFixture < UsersListComponent > ;
1419 let store : MockStore < UserState > ;
20+ let httpMock : HttpTestingController ;
1521 const actionSub : ActionsSubject = new ActionsSubject ( ) ;
22+ let loginService : LoginService ;
23+ let userInfoService : UserInfoService ;
24+ const userInfoServiceStub = {
25+ isAdmin : ( ) => of ( false ) ,
26+ } ;
1627
1728 const state : UserState = {
1829 data : [
@@ -33,9 +44,11 @@ describe('UsersListComponent', () => {
3344 beforeEach (
3445 waitForAsync ( ( ) => {
3546 TestBed . configureTestingModule ( {
36- imports : [ NgxPaginationModule , DataTablesModule ] ,
47+ imports : [ NgxPaginationModule , DataTablesModule , HttpClientTestingModule ] ,
3748 declarations : [ UsersListComponent ] ,
38- providers : [ provideMockStore ( { initialState : state } ) , { provide : ActionsSubject , useValue : actionSub } ] ,
49+ providers : [ provideMockStore ( { initialState : state } ) ,
50+ { provide : ActionsSubject , useValue : actionSub } ,
51+ { providers : LoginService , useValue : { } } , ] ,
3952 } ) . compileComponents ( ) ;
4053 } )
4154 ) ;
@@ -44,6 +57,9 @@ describe('UsersListComponent', () => {
4457 fixture = TestBed . createComponent ( UsersListComponent ) ;
4558 component = fixture . componentInstance ;
4659 store = TestBed . inject ( MockStore ) ;
60+ httpMock = TestBed . inject ( HttpTestingController ) ;
61+ loginService = TestBed . inject ( LoginService ) ;
62+ userInfoService = TestBed . inject ( UserInfoService ) ;
4763 store . setState ( state ) ;
4864 fixture . detectChanges ( ) ;
4965 } ) ;
@@ -229,9 +245,19 @@ describe('UsersListComponent', () => {
229245 expect ( component . ROLES ) . toEqual ( ROLES ) ;
230246 } ) ;
231247
248+ it ( 'Should call to localstorage and helper decode for get information about user when checkRoleCurrentUser method is called' , ( ) => {
249+ const account = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6ImFiYyIsIm5hbWUiOiJhYmMiLCJlbWFpbCI6ImVtYWlsIiwiZ3JvdXBzIjpbImFkbWluIl19.gy1GljkoiuOjP8DzkoLRYE9SldBn5ljRc4kp8rwq7UI' ;
250+ spyOn ( loginService , 'getLocalStorage' ) . and . returnValue ( account ) ;
251+ spyOn ( userInfoService , 'isAdmin' ) . and . returnValue ( of ( true ) ) ;
252+ const response = component . checkRoleCurrentUser ( 'email' )
253+ expect ( response ) . toBeTrue ( ) ;
254+ expect ( userInfoService . isAdmin ) . toHaveBeenCalled ( ) ;
255+ expect ( loginService . getLocalStorage ) . toHaveBeenCalled ( ) ;
256+ } ) ;
257+
232258 afterEach ( ( ) => {
233259 component . dtTrigger . unsubscribe ( ) ;
234260 component . loadUsersSubscription . unsubscribe ( ) ;
235261 fixture . destroy ( ) ;
236262 } ) ;
237- } ) ;
263+ } ) ;
0 commit comments