TypeError: Cannot read properties of undefined (reading 'subscribe')

482 views
Skip to first unread message

Balaram Reddy

unread,
Jun 2, 2024, 12:14:01 PMJun 2
to Angular and AngularJS discussion
error : 

4.6.0
Options
finished in 0.33s8 specs, 3 failures, randomized with seed 70834Spec List | Failures
TypeError: Cannot read properties of undefined (reading 'subscribe')
at CardApprovalFlowComponent.getDocumentTypes (http://localhost:9876/_karma_webpack_/webpack:/src/app/components/card-approval/card-approval-flow/card-approval-flow.component.ts:118:51) at Object.next (http://localhost:9876/_karma_webpack_/webpack:/src/app/components/card-approval/card-approval-flow/card-approval-flow.component.ts:76:14) at ConsumerObserver.next (http://localhost:9876/_karma_webpack_/webpack:/node_modules/rxjs/dist/esm/internal/Subscriber.js:91:33) at SafeSubscriber._next (http://localhost:9876/_karma_webpack_/webpack:/node_modules/rxjs/dist/esm/internal/Subscriber.js:60:26) at SafeSubscriber.next (http://localhost:9876/_karma_webpack_/webpack:/node_modules/rxjs/dist/esm/internal/Subscriber.js:31:18) at Observable._subscribe (http://localhost:9876/_karma_webpack_/webpack:/node_modules/rxjs/dist/esm/internal/observable/innerFrom.js:51:24) at Observable._trySubscribe (http://localhost:9876/_karma_webpack_/webpack:/node_modules/rxjs/dist/esm/internal/Observable.js:37:25) at cb (http://localhost:9876/_karma_webpack_/webpack:/node_modules/rxjs/dist/esm/internal/Observable.js:31:30) at errorContext (http://localhost:9876/_karma_webpack_/webpack:/node_modules/rxjs/dist/esm/internal/util/errorContext.js:19:9) at Observable.subscribe (http://localhost:9876/_karma_webpack_/webpack:/node_modules/rxjs/dist/esm/internal/Observable.js:22:21)
TypeError: Cannot read properties of undefined (reading 'subscribe')
at CardApprovalFlowComponent.getApplicationById (http://localhost:9876/_karma_webpack_/webpack:/src/app/components/card-approval/card-approval-flow/card-approval-flow.component.ts:133:45) at Object.next (http://localhost:9876/_karma_webpack_/webpack:/src/app/components/card-approval/card-approval-flow/card-approval-flow.component.ts:77:14) at ConsumerObserver.next (http://localhost:9876/_karma_webpack_/webpack:/node_modules/rxjs/dist/esm/internal/Subscriber.js:91:33) at SafeSubscriber._next (http://localhost:9876/_karma_webpack_/webpack:/node_modules/rxjs/dist/esm/internal/Subscriber.js:60:26) at SafeSubscriber.next (http://localhost:9876/_karma_webpack_/webpack:/node_modules/rxjs/dist/esm/internal/Subscriber.js:31:18) at Observable._subscribe (http://localhost:9876/_karma_webpack_/webpack:/node_modules/rxjs/dist/esm/internal/observable/innerFrom.js:51:24) at Observable._trySubscribe (http://localhost:9876/_karma_webpack_/webpack:/node_modules/rxjs/dist/esm/internal/Observable.js:37:25) at cb (http://localhost:9876/_karma_webpack_/webpack:/node_modules/rxjs/dist/esm/internal/Observable.js:31:30) at errorContext (http://localhost:9876/_karma_webpack_/webpack:/node_modules/rxjs/dist/esm/internal/util/errorContext.js:19:9) at Observable.subscribe (http://localhost:9876/_karma_webpack_/webpack:/node_modules/rxjs/dist/esm/internal/Observable.js:22:21)
code :
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);
  });
});

Reply all
Reply to author
Forward
0 new messages