问题描述
我正在尝试通过选择 angular 11 中的 rowIndex 来测试启用或禁用按钮,它在 expect(component.deleteRoleButtondisabled).toBeFalse(); 中失败;并且永远如此!你们有什么想法吗?
HTML:
<tr class="roles-list" mat-row *matRowDef="let row; columns: displayedColumns;"
(click)="highlight(row)" [ngClass]="{'highlight': selectedRowIndex == row.id}"
id="highlightRow"></tr>
TS:
selectedRowIndex: number | undefined;
deleteRoleButtondisabled = true;
highlight(row: RoleAssignmentTableRowData) {
this.selectedRowIndex = row.id;
this.deleteRoleButtondisabled = false;
}
规格:
it('should enable delete role button if one role is selected',() => {
component.selectedRowIndex = 1;
expect(component.selectedRowIndex).toBe(1);
expect(component.highlight).toBeTrue();
expect(component.deleteRoleButtondisabled).toBeFalse(); // this goes to be always true!
});
解决方法
highlight(...) 是在您的测试中根本没有触发的点击事件上调用的,因此没有理由将 deleteRoleButtonDisabled 变为 false。