【发布时间】:2019-03-25 19:40:56
【问题描述】:
寻找一些关于如何在 lodash 的 debounce 中模拟 .cancel 方法的建议。
我有一个函数调用debounce,然后使用返回的去抖动值调用debouncedThing.cancel()。
我可以在我的测试中很好地模拟debounce,除非我的函数被称为.cancel()。
在我目前正在进行的单元测试的顶部:
jest.mock('lodash/debounce', () => fn => fn));
除了我打电话给debouncedThing.cancel() 的地方之外,上面的模拟运行良好。在那些测试中,我得到一个错误,debouncedThing.cancel() 不是函数。
我在哪里使用 debounce 的伪代码如下所示:
const debouncedThing = debounce(
(myFunc, data) => myFunc(data),
DEBOUNCE_DELAY_TIME,
);
const otherFunc = () => {
/* omitted */
debouncedThing.cancel();
}
【问题讨论】:
标签: mocking jestjs lodash debounce