我正在尝试模拟去抖动,以便可以在单元测试中测试去抖动的功能,但是它告诉我功能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');
});
答案 0 :(得分:0)
原来,我的防抖模拟仅通过触摸即可正常工作的代码即可关闭:
import debounce from 'lodash/debounce';
jest.mock('lodash/debounce', () => jest.fn(fn => fn));