Skip to content

Commit 263f91b

Browse files
committed
fixed tests
1 parent 1c5203e commit 263f91b

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

src/app/modules/users/components/users-list/users-list.component.spec.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { waitForAsync, ComponentFixture, TestBed } from '@angular/core/testing';
2+
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
23
import { MockStore, provideMockStore } from '@ngrx/store/testing';
34
import { NgxPaginationModule } from 'ngx-pagination';
45
import { UsersListComponent } from './users-list.component';
@@ -7,15 +8,22 @@ import { ActionsSubject } from '@ngrx/store';
78
import { DataTablesModule } from 'angular-datatables';
89
import { GrantUserRole, RevokeUserRole } from '../../store/user.actions';
910
import { ROLES } from '../../../../../environments/environment';
10-
import { UserInfoService } from 'src/app/modules/user/services/user-info.service';
1111
import { LoginService } from '../../../login/services/login.service';
12-
import { JwtHelperService } from '@auth0/angular-jwt';
12+
import { of } from 'rxjs';
13+
import { UserInfoService } from 'src/app/modules/user/services/user-info.service';
14+
1315

1416
describe('UsersListComponent', () => {
1517
let component: UsersListComponent;
1618
let fixture: ComponentFixture<UsersListComponent>;
1719
let store: MockStore<UserState>;
20+
let httpMock: HttpTestingController;
1821
const actionSub: ActionsSubject = new ActionsSubject();
22+
let loginService: LoginService;
23+
let userInfoService: UserInfoService;
24+
const userInfoServiceStub = {
25+
isAdmin: () => of(false),
26+
};
1927

2028
const state: UserState = {
2129
data: [
@@ -36,9 +44,11 @@ describe('UsersListComponent', () => {
3644
beforeEach(
3745
waitForAsync(() => {
3846
TestBed.configureTestingModule({
39-
imports: [NgxPaginationModule, DataTablesModule],
47+
imports: [NgxPaginationModule, DataTablesModule, HttpClientTestingModule],
4048
declarations: [UsersListComponent],
41-
providers: [provideMockStore({ initialState: state }), { provide: ActionsSubject, useValue: actionSub }],
49+
providers: [provideMockStore({ initialState: state }),
50+
{ provide: ActionsSubject, useValue: actionSub },
51+
{ providers: LoginService, useValue: {}},],
4252
}).compileComponents();
4353
})
4454
);
@@ -47,6 +57,9 @@ describe('UsersListComponent', () => {
4757
fixture = TestBed.createComponent(UsersListComponent);
4858
component = fixture.componentInstance;
4959
store = TestBed.inject(MockStore);
60+
httpMock = TestBed.inject(HttpTestingController);
61+
loginService = TestBed.inject(LoginService);
62+
userInfoService = TestBed.inject(UserInfoService);
5063
store.setState(state);
5164
fixture.detectChanges();
5265
});
@@ -232,9 +245,19 @@ describe('UsersListComponent', () => {
232245
expect(component.ROLES).toEqual(ROLES);
233246
});
234247

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+
235258
afterEach(() => {
236259
component.dtTrigger.unsubscribe();
237260
component.loadUsersSubscription.unsubscribe();
238261
fixture.destroy();
239262
});
240-
});
263+
});

0 commit comments

Comments
 (0)