When running unit tests I am getting the error:
TypeError: Cannot read property 'value' of undefined
In the component.ts file I have tried intializing all values:
@Input() beneficiary: string = '';
pageInfo: { startingPayment: number; endingPayment: number; total: number };
history = new BehaviorSubject<[]>([]);
history$: Observable<[]> = null;
pageNumber = 1;
maxPages: number = 0;
maxPerPage: number = 0;
totalPayments: number = 0;
prevBtnDisabled = true;
nextBtnDisabled = true;
errorMessage: string = '';
handleId: string = '';
apiCalled = true;
paginator: MatPaginator = null;
constructor(private thirdPartyAccountsStoreService: ThirdPartyAccountsStoreService) {
this.history$ = this.history.asObservable();
}
ngOnInit(): void {
this.getPaymentInitHistory();
}
getPaymentInitHistory() {
if (this.beneficiary) {
this.apiCalled = true;
this.resetProperties();
this.thirdPartyAccountsStoreService.getPaymentInitHistory(this.beneficiary).subscribe(
(result: any) => {
this.handleId = result.input.value;
this.setUpPagesProperties(result.model[0]);
this.handleResponse(result);
},
(error) => this.handleError(error)
);
}
}
In the html I added ngIf to check history$
before running the ngFor:
<div *ngIf="history$">
<div class="grid" *ngFor="let row of history$ | async">
<div class="grid-row">
<div class="text-size">{{ row.valuedate | appFormatDate | async }}</div>
<div class="secondary-text">{{ row.narrative }}</div>
</div>
<div class="grid-row-end">
<div class="text-size">
<app-amount [value]="row.credit" [currency]="row.currency"></app-amount>
</div>
<div class="secondary-text">{{ row.contranarrative }}</div>
</div>
I am not sure what is causing this error. The only place value
is used is in the html page [value]="row.credit"
and I have tried using [value]="row?.credit"
but that hasn't worked either. And in the ts file in the file this.handleId = result.input.value;
but I added a check if (this.beneficiary)
so if that field is blank it shouldn't run the code in the function