1

I have written a test in jasmine for my angular application which is failing but the actual functionality works absolutely fine. I am also calling the fixture.DetectChanges() but not sure what the problem could be. I am getting an error

TypeError: Cannot read property 'maxSize' of undefined at DomicileSelectionComponent.isMinValid (webpack:///C:/vstsprojects/Risk.Analytics.Captives/Clientside/captives/src/app/pages/feasibility/ca ptives/domicile-selection/domicile-selection.component.ts?:88:52)

Why is the error coming for only maxSize and not minSize ?

TestComponent

describe('DomicileSelectionComponent', () => {
  let comp: DomicileSelectionComponent;
  let fixture: ComponentFixture<DomicileSelectionComponent>;

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [
        TooltipModule.forRoot(),
        FormsModule,
        TranslateModule.forRoot({
          loader: { provide: TranslateLoader, useClass: TranslateFakeLoader }
        })
      ],
      providers: [
        { provide: BsModalRef, useClass: BsModalRefStub },
        { provide: BackendProxy.ReferenceProxy, useClass: ReferenceProxyStub },
        { provide: RunService, useValue: runServiceStub }
      ],
      declarations: [DomicileSelectionComponent, YesNoPipe, CLICK_INPUT_DIRECTIVE, ShortNumberFormatPipe]
    });
  });

  beforeEach(() => {
   spyOn(BsModalRefStub.prototype, 'hide').and.callThrough();

    fixture = TestBed.createComponent(DomicileSelectionComponent);
    comp = fixture.componentInstance;
    comp.domicileInfo = domicileInformationDataResult.data;
    fixture.detectChanges();
  });

  fit('should return true because the index of the item is zero and min is less than max', () => {
    comp.domicileInfo.taxAssesment.items = [{  minSize: 30000000, maxSize: 40000000, values: [0.02, 0.02]}];
    fixture.detectChanges();
    console.log(comp.domicileInfo.taxAssesment.items);
    let isMin: boolean = comp.isMinValid(comp.domicileInfo.taxAssesment.items, 0);
    expect(isMin).toBe(true);
  });

});

Main component

isMinValid(currentItem: any, index: number) {
    if (index === 0 && (+currentItem.minSize <= +currentItem.maxSize)) {
        return !this._isMinValid;
    }
    else if ( index === 0 && +currentItem.minSize >= +currentItem.maxSize) {
          return this._isMinValid;
    } 
    let previousItem = this.domicileInfo.taxAssesment.items[index - 1];
    if (+currentItem.minSize !== +previousItem.maxSize) {
     return this._isMinValid;
    }
    return !this._isMinValid;
  }
3
  • Is it normal that you give the entire array on comp.isMinValid(comp.domicileInfo.taxAssesment.items, 0); ? Commented May 18, 2018 at 12:11
  • it should have only one item in the array Commented May 18, 2018 at 12:14
  • I got my answer. I just passed a single array item and it worked Commented May 18, 2018 at 12:15

1 Answer 1

1

I guess the problem is there :

fit('should return true because the index of the item is zero and min is less than max', () => {
    comp.domicileInfo.taxAssesment.items = [{  minSize: 30000000, maxSize: 40000000, values: [0.02, 0.02]}];
    fixture.detectChanges();
    console.log(comp.domicileInfo.taxAssesment.items);
    let isMin: boolean = comp.isMinValid(comp.domicileInfo.taxAssesment.items, 0);
    expect(isMin).toBe(true);
});

You give to isMinValid the entire array ( comp.domicileInfo.taxAssesment.items. )

Try this :

let isMin: boolean = comp.isMinValid(comp.domicileInfo.taxAssesment.items[0], 0);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.