Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fixed tests
  • Loading branch information
mmaquina committed Sep 6, 2022
commit 263f91b8292b5ac09fb4e24d04d013b52911dc88
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { waitForAsync, ComponentFixture, TestBed } from '@angular/core/testing';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { MockStore, provideMockStore } from '@ngrx/store/testing';
import { NgxPaginationModule } from 'ngx-pagination';
import { UsersListComponent } from './users-list.component';
Expand All @@ -7,15 +8,22 @@ import { ActionsSubject } from '@ngrx/store';
import { DataTablesModule } from 'angular-datatables';
import { GrantUserRole, RevokeUserRole } from '../../store/user.actions';
import { ROLES } from '../../../../../environments/environment';
import { UserInfoService } from 'src/app/modules/user/services/user-info.service';
import { LoginService } from '../../../login/services/login.service';
import { JwtHelperService } from '@auth0/angular-jwt';
import { of } from 'rxjs';
import { UserInfoService } from 'src/app/modules/user/services/user-info.service';


describe('UsersListComponent', () => {
let component: UsersListComponent;
let fixture: ComponentFixture<UsersListComponent>;
let store: MockStore<UserState>;
let httpMock: HttpTestingController;
const actionSub: ActionsSubject = new ActionsSubject();
let loginService: LoginService;
let userInfoService: UserInfoService;
const userInfoServiceStub = {
isAdmin: () => of(false),
};

const state: UserState = {
data: [
Expand All @@ -36,9 +44,11 @@ describe('UsersListComponent', () => {
beforeEach(
waitForAsync(() => {
TestBed.configureTestingModule({
imports: [NgxPaginationModule, DataTablesModule],
imports: [NgxPaginationModule, DataTablesModule, HttpClientTestingModule],
declarations: [UsersListComponent],
providers: [provideMockStore({ initialState: state }), { provide: ActionsSubject, useValue: actionSub }],
providers: [provideMockStore({ initialState: state }),
{ provide: ActionsSubject, useValue: actionSub },
{ providers: LoginService, useValue: {}},],
}).compileComponents();
})
);
Expand All @@ -47,6 +57,9 @@ describe('UsersListComponent', () => {
fixture = TestBed.createComponent(UsersListComponent);
component = fixture.componentInstance;
store = TestBed.inject(MockStore);
httpMock = TestBed.inject(HttpTestingController);
loginService = TestBed.inject(LoginService);
userInfoService = TestBed.inject(UserInfoService);
store.setState(state);
fixture.detectChanges();
});
Expand Down Expand Up @@ -232,9 +245,19 @@ describe('UsersListComponent', () => {
expect(component.ROLES).toEqual(ROLES);
});

it('Should call to localstorage and helper decode for get information about user when checkRoleCurrentUser method is called', () => {
const account = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6ImFiYyIsIm5hbWUiOiJhYmMiLCJlbWFpbCI6ImVtYWlsIiwiZ3JvdXBzIjpbImFkbWluIl19.gy1GljkoiuOjP8DzkoLRYE9SldBn5ljRc4kp8rwq7UI';
spyOn(loginService, 'getLocalStorage').and.returnValue(account);
spyOn(userInfoService, 'isAdmin').and.returnValue(of(true));
const response = component.checkRoleCurrentUser('email')
expect(response).toBeTrue();
expect(userInfoService.isAdmin).toHaveBeenCalled();
expect(loginService.getLocalStorage).toHaveBeenCalled();
});

afterEach(() => {
component.dtTrigger.unsubscribe();
component.loadUsersSubscription.unsubscribe();
fixture.destroy();
});
});
});