[英]Cannot trigger click event undefined element
I need to test click event on mat-table row.我需要在 mat-table 行上测试点击事件。 Below is my mat-table code
下面是我的垫表代码
<table
mat-table
[dataSource]="dataSource"
matSort
matSortActive="formCode"
matSortDirection="asc"
>
<caption></caption>
<ng-container matColumnDef="isChecked">
<th mat-header-cell *matHeaderCellDef></th>
<td mat-cell *matCellDef="let row">
<mat-checkbox
(click)="$event.stopPropagation()"
(change)="$event ? selection.toggle(row) : null"
[checked]="selection.isSelected(row)"
>
</mat-checkbox>
</td>
</ng-container>
<ng-container matColumnDef="code">
<th mat-header-cell *matHeaderCellDef mat-sort-header>Référence</th>
<td
mat-cell
*matCellDef="let element"
class="clickable-link"
(click)="openSummary(element.code)"
>
{{ element.code }}
</td>
</ng-container>
</table>
I try to find a row by css and I do something like this: ngMocks.click('.clickable-link')
;我试图通过 css 找到一行,我做了这样的事情:
ngMocks.click('.clickable-link')
; or ngMocks.click('mat-checkbox')
but it return undefined I already try fixture.debugElement.query(By.css('mat-checkbox'))
but nothing changed.或
ngMocks.click('mat-checkbox')
但它返回未定义我已经尝试了fixture.debugElement.query(By.css('mat-checkbox'))
但没有任何改变。 This my spec.ts
file:这是我的
spec.ts
文件:
it('should open summary',async ()=>{
filesService.getFormByCode.and.returnValue(of(formList));
ngMocks.click('.clickable-link');
fixture.detectChanges();
expect(filesService.getFormByCode).toHaveBeenCalled();
})
How can I access click event on the table's row?如何访问表格行上的点击事件?
You have to access the mat-table
before the class of td
what happens if you tried ngMocks.click('table td.clickable-link');
您必须在
td
的 class 之前访问mat-table
如果您尝试ngMocks.click('table td.clickable-link');
会发生什么情况?
When using mockbuilder, did you .keep(MatTableModule)
?使用 mockbuilder 时,你
.keep(MatTableModule)
吗?
If you don't, it's probably getting mocked, which means your table probably isn't getting rendered.如果你不这样做,它可能会被嘲笑,这意味着你的表格可能没有被渲染。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.