Angular 单元测试未初始化 ag-Grid
Angular unit test doesn't initialize ag-Grid
我正在尝试对包含 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();
});
"should create" 有效,但 "should detect changes" 失败并出现此错误:
TypeError: Cannot read property 'setColumnDefs' of undefined
在 html 中,这是我的农业网格:
<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。
我能够通过添加来摆脱这个错误:
import { ClientSideRowModelModule } from '@ag-grid-community/client-side-row-model';
modules: Module[] = [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>
我不知道为什么修复它。
以下是有关如何使用模块的文档:https://www.ag-grid.com/javascript-grid-modules/。您需要注册要使用的模块才能使 ag-grid 正常工作。
我正在尝试对包含 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();
});
"should create" 有效,但 "should detect changes" 失败并出现此错误:
TypeError: Cannot read property 'setColumnDefs' of undefined
在 html 中,这是我的农业网格:
<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。
我能够通过添加来摆脱这个错误:
import { ClientSideRowModelModule } from '@ag-grid-community/client-side-row-model';
modules: Module[] = [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>
我不知道为什么修复它。
以下是有关如何使用模块的文档:https://www.ag-grid.com/javascript-grid-modules/。您需要注册要使用的模块才能使 ag-grid 正常工作。