Summary Table

Categories Total Count
PII 0
URL 0
DNS 0
EKL 0
IP 0
PORT 0
VsID 0
CF 0
AI 1
VPD 0
PL 0
Other 0

File Content

import { UserAdminComponent } from './user-admin.component';
import { FormBuilder } from '@angular/forms';
import { UserAdminService } from './user-admin.service';
import { PaginationSettings } from '../../shared/table';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/throw';
import 'rxjs/add/observable/of';
import { AuthenticationService } from '../../auth/auth.service';
import { AppSettingsService } from './../../shared/app-settings/app-settings.service';

import { PaginationModel, UserRequestModel } from './user-admin.model';

describe('UserAdminComponent', () => {
let fb: FormBuilder;
let userAdminService: UserAdminService;
let component: UserAdminComponent;
let authenticationService: AuthenticationService;
let appSettingsService: AppSettingsService;

beforeEach(() => {
fb = new FormBuilder();
userAdminService = new UserAdminService(null);
authenticationService = new AuthenticationService(null, null, null, null);
appSettingsService = new AppSettingsService(authenticationService, null);
component = new UserAdminComponent(
fb,
userAdminService,
authenticationService,
appSettingsService
);
});

it('should be created', () => {
expect(component).toBeTruthy();
});

it('test ngOnInit method', () => {
const formInitSpy = spyOn(component, 'formInit');
component.tableSettings.buttonShow = false;
const appSettingsServiceSpy = spyOn(
appSettingsService,
'getMenu'
).and.callFake(() => new Observable(observer => observer.next('')));
component.ngOnInit();

expect(appSettingsServiceSpy).toHaveBeenCalled();
expect(formInitSpy).toHaveBeenCalled();
expect(component.tableSettings.buttonShow).toBeTruthy();
expect(component.tableSettings.buttonNames).toEqual(['Modify']);
});

it('test formInit method: verify appropriate forms have been instantiated', () => {
component.formInit();

expect(component.createForm).toBeDefined();
expect(component.createForm).not.toEqual(null);

expect(component.searchForm).toBeDefined();
expect(component.searchForm).not.toEqual(null);

expect(component.modifyForm).toBeDefined();
expect(component.modifyForm).not.toEqual(null);
});

it('test formInit method: verify some basic operations on forms', () => {
component.formInit();

component.createForm.get('userName').setValue('hi');
expect(component.createForm.get('userName').valid).toBeTruthy();

component.createForm.get('userName').setValue('');
expect(component.createForm.get('userName').valid).toBeFalsy();

// ***********************************************************************

component.createForm.get('phone').setValue('123');
expect(component.createForm.get('phone').valid).toBeTruthy();

component.createForm.get('phone').setValue('abc');
expect(component.createForm.get('phone').valid).toBeFalsy();

// next form
// ***********************************************************************
// ***********************************************************************

component.searchForm.get('userName').setValue('hi');
expect(component.searchForm.get('userName').valid).toBeTruthy();

component.searchForm.get('userName').setValue('');
expect(component.searchForm.get('userName').valid).toBeFalsy();

// next form
// ***********************************************************************
// ***********************************************************************

component.modifyForm.get('editPhone').setValue('123');
expect(component.modifyForm.get('editPhone').valid).toBeTruthy();

component.modifyForm.get('editPhone').setValue('abc');
expect(component.modifyForm.get('editPhone').valid).toBeFalsy();

expect(component.createForm).toBeTruthy();
expect(component.searchForm).toBeTruthy();
expect(component.modifyForm).toBeTruthy();
});

it('test search method', () => {
component.searchUserClicked = false;
component.paginationSettings = new PaginationSettings();
component.paginationSettings.sortColumn = 'sort';
const fetchDataSpy = spyOn(component, 'fetchData');

component.search();

expect(component.searchUserClicked).toBeTruthy();
expect(component.paginationSettings.sortColumn).toEqual('');
expect(fetchDataSpy).toHaveBeenCalled();
});

// it('test fetchData method with searchUserClicked false and an error', () => {
// component.searchUserClicked = false;

// const getUserInfoSpy = spyOn(userAdminService, 'getAllUsers').and.callFake(
// () => new Observable(observer => observer.next({ errorCode: true }))
// );
// component.fetchData();

// expect(component.errorMsg).toEqual('');
// });

// it('test fetchData method with searchUserClicked false and no error', () => {
// component.searchUserClicked = false;

// const getUserInfoSpy = spyOn(userAdminService, 'getAllUsers').and.callFake(
// () => new Observable(observer => observer.next({ errorCode: false }))
// );

// component.fetchData();

// expect(component.errorMsg).toEqual('');
// expect(component.showResults).toBeTruthy();
// });

it('test updateTable method', () => {
const updateTableSpy = spyOn(component, 'fetchData');
component.updateTable();

expect(updateTableSpy).toHaveBeenCalled();
});

it('test listAllUsers method', () => {
component.searchUserClicked = true;
const fetchDataSpy = spyOn(component, 'fetchData');

component.listAllUsers();

expect(component.searchUserClicked).toBeFalsy();
expect(component.paginationSettings).toEqual(new PaginationSettings());
expect(fetchDataSpy).toHaveBeenCalled();
});

it('test resetSearchForm method', () => {
component.showResults = true;
const clearMsgsSpy = spyOn(component, 'clearMsgs');

component.resetSearchForm();

expect(component.showResults).toBeFalsy();
expect(component.paginationModel).toEqual(new PaginationModel());
expect(clearMsgsSpy).toHaveBeenCalled();
});

it('test clearMsgs method', () => {
component.errorStatus = true;
component.clearMsgs();

expect(component.errorStatus).toBeFalsy();
});

it('test resetCreateForm method', () => {
const clearMsgsSpy = spyOn(component, 'clearMsgs');
component.requestModel = new UserRequestModel();
// component.requestModel.edit = 'something';
component.enableEdit = true;
component.resetCreateForm();

// expect(component.enableEdit).toBeFalsy();
expect(clearMsgsSpy).toHaveBeenCalled();
expect(component.requestModel).toEqual(new UserRequestModel());
});

it('test buttonClicked method with buttonName set to Modify', () => {
const openEditModalSpy = spyOn(component, 'openEditModal');

const inputEvent = { buttonName: 'Modify' };
component.buttonClicked(inputEvent);

expect(openEditModalSpy).toHaveBeenCalled();
expect(openEditModalSpy).toHaveBeenCalledWith(inputEvent);
});

it('test buttonClicked method', () => {
spyOn(component, 'openEditModal');

const inputEvent = { buttonName: false };
component.buttonClicked(inputEvent);

expect(component.openEditModal).toHaveBeenCalled();
expect(component.openEditModal).toHaveBeenCalledWith(inputEvent);
});

it('test openEditModal method', () => {
component.modalClosed = true;
spyOn(component, 'setCustomValidator');

const inputUserRequestModel = new UserRequestModel();
// inputUserRequestModel.export = 'No';
const inputEvent = { row: inputUserRequestModel };
component.openEditModal(inputEvent);

expect(component.setCustomValidator).toHaveBeenCalled();
expect(component.setCustomValidator).toHaveBeenCalledWith(
component.modifyForm,
component.userValueBeforeChange
);

expect(component.modalClosed).toBeFalsy();
expect(component.editRequestModel).toEqual({ ...inputEvent.row });
expect(component.userValueBeforeChange).toEqual({ ...inputEvent.row });
expect(component.modifyEnableExport).toEqual(false);
});

it('test createUser method with error', () => {
component.enableExport = true;
component.enableEdit = true;
component.isAdmin = true;

const createUserSpy = spyOn(userAdminService, 'createUser').and.callFake(
() =>
new Observable(observer =>
observer.next({ errorCode: true, message: 'message' })
)
);

spyOn(authenticationService, 'getDecodedUserInfo').and.callFake(() => {
return {
userName:
AI '
};
});

component.createUser();

// expect(component.requestModel.export).toEqual('N');
// expect(component.requestModel.edit).toEqual('N');
// expect(component.requestModel.admin).toEqual('N');
expect(component.requestModel.createdBy).toEqual('');
expect(component.errorMsg).toEqual('');
});

it('test createUser method without error', () => {
spyOn(authenticationService, 'getDecodedUserInfo').and.callFake(() => {
return {
userName: ''
};
});
component.enableExport = true;
component.enableEdit = true;
component.isAdmin = true;

const createUserSpy = spyOn(userAdminService, 'createUser').and.callFake(
() => new Observable(observer => observer.next({ errorCode: false }))
);

component.createUser();

// expect(component.requestModel.export).toEqual('N');
// expect(component.requestModel.edit).toEqual('N');
// expect(component.requestModel.admin).toEqual('N');
expect(component.requestModel.createdBy).toEqual('');
expect(component.successMsg).toEqual('User was successfully created.');
});

it('test modifyUser method', () => {
const getDecoderUserInfoReturnData = { userName: 'USER_NAME' };
spyOn(authenticationService, 'getDecodedUserInfo').and.callFake(
() => getDecoderUserInfoReturnData
);

const observableSubscription = Observable.of({});
spyOn(userAdminService, 'modifyUser').and.returnValue(
observableSubscription
);

spyOn(component.subscription, 'add');
spyOn(component, 'fetchData');

component.successMsg = null;
component.successStatus = null;
component.errorMsg = null;
component.errorStatus = null;
component.paginationSettings = null;

component.searchUserClicked = false;
component.modifyUser();

expect(component.errorMsg).toEqual('');
expect(component.errorStatus).toEqual(false);
expect(component.successMsg).toEqual('User was successfully modified.');
expect(component.successStatus).toEqual(true);

expect(component.paginationSettings).toBeDefined();
expect(component.paginationSettings).not.toEqual(null);

expect(component.editRequestModel.modifiedBy).toEqual('USER_NAME');
expect(component.modalClosed).toEqual(true);

expect(userAdminService.modifyUser).toHaveBeenCalled();
expect(userAdminService.modifyUser).toHaveBeenCalledWith(
component.editRequestModel
);

expect(component.fetchData).toHaveBeenCalled();
expect(component.subscription.add).toHaveBeenCalled();
});

it('test onModalClose method', () => {
component.onModalClose({});
expect(component.modalClosed).toEqual(true);
});

it('test resetModal method', () => {
const initalUserRequestModel = new UserRequestModel();
expect(component.editRequestModel).toEqual(initalUserRequestModel);
component.modalClosed = false;

component.userValueBeforeChange = new UserRequestModel();
component.userValueBeforeChange.userName = 'USER_NAME';
component.userValueBeforeChange.domain = 'DOMAIN';
// component.userValueBeforeChange.status = 'N';
// component.userValueBeforeChange.export = 'Y';
// component.userValueBeforeChange.edit = 'Y';
// component.userValueBeforeChange.admin = 'Y';
component.userValueBeforeChange.createdBy = 'CREATED_BY';
component.userValueBeforeChange.phone = 1234567890;
component.userValueBeforeChange.email = 'E-MAIL';
component.userValueBeforeChange.lastName = 'LAST_NAME';
component.userValueBeforeChange.firstName = 'FIRST_NAME';
component.userValueBeforeChange.modifiedBy = 'MODIFIED_BY';
component.userValueBeforeChange.userId = 555456789;

component.resetModal();

expect(component.editRequestModel).toEqual({
...component.userValueBeforeChange
});
expect(component.modalClosed).toEqual(true);
});

it('test ngOnDestroy method', () => {
spyOn(component.subscription, 'unsubscribe');
component.ngOnDestroy();
expect(component.subscription.unsubscribe).toHaveBeenCalled();
});

it('test setCustomValidator method', () => {
const form = fb.group({});
spyOn(form, 'setValidators');
spyOn(form, 'updateValueAndValidity');

const initialObject = new UserRequestModel();
component.setCustomValidator(form, initialObject);

expect(form.setValidators).toHaveBeenCalled();
expect(form.updateValueAndValidity).toHaveBeenCalled();
});
});