【发布时间】:2018-06-21 04:48:57
【问题描述】:
我正在尝试测试是否在 componentDidMount 中调用了一个方法:
private timerToken;
public componentDidMount() {
this.timerToken = setInterval(() => { this._getWebJobStatus(); }, 2000);
}
test("_getWebJobStatus() is called", () => {
const spy = jest.spyOn(UploadStatus.prototype, "componentDidMount").mockReturnThis();
const wrapper = mount(<testComponent />);
const component = wrapper.instance() as testComponent;
const _getWebJobStatusMock = jest.fn();
component['timerToken'] = _getWebJobStatusMock;
expect(spy).toHaveBeenCalledTimes(1); // this works
expect(_getWebJobStatusMock).toHaveBeenCalled(); //this is not working
});
如何测试 _getWebJobStatus() 是否被调用?
谢谢!
【问题讨论】: