[英]Jest test not passing for debounce function
我有這個 function
import _ from 'underscore';
const configMap = {};
export function someFunctionName(someValue, dispatch, reduxAction) {
if (!configMap[someValue]) {
configMap[someValue] = _.debounce(
(someValue) => dispatch(reduxAction(someValue)),
1000,
);
}
return configMap[someValue];
}
開玩笑的測試:
const dispatchMock = jest.fn();
const reduxAction = jest.fn().mockReturnValue({});
jest.useFakeTimers();
describe('someFunctionName', () => {
it('should dispatch reduxAction', async () => {
someFunctionName('value', dispatchMock, reduxAction);
jest.runAllTimers();
expect(reduxAction).toHaveBeenCalled();
});
});
測試一直失敗,我不確定為什么。 我最初認為可能是 debounce 方法需要模擬,但這似乎並不能解決問題。
Lodash 的debounce
function 是異步的,這意味着debounce
的返回語句將在someFunctionName
返回一個值之前執行。 此外,function inside debounce
在提供的 1000 毫秒等待時間過去之前不會運行。
在返回之前,您需要添加異步邏輯以等待someFunctionName
中的debounce
結果。 This SO post可能會提供有關如何實現這一目標的進一步幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.