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;
}
comp.isMinValid(comp.domicileInfo.taxAssesment.items, 0);?