自定义油门单元测试

我有以下测试:


import { throttle } from "./event";


jest.useFakeTimers();


it('mock setTimeout test', () => {

    const mockedFunction = jest.fn();

    throttle(mockedFunction, 1);

    expect(setTimeout).toHaveBeenLastCalledWith(mockedFunction, 1000);

    expect(setTimeout).toHaveBeenCalledTimes(1);

});

油门在哪里:


export const throttle = (func, wait) => {

    let timer = null;

    return function(...args) {

        if (timer === null) {

            timer = setTimeout(() => {

                func.apply(this, args);

                timer = null;

            }, wait); 

        }

    };

};

但测试失败:

http://img1.mukewang.com/60b8a9640001c07206410226.jpg

浮云间
浏览 110回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript