import { ComponentFixture, TestBed, fakeAsync, tick, waitForAsync } from '@angular/core/testing';
import { ReactiveFormsModule, FormBuilder, FormsModule } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';
import { MessageService } from 'primeng/api';
import { of, throwError, Subject } from 'rxjs';
import { CardApprovalFlowComponent } from './card-approval-flow.component';
import { AdminFacadeService } from '../../../facade/admin.facade.service';
import { CardApplicationFacadeService } from '../../../facade/card-application.facade.service';
import { ShowPopUpService } from '../../../helpers/showPopUp.service';
import EncryptDecrypt from '../../../helpers/encrypt.decrypt';
describe('CardApprovalFlowComponent', () => {
let component: CardApprovalFlowComponent;
let fixture: ComponentFixture<CardApprovalFlowComponent>;
let mockActivatedRoute: Partial<ActivatedRoute>;
let mockCardApplicationService: jasmine.SpyObj<CardApplicationFacadeService>;
let mockMessageService: jasmine.SpyObj<MessageService>;
let mockAdminService: jasmine.SpyObj<AdminFacadeService>;
let mockShowPopUpService: jasmine.SpyObj<ShowPopUpService>;
let decryptSpy: jasmine.Spy;
let router: jasmine.SpyObj<Router>;
let resetRejectRemarksDialogSubject: Subject<boolean>;
beforeEach(waitForAsync(() => {
mockActivatedRoute = { queryParams: of({ applicationId: 'encryptedId', status: 'encryptedStatus' }) };
mockCardApplicationService = jasmine.createSpyObj('CardApplicationFacadeService', [
'getDocumentTypes',
'getCardApplicationDocs',
'getDocumentById',
'getApplicationById'
]);
mockMessageService = jasmine.createSpyObj('MessageService', ['add', 'clear']);
mockAdminService = jasmine.createSpyObj('AdminFacadeService', [
'getApplicationById',
'approveOrRejectDocumentByApprover',
'approveOrRejectGeneralInfoByApprover',
'approveOrRejectResidentialAddressByApprover',
'approveOrRejectPermanentAddressByApprover',
'updateApprovalVerificationStatusByApprover',
'generateMedicalIdByApprover',
'getRejectReasons'
]);
mockAdminService.getRejectReasons.and.returnValue(of([]));
mockShowPopUpService = jasmine.createSpyObj('ShowPopUpService', ['resetRejectRemarksDialog']);
resetRejectRemarksDialogSubject = new Subject<boolean>();
mockShowPopUpService.resetRejectRemarksDialog = resetRejectRemarksDialogSubject;
decryptSpy = spyOn(EncryptDecrypt, 'decrypt').and.callFake((value: string) => value);
router = jasmine.createSpyObj('Router', ['navigate']);
TestBed.configureTestingModule({
imports: [ReactiveFormsModule, FormsModule],
declarations: [],
providers: [
FormBuilder,
{ provide: ActivatedRoute, useValue: mockActivatedRoute },
{ provide: Router, useValue: router },
{ provide: CardApplicationFacadeService, useValue: mockCardApplicationService },
{ provide: AdminFacadeService, useValue: mockAdminService },
{ provide: ShowPopUpService, useValue: mockShowPopUpService },
{ provide: MessageService, useValue: mockMessageService },
]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(CardApprovalFlowComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create the component', () => {
expect(component).toBeTruthy();
});
it('should initialize the form', () => {
component.initializeForm();
expect(component.verificationSummaryForm).toBeTruthy();
expect(component.verificationSummaryForm.controls['verificationStatus']).toBeTruthy();
expect(component.verificationSummaryForm.controls['verificationRemarks']).toBeTruthy();
});
it('should call getDocumentTypes on init with correct parameters', () => {
const applicationId = 'decryptedApplicationId';
const status = 'decryptedStatus';
decryptSpy.and.callFake((value: string) => value === 'encryptedId' ? applicationId : status);
spyOn(component as any, 'getDocumentTypes');
component.ngOnInit();
expect(component['getDocumentTypes']).toHaveBeenCalledWith(applicationId);
expect(mockCardApplicationService.getDocumentTypes).toHaveBeenCalled();
});
it('should handle error in getDocumentTypes', () => {
const id = 'someId';
mockCardApplicationService.getDocumentTypes.and.returnValue(throwError('Error'));
component.getDocumentTypes(id);
expect(mockMessageService.add).toHaveBeenCalledWith({
severity: 'error',
summary: 'Error',
detail: 'Error retrieving document types'
});
});
it('should get application data and handle success response', () => {
const applicationDataResponse: any = { data: {} };
mockAdminService.getApplicationById.and.returnValue(of(applicationDataResponse));
component.getApplicationById('id');
expect(mockAdminService.getApplicationById).toHaveBeenCalledWith('id');
expect(component.applicationData).not.toEqual(applicationDataResponse.data);
});
it('should handle error in getApplicationById', fakeAsync(() => {
const errorMessage = 'Error retrieving application';
mockAdminService.getApplicationById.and.returnValue(throwError(new Error(errorMessage)));
component.getApplicationById('id');
tick();
expect(mockMessageService.add).toHaveBeenCalledWith({
severity: 'error',
summary: 'Error',
detail: errorMessage
});
}));
it('should close the application and navigate based on status', () => {
const statuses = ['Rejected', 'New-For-Approval', 'InProgress', 'Reuploaded', 'Approved'];
const routes = ['card-approval/rejected', 'card-approval/verified', 'card-approval/in-progress', 'card-approval/re-uploaded', 'card-approval/approved'];
for (let i = 0; i < statuses.length; i++) {
component.applicationStatus = statuses[i];
component.closeApplication();
expect(router.navigate).toHaveBeenCalledWith([routes[i]]);
}
});
it('should decrypt and set applicationId and applicationStatus on init', () => {
const encryptedId = 'encryptedId';
const encryptedStatus = 'encryptedStatus';
const decryptedId = 'decryptedId';
const decryptedStatus = 'decryptedStatus';
decryptSpy.and.callFake((value: string) => {
if (value === encryptedId) {
return decryptedId;
} else if (value === encryptedStatus) {
return decryptedStatus;
}
return value;
});
component.ngOnInit();
expect(component.applicationId).toBe(decryptedId);
expect(component.applicationStatus).toBe(decryptedStatus);
});
});