【发布时间】:2019-11-07 21:05:20
【问题描述】:
我正在尝试模拟 debounce,以便我可以在单元测试中测试 debounce 函数,但它告诉我函数 is not a function
错误:
TypeError: (0 , _usersDialog.debounceUpdateSearchText) is not a function
功能:
export const debounceUpdateSearchText = debounce(
(updateText, searchString) => {
if (searchString === '' || searchString.length === 1) {
updateText(' ');
}
updateText(searchString);
},
500
);
测试代码:
// earlier in the file
import debounce from 'lodash/debounce';
jest.mock('lodash/debounce');
// test
it('updates the search text', () => {
// jest.useFakeTimers();
debounce.mockImplementation(fn => fn);
const updateText = jest.fn();
// call function
debounceUpdateSearchText(updateText, 'fuego');
// jest.advanceTimersByTime(501);
expect(props.updateText).toHaveBeenCalledWith('fuego');
});
【问题讨论】:
-
如果你导出你的非去抖动函数,你就不必模拟任何东西了。
标签: javascript unit-testing jestjs lodash enzyme