检查在单元测试中定义的gridOptions.api后触发onGridReady事件时,gridOptions.api为null

时间:2020-09-26 15:02:24

标签: angular unit-testing karma-jasmine ag-grid ag-grid-angular

在我的<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://${db.host}:${db.port}/${db.db}"/> <property name="username" value="${db.username}"/> <property name="password" value="${db.password}"/> <property name="connectionProperties"> <props> <prop key="allowMultiQueries">true</prop> <prop key="useUnicode">yes</prop> <prop key="characterEncoding">utf8</prop> <prop key="autoReconnect">true</prop> <prop key="failOverReadOnly">false</prop> <prop key="maxReconnects">10</prop> </props> </property> </bean> 事件中,我将onGridReady分配给api

this.gridApi

我有一种使用private onGridReady(gridReadyEvent: GridReadyEvent): void { this.gridApi = gridReadyEvent.api; } 的方法。

this.gridApi

我正在为public getGridState(): GridState { if (this.gridApi) { // code to get state } } 编写单元测试。

单元测试

getGridState

因此,为了确保定义了it('should be able to get state of grid', async () => { waitForGridApiToBeAvailable(component.gridOptions, () => { component.getGridState(); }); }); ,我使用的是ag-Grid unit testing doc中提到的this.gridApi。因此,这意味着在定义waitForGridApiToBeAvailable时,gridOptions.api也是如此,对吗?

问题

运行单元测试时,只有在定义了this.gridApi之后,component.getGridState()才会调用component.gridOptions.api,而this.gridApi中的undefinedgetGridState()

除此之外,令我更困惑的是,onGridReady()在调用getGridState()之后被调用,而当我安慰this.gridOptions.api时,结果是{{1} }。在检查null被定义并在this.gridOptions.api之后被调用null之后,从单元测试中调用getGridState()时,component.gridOptions.api如何成为onGridReady()

注意:我已尽力解释得尽可能清楚。让我知道是否需要更多解释。

0 个答案:

没有答案