1
1
import { waitForAsync , ComponentFixture , TestBed } from '@angular/core/testing' ;
2
+ import { HttpClientTestingModule , HttpTestingController } from '@angular/common/http/testing' ;
2
3
import { MockStore , provideMockStore } from '@ngrx/store/testing' ;
3
4
import { NgxPaginationModule } from 'ngx-pagination' ;
4
5
import { UsersListComponent } from './users-list.component' ;
@@ -7,12 +8,22 @@ import { ActionsSubject } from '@ngrx/store';
7
8
import { DataTablesModule } from 'angular-datatables' ;
8
9
import { GrantUserRole , RevokeUserRole } from '../../store/user.actions' ;
9
10
import { 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
+
10
15
11
16
describe ( 'UsersListComponent' , ( ) => {
12
17
let component : UsersListComponent ;
13
18
let fixture : ComponentFixture < UsersListComponent > ;
14
19
let store : MockStore < UserState > ;
20
+ let httpMock : HttpTestingController ;
15
21
const actionSub : ActionsSubject = new ActionsSubject ( ) ;
22
+ let loginService : LoginService ;
23
+ let userInfoService : UserInfoService ;
24
+ const userInfoServiceStub = {
25
+ isAdmin : ( ) => of ( false ) ,
26
+ } ;
16
27
17
28
const state : UserState = {
18
29
data : [
@@ -33,9 +44,11 @@ describe('UsersListComponent', () => {
33
44
beforeEach (
34
45
waitForAsync ( ( ) => {
35
46
TestBed . configureTestingModule ( {
36
- imports : [ NgxPaginationModule , DataTablesModule ] ,
47
+ imports : [ NgxPaginationModule , DataTablesModule , HttpClientTestingModule ] ,
37
48
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 : { } } , ] ,
39
52
} ) . compileComponents ( ) ;
40
53
} )
41
54
) ;
@@ -44,6 +57,9 @@ describe('UsersListComponent', () => {
44
57
fixture = TestBed . createComponent ( UsersListComponent ) ;
45
58
component = fixture . componentInstance ;
46
59
store = TestBed . inject ( MockStore ) ;
60
+ httpMock = TestBed . inject ( HttpTestingController ) ;
61
+ loginService = TestBed . inject ( LoginService ) ;
62
+ userInfoService = TestBed . inject ( UserInfoService ) ;
47
63
store . setState ( state ) ;
48
64
fixture . detectChanges ( ) ;
49
65
} ) ;
@@ -229,9 +245,19 @@ describe('UsersListComponent', () => {
229
245
expect ( component . ROLES ) . toEqual ( ROLES ) ;
230
246
} ) ;
231
247
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
+
232
258
afterEach ( ( ) => {
233
259
component . dtTrigger . unsubscribe ( ) ;
234
260
component . loadUsersSubscription . unsubscribe ( ) ;
235
261
fixture . destroy ( ) ;
236
262
} ) ;
237
- } ) ;
263
+ } ) ;
0 commit comments