This reverts commit 5ea0436051.
This commit is contained in:
@@ -1,21 +0,0 @@
|
||||
// Returns a function, that, as long as it continues to be invoked, will not
|
||||
// be triggered. The function will be called after it stops being called for
|
||||
// N milliseconds. If `immediate` is passed, trigger the function on the
|
||||
// leading edge, instead of the trailing.
|
||||
|
||||
export const debounce = (func, delay, immediate) => {
|
||||
let timerId;
|
||||
return (...args) => {
|
||||
const boundFunc = func.bind(this, ...args);
|
||||
clearTimeout(timerId);
|
||||
if (immediate && !timerId) {
|
||||
boundFunc();
|
||||
}
|
||||
const calleeFunc = immediate
|
||||
? () => {
|
||||
timerId = null;
|
||||
}
|
||||
: boundFunc;
|
||||
timerId = setTimeout(calleeFunc, delay);
|
||||
};
|
||||
};
|
||||
@@ -1,32 +0,0 @@
|
||||
import { debounce } from '../TimeHelpers';
|
||||
// Tell Jest to mock all timeout functions
|
||||
jest.useFakeTimers('modern');
|
||||
|
||||
describe('debounce', () => {
|
||||
test('execute just once with immediate false', () => {
|
||||
let func = jest.fn();
|
||||
let debouncedFunc = debounce(func, 1000);
|
||||
|
||||
for (let i = 0; i < 100; i += 1) {
|
||||
debouncedFunc();
|
||||
}
|
||||
|
||||
// Fast-forward time
|
||||
jest.runAllTimers();
|
||||
|
||||
expect(func).toBeCalledTimes(1);
|
||||
});
|
||||
|
||||
test('execute just once with immediate true', () => {
|
||||
let func = jest.fn();
|
||||
let debouncedFunc = debounce(func, 1000, true);
|
||||
|
||||
for (let i = 0; i < 100; i += 1) {
|
||||
debouncedFunc();
|
||||
}
|
||||
|
||||
jest.runAllTimers();
|
||||
|
||||
expect(func).toBeCalledTimes(1);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user