我正在尝试测试农业网格功能,但无法触发事件。
it('should open Component on double click', () => {
component.gridOptions.api.setRowData(ordersStub);
fixture.detectChanges();
const node = component.gridOptions.api.getRowNode(ordersStub[0].id);
const nodeData = node.data;
const element = (fixture.debugElement.nativeElement as HTMLElement).querySelector(`[row-id="${nodeData.id}"]`);
const onRowDoubleClickedSpy = spyOn(component, 'onRowDoubleClicked');
element.dispatchEvent(new MouseEvent('dblclick', { bubbles: true, view: window, cancelable: true }));
expect(onRowDoubleClickedSpy).toHaveBeenCalled();
});
我在做什么错了?
答案 0 :(得分:0)
您调度的事件看起来不错。因此,我认为您需要在调度事件与期望调用组件函数之间有一个延迟:
it('should open Component on double click', (done) => {
...
element.dispatchEvent(new MouseEvent('dblclick', { bubbles: true, view: window, cancelable: true }));
setTimeout(() => {
expect(onRowDoubleClickedSpy).toHaveBeenCalled();
done();
});
});
您还可以使用异步测试:
it('should open Component on double click', async(async() => {
...
element.dispatchEvent(new MouseEvent('dblclick', { bubbles: true, view: window, cancelable: true }));
await fixture.whenStable();
expect(onRowDoubleClickedSpy).toHaveBeenCalled();
}));