有人在 Angular 2 中对 ag-grid 组件进行过单元测试吗?
对我来说,测试用例运行时 this.gridOptions.api 仍然未定义。
很抱歉参加聚会有点晚了,但几天前我一直在寻找这个问题的答案,所以想为最终来到这里的其他人留下答案。正如 Minh 上面提到的,现代的等价物$digest
确实需要运行才能使 ag-grid api 可用。
这是因为在之后onGridReady()
已运行,您可以通过参数访问 api,如下所示。当带有网格的组件初始化时,它会自动运行。假设它是在网格中定义的(gridReady)="onGridReady($event)"
public onGridReady(params)
{
this.gridOptions = params;
}
现在这意味着您可以访问this.gridOptions.api
并且它将被定义,您需要通过运行在测试中重新创建它detectChanges()
。以下是我如何让它为我的项目工作。
fixture = TestBed.createComponent(TestComponent);
component = fixture.componentInstance;
fixture.detectChanges(); // This will ensure the onGridReady(); is called
这应该会导致.api
在运行测试时定义。这是 Angular 6。
有时,测试可能必须执行等待或勾选:
it('should test the grid', fakeAsync( async () => {
// also try tick(ms) if a lot of data is being tested
// try to keep test rows as short as possible
// this line seems essential at times for onGridReady to be processed.
await fixture.whenStable();
// perform your expects...after the await
}));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)