如何用 angular 中的 return 值编写 ag 网格单元格渲染器的单元测试用例
How to write unit test case of ag agrid cellRenderer with return value in angular
TS:
columnDefs = [
{ headerName: 'columnone', field: 'one', width: 120, sortable: true,
cellRenderer: (data) => {
return this.limitCommonUtil.numberFormatter(data?.data?.upperAmount);
}
},
{ headerName: 'columntwo', field: 'two', width: 120, sortable: true,
cellRenderer: (data) => {
return this.limitCommonUtil.dateFormatterDDMMMYYYY(data?.data?.startDate);
}]
HTML:
<ag-grid-angular
[rowData]="(dataStream)"
[columnDefs]="columnDefs"
class="side-panels-container-grid"
[gridOptions]="gridOptionsDefault"
(selectionChanged)="onSelectionChanged()">
</ag-grid-angular>
如何在 jasmine 和 karma 中为 columndef 编写单元测试用例?我无法涵盖 cellRendered return 功能。出现如下错误。
错误:
无法读取未定义的属性(读取 'data')抛出
您在测试中是否正确模拟了网格的数据?如果您只对单元测试 cellRenderer 本身感兴趣,您可以:
const cellRenderer = component.columnDefs.find(c => c.headerName === 'columnone').cellRenderer as any;
expect(cellRenderer({data: { upperAmount: 10 }})).toEqual('Whatever is expected');
TS:
columnDefs = [
{ headerName: 'columnone', field: 'one', width: 120, sortable: true,
cellRenderer: (data) => {
return this.limitCommonUtil.numberFormatter(data?.data?.upperAmount);
}
},
{ headerName: 'columntwo', field: 'two', width: 120, sortable: true,
cellRenderer: (data) => {
return this.limitCommonUtil.dateFormatterDDMMMYYYY(data?.data?.startDate);
}]
HTML:
<ag-grid-angular
[rowData]="(dataStream)"
[columnDefs]="columnDefs"
class="side-panels-container-grid"
[gridOptions]="gridOptionsDefault"
(selectionChanged)="onSelectionChanged()">
</ag-grid-angular>
如何在 jasmine 和 karma 中为 columndef 编写单元测试用例?我无法涵盖 cellRendered return 功能。出现如下错误。
错误: 无法读取未定义的属性(读取 'data')抛出
您在测试中是否正确模拟了网格的数据?如果您只对单元测试 cellRenderer 本身感兴趣,您可以:
const cellRenderer = component.columnDefs.find(c => c.headerName === 'columnone').cellRenderer as any;
expect(cellRenderer({data: { upperAmount: 10 }})).toEqual('Whatever is expected');