在 Angular 和 Jest 的测试中未调用 AG-Grid gridReady
AG-Grid gridReady not being called on tests with Angular and Jest
Jest 测试中未调用 Angular AG Grid 的 gridReady
方法。
你知道为什么吗?
这是我的测试代码:
import {TestBed} from "@angular/core/testing";
import {BrowserDynamicTestingModule, platformBrowserDynamicTesting} from "@angular/platform-browser-dynamic/testing";
import {AppComponent} from "./app.component";
import {FormsModule} from "@angular/forms";
import {AgGridModule} from "ag-grid-angular";
describe('AG Grid', () => {
it('calls grid ready', async()=> {
TestBed.resetTestEnvironment();
TestBed.initTestEnvironment(BrowserDynamicTestingModule,
platformBrowserDynamicTesting());
await TestBed.configureTestingModule({
imports: [
FormsModule,
AgGridModule.withComponents([]),
],
declarations: [AppComponent]
}).compileComponents();
const fixture = TestBed.createComponent(AppComponent);
const component = fixture.componentInstance;
fixture.detectChanges();
expect(component.api).toBeDefined();
expect(component.columnApi).toBeDefined();
});
})
问题在fixture.detectChanges()
后使用await fixture.whenStable()
解决
Jest 测试中未调用 Angular AG Grid 的 gridReady
方法。
你知道为什么吗?
这是我的测试代码:
import {TestBed} from "@angular/core/testing";
import {BrowserDynamicTestingModule, platformBrowserDynamicTesting} from "@angular/platform-browser-dynamic/testing";
import {AppComponent} from "./app.component";
import {FormsModule} from "@angular/forms";
import {AgGridModule} from "ag-grid-angular";
describe('AG Grid', () => {
it('calls grid ready', async()=> {
TestBed.resetTestEnvironment();
TestBed.initTestEnvironment(BrowserDynamicTestingModule,
platformBrowserDynamicTesting());
await TestBed.configureTestingModule({
imports: [
FormsModule,
AgGridModule.withComponents([]),
],
declarations: [AppComponent]
}).compileComponents();
const fixture = TestBed.createComponent(AppComponent);
const component = fixture.componentInstance;
fixture.detectChanges();
expect(component.api).toBeDefined();
expect(component.columnApi).toBeDefined();
});
})
问题在fixture.detectChanges()
await fixture.whenStable()
解决