[英]Angular testing Cannot read property 'column' of undefined
I am trying to write a test case for the component but I am getting the error as Cannot read property 'column' of undefined. 我正在尝试为组件编写一个测试用例,但我收到错误,因为无法读取未定义的属性“列”。 I am using angular 6.I am trying to write a test case for the component but I am getting the error as Cannot read property 'column' of undefined.
我正在使用角度6.我正在尝试为组件编写测试用例,但我收到错误,因为无法读取未定义的属性“列”。 I am using angular 6.
我正在使用角6。
Here My code for the component 这是我的组件代码
import { Component, Inject } from '@angular/core';
import { ICellRendererAngularComp } from 'ag-grid-angular';
import { faEdit, faTrashAlt } from '@fortawesome/free-solid-svg-icons';
import { Router } from '@angular/router';
import { MatDialog } from '@angular/material/dialog';
import { ConfirmDialogComponent } from 'src/app/common/confirm-dialog/confirm-dialog.component';
@Component({
selector: 'ap-grid-render',
template: `
<div *ngIf="params.column === 'status'">
<mat-chip *ngIf="params.value === 'In progress'" color="primary" selected>
{{ params.value }}
</mat-chip>
<mat-chip *ngIf="params.value === 'Approved'" color="accent" selected>
{{ params.value }}
</mat-chip>
<mat-chip *ngIf="params.value === 'Rejected'" color="warn" selected>
{{ params.value }}
</mat-chip>
</div>
<div *ngIf="params.column === 'edit'">
<button
mat-icon-button
color="accent"
matTooltip="Edit request"
matTooltipPosition="above"
(click)="goToEdit()"
>
<fa-icon [icon]="faEdit"></fa-icon>
</button>
<button
mat-icon-button
color="accent"
matTooltip="Delete"
matTooltipPosition="above"
(click)="deleteDomain()"
>
<fa-icon [icon]="faTrashAlt"></fa-icon>
</button>
</div>
`
})
export class GridRendererComponent implements ICellRendererAngularComp {
public params: any;
// Icons
faEdit = faEdit;
faTrashAlt = faTrashAlt;
constructor(private router: Router, public dialog: MatDialog) {}
agInit(params: any): void {
this.params = params;
}
goToEdit() {
this.router.navigate(['./n-access-request']);
}
deleteDomain() {
this.dialog.open(ConfirmDialogComponent, {
panelClass: '_small-dialog',
disableClose: true,
position: { top: '50px' },
data: {
title: 'Delete Domain',
description: `Are you sure do you want to delete domain Contract ID: ${
this.params.data.contactId
}?`
}
});
}
refresh(params: any) {
this.params = params;
return true;
}
}
Here is the below code for spec which I tried 以下是我尝试的规范的下面代码
it('agInit', () => {
let params = {
"column" : "status"
};
gridRendererComponent.agInit(params);
expect(gridRendererComponent.params).not.toBe(null);
});
The error is caused as the value of params
is undefined when trying to access from template. 由于在尝试从模板访问时未定义
params
的值,因此导致该错误。 To solve it, use safe navigation operator ?
要解决它,请使用安全的导航操作员
?
. 。
Instead of, 代替,
params.column
Use, 采用,
params?.column
You might also wanna consider changing params.value
to params?.value
or it will also throw an error. 您可能还想考虑将
params.value
更改为params?.value
或者它也会引发错误。
Declare the type(class) of params
instead of any
声明
params
的类型(类)而不是any
public params: Params;
export class Params {
public column :any
public value :any
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.