我有自定义 ag-grid 侦听器,用于更改过滤器时,我正在尝试测试在侦听器中调用的方法:
public startListener() {
this.gridApi.addEventListener('filterChanged', e => {
if(this.gridId == 'productsGrid') {
this.updateProducts(e);
}
if(this.gridId == 'userGrid') {
this.updateUser(e);
}
});
}
测试:
describe('Listener test', () => {
it('should call updateProducts', () => {
let productCall = jest.spyOn(component, 'updateProducts').mockImplementation();
component.startListener();
component.gridApi.setFilterModel({test: 'test'});
expect(productCall).toHaveBeenCalled();
});
it('should call updateProducts', () => {
let userCall = jest.spyOn(component, 'updateUser').mockImplementation();
component.startListener();
component.gridApi.setFilterModel({test: 'test'});
expect(userCall).toHaveBeenCalled();
});
});
两个测试都建议预计呼叫次数:0