ag-Grid:如何从单元测试访问 gridApi?
ag-Grid: How can I access the gridApi from a Unit Test?
我正在尝试对包含对 gridApi 的调用的函数进行单元测试,例如 this.gridApi.getSelectedRows();
。当单元测试到这一行时,它给出了这个错误:Failed: Cannot read property 'getSelectedRows' of undefined
,因为gridApi还没有被定义。
如何从单元测试中定义 gridApi?它通常在 onGridReady 函数中这样定义:
onGridReady(params) {
this.gridApi = params.api;
this.gridColumnApi = params.columnApi;
this.gridApi.sizeColumnsToFit();
}
是否可以从单元测试中触发 onGridReady
?我不知道可以向它发送什么参数。有没有其他方法来定义 this.gridApi
呢?
this.gridApi 和 this.gridColumnApi 在测试用例中自动初始化。为此需要从 'ag-grid-angular' 导入 AgGridModule。
并初始化组件,与以下代码相同:
let component: MyComponent;
let fixture: ComponentFixture<MyComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [MyComponent],
imports: [
AgGridModule.withComponents([])
]
}).compileComponents();
fixture = TestBed.createComponent(MyComponent);
component = fixture.componentInstance;
fixture.detectChanges();
}));
在文件顶部测试 onGridReady Import。
import { GridOptions } from "ag-grid-community";
并声明一个变量
gridOptions: GridOptions = <GridOptions>{};
编写测试用例
it('onGridReady called', () => {
const data = {
api: component.gridOptions.api,
columnApi: component.gridOptions.columnApi
}
component.onGridReady(params);
expect(component.gridApi).not.toBeNull();
expect(component.gridColumnApi).not.toBeNull();
});
我正在尝试对包含对 gridApi 的调用的函数进行单元测试,例如 this.gridApi.getSelectedRows();
。当单元测试到这一行时,它给出了这个错误:Failed: Cannot read property 'getSelectedRows' of undefined
,因为gridApi还没有被定义。
如何从单元测试中定义 gridApi?它通常在 onGridReady 函数中这样定义:
onGridReady(params) {
this.gridApi = params.api;
this.gridColumnApi = params.columnApi;
this.gridApi.sizeColumnsToFit();
}
是否可以从单元测试中触发 onGridReady
?我不知道可以向它发送什么参数。有没有其他方法来定义 this.gridApi
呢?
this.gridApi 和 this.gridColumnApi 在测试用例中自动初始化。为此需要从 'ag-grid-angular' 导入 AgGridModule。 并初始化组件,与以下代码相同:
let component: MyComponent;
let fixture: ComponentFixture<MyComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [MyComponent],
imports: [
AgGridModule.withComponents([])
]
}).compileComponents();
fixture = TestBed.createComponent(MyComponent);
component = fixture.componentInstance;
fixture.detectChanges();
}));
在文件顶部测试 onGridReady Import。
import { GridOptions } from "ag-grid-community";
并声明一个变量
gridOptions: GridOptions = <GridOptions>{};
编写测试用例
it('onGridReady called', () => {
const data = {
api: component.gridOptions.api,
columnApi: component.gridOptions.columnApi
}
component.onGridReady(params);
expect(component.gridApi).not.toBeNull();
expect(component.gridColumnApi).not.toBeNull();
});