gpt4 book ai didi

Angular 单元测试不初始化 ag-Grid

转载 作者:行者123 更新时间:2023-12-02 07:28:27 33 4
gpt4 key购买 nike

我正在尝试对包含 ag-Grid 的组件进行单元测试,但 onGridReady 函数永远不会被调用,因此涉及 ag-Grid 的所有测试都会失败。如何在测试运行之前真正调用 onGridReady?

规范文件:

describe('MyComponent', () => {
let spectator: SpectatorHost<MyComponent>;
let fixture: ComponentFixture<MyComponent>;

const createHost = createHostFactory({
component: MyComponent,
entryComponents: [MyComponent],
imports: [
TranslateModule.forRoot(),
AgGridModule.withComponents([]),
MatTooltipModule,
InlineSVGModule.forRoot(),
MaterialModule,
HMSharedModule,
],
providers: [
{
provide: MatDialogRef,
useValue: [],
},
{
provide: MAT_DIALOG_DATA,
useValue: [],
},
HttpClient,
HttpHandler,
],
});

beforeEach(() => {
spectator = createHost(`<MyComponentSelector></MyComponentSelector>`, {
hostProps: {},
});
fixture = spectator.fixture;
fixture.detectChanges();
});

it('should create', () => {
expect(spectator.component).toBeTruthy();
});

it('should detect changes', () => {
spectator.fixture.detectChanges();

});

“应该创建”有效,但“应该检测更改”失败并出现以下错误:

类型错误:无法读取未定义的属性“setColumnDefs”

在 html 中,这是我的 ag-grid:

          <ag-grid-angular
style="height: 157px"
#agGrid1
class="ag-grid ag-theme-balham"
[rowSelection]="rowSelection"
[columnDefs]="columnDefs1"
[gridOptions]="gridOptions"
(gridReady)="onGridReady($event)"
(gridSizeChanged)="resizeColumns($event)"
(cellValueChanged)="updateBottomGrid('agGrid1', false)"
[singleClickEdit]="true"
>
</ag-grid-angular>

这是 onGridReady:

  onGridReady(params: any): void {
console.log('onGridReady called successfully');
if (this.agGrid1) {
this.agGrid1.api = params.api;
}
}

控制台语句永远不会被打印,因此 onGridReady 永远不会被调用。如何在单元测试之前调用它?

编辑:有人提到我需要为columnDefs提供数据,但我已经有定义所有列的initializeColumnDefs(),并且我已经确认在单元测试期间确实会调用它,所以这不是问题。

另外,我认为这不相关,但这个组件是一个 MatDialog。

最佳答案

我可以通过添加以下内容来消除此错误:

从'@ag-grid-community/client-side-row-model'导入{ClientSideRowModelModule};

模块:模块[] = [ClientSideRowModelModule];

并将模块添加到网格中

          <ag-grid-angular
style="height: 157px"
#agGrid1
class="ag-grid ag-theme-balham"
[rowSelection]="rowSelection"
[columnDefs]="columnDefs1"
[gridOptions]="gridOptions"
(gridReady)="onGridReady($event)"
(gridSizeChanged)="resizeColumns($event)"
(cellValueChanged)="updateBottomGrid('agGrid1', false)"
[singleClickEdit]="true"
[modules]="modules"
>
</ag-grid-angular>

我不知道为什么会解决这个问题。

关于Angular 单元测试不初始化 ag-Grid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60730168/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com