mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-01 15:57:48 +00:00
* test: fix throttle.spec.js Signed-off-by: tison <wander4096@gmail.com> * test: fix pickBy.spec.js Signed-off-by: tison <wander4096@gmail.com> * test: fix isBuffer.spec.js Signed-off-by: tison <wander4096@gmail.com> * test: partially fix attempt.spec.js Signed-off-by: tison <wander4096@gmail.com> * test: partially fix dropRightWhile.spec.js Signed-off-by: tison <wander4096@gmail.com> * test: fix defer.spec.js and rest.spec.js Signed-off-by: tison <wander4096@gmail.com> * test: fix invoke.spec.js Signed-off-by: tison <wander4096@gmail.com> * test: fix isArray.spec.js Signed-off-by: tison <wander4096@gmail.com> * test: partially fix iteration-methods.spec.js Signed-off-by: tison <wander4096@gmail.com> * test: fix xor-methods.spec.js Signed-off-by: tison <wander4096@gmail.com> * test: fix property.spec.js Signed-off-by: tison <wander4096@gmail.com> * test: fix ary.spec.js Signed-off-by: tison <wander4096@gmail.com> * test: fix omit-methods.spec.js Signed-off-by: tison <wander4096@gmail.com> * test: fix debounce-and-throttle.spec.js Signed-off-by: tison <wander4096@gmail.com> * test: fix unzip-and-zip.spec.js Signed-off-by: tison <wander4096@gmail.com> * test: fix toPairs-methods.spec.js Signed-off-by: tison <wander4096@gmail.com> * test: fix exit-early.spec.js Signed-off-by: tison <wander4096@gmail.com> * test: temporarily comment out takeWhile and dropWhile tests Signed-off-by: tison <wander4096@gmail.com> * test: partially fix union*.spec.js Signed-off-by: tison <wander4096@gmail.com> * test: fix startsWith-and-endsWith.spec.js Signed-off-by: tison <wander4096@gmail.com> * test: fix isNil.spec.js Signed-off-by: tison <wander4096@gmail.com> * test: fix some of syntax errors Signed-off-by: tison <wander4096@gmail.com> --------- Signed-off-by: tison <wander4096@gmail.com>
261 lines
6.5 KiB
JavaScript
261 lines
6.5 KiB
JavaScript
import lodashStable, { runInContext } from 'lodash';
|
|
import * as assert from 'assert';
|
|
import { identity, isModularize, argv, isPhantom } from './utils';
|
|
import throttle from '../src/throttle';
|
|
|
|
describe('throttle', () => {
|
|
it('should throttle a function', (done) => {
|
|
let callCount = 0;
|
|
const throttled = throttle(() => {
|
|
callCount++;
|
|
}, 32);
|
|
|
|
throttled();
|
|
throttled();
|
|
throttled();
|
|
|
|
const lastCount = callCount;
|
|
expect(callCount);
|
|
|
|
setTimeout(() => {
|
|
expect(callCount > lastCount);
|
|
done();
|
|
}, 64);
|
|
});
|
|
|
|
it('subsequent calls should return the result of the first call', (done) => {
|
|
const throttled = throttle(identity, 32);
|
|
const results = [throttled('a'), throttled('b')];
|
|
|
|
expect(results).toEqual(['a', 'a']);
|
|
|
|
setTimeout(() => {
|
|
const results = [throttled('c'), throttled('d')];
|
|
assert.notStrictEqual(results[0], 'a');
|
|
assert.notStrictEqual(results[0], undefined);
|
|
|
|
assert.notStrictEqual(results[1], 'd');
|
|
assert.notStrictEqual(results[1], undefined);
|
|
done();
|
|
}, 64);
|
|
});
|
|
|
|
it('should clear timeout when `func` is called', (done) => {
|
|
if (!isModularize) {
|
|
let callCount = 0;
|
|
let dateCount = 0;
|
|
|
|
const lodash = runInContext({
|
|
Date: {
|
|
now: function () {
|
|
return ++dateCount === 5 ? Infinity : +new Date();
|
|
},
|
|
},
|
|
});
|
|
|
|
const throttled = lodash.throttle(() => {
|
|
callCount++;
|
|
}, 32);
|
|
|
|
throttled();
|
|
throttled();
|
|
|
|
setTimeout(() => {
|
|
expect(callCount).toBe(2);
|
|
done();
|
|
}, 64);
|
|
} else {
|
|
done();
|
|
}
|
|
});
|
|
|
|
it('should not trigger a trailing call when invoked once', (done) => {
|
|
let callCount = 0;
|
|
const throttled = throttle(() => {
|
|
callCount++;
|
|
}, 32);
|
|
|
|
throttled();
|
|
expect(callCount).toBe(1);
|
|
|
|
setTimeout(() => {
|
|
expect(callCount).toBe(1);
|
|
done();
|
|
}, 64);
|
|
});
|
|
|
|
lodashStable.times(2, (index) => {
|
|
it(`should trigger a call when invoked repeatedly${
|
|
index ? ' and `leading` is `false`' : ''
|
|
}`, (done) => {
|
|
let callCount = 0;
|
|
const limit = argv || isPhantom ? 1000 : 320;
|
|
const options = index ? { leading: false } : {};
|
|
const throttled = throttle(
|
|
() => {
|
|
callCount++;
|
|
},
|
|
32,
|
|
options,
|
|
);
|
|
|
|
const start = +new Date();
|
|
while (new Date() - start < limit) {
|
|
throttled();
|
|
}
|
|
const actual = callCount > 1;
|
|
setTimeout(() => {
|
|
expect(actual);
|
|
done();
|
|
}, 1);
|
|
});
|
|
});
|
|
|
|
it('should trigger a second throttled call as soon as possible', (done) => {
|
|
let callCount = 0;
|
|
|
|
const throttled = throttle(
|
|
() => {
|
|
callCount++;
|
|
},
|
|
128,
|
|
{ leading: false },
|
|
);
|
|
|
|
throttled();
|
|
|
|
setTimeout(() => {
|
|
expect(callCount).toBe(1);
|
|
throttled();
|
|
}, 192);
|
|
|
|
setTimeout(() => {
|
|
expect(callCount).toBe(1);
|
|
}, 254);
|
|
|
|
setTimeout(() => {
|
|
expect(callCount).toBe(2);
|
|
done();
|
|
}, 384);
|
|
});
|
|
|
|
it('should apply default options', (done) => {
|
|
let callCount = 0;
|
|
const throttled = throttle(
|
|
() => {
|
|
callCount++;
|
|
},
|
|
32,
|
|
{},
|
|
);
|
|
|
|
throttled();
|
|
throttled();
|
|
expect(callCount).toBe(1);
|
|
|
|
setTimeout(() => {
|
|
expect(callCount).toBe(2);
|
|
done();
|
|
}, 128);
|
|
});
|
|
|
|
it('should support a `leading` option', () => {
|
|
const withLeading = throttle(identity, 32, { leading: true });
|
|
expect(withLeading('a')).toBe('a');
|
|
|
|
const withoutLeading = throttle(identity, 32, { leading: false });
|
|
expect(withoutLeading('a')).toBe(undefined);
|
|
});
|
|
|
|
it('should support a `trailing` option', (done) => {
|
|
let withCount = 0;
|
|
let withoutCount = 0;
|
|
|
|
const withTrailing = throttle(
|
|
(value) => {
|
|
withCount++;
|
|
return value;
|
|
},
|
|
64,
|
|
{ trailing: true },
|
|
);
|
|
|
|
const withoutTrailing = throttle(
|
|
(value) => {
|
|
withoutCount++;
|
|
return value;
|
|
},
|
|
64,
|
|
{ trailing: false },
|
|
);
|
|
|
|
expect(withTrailing('a')).toBe('a');
|
|
expect(withTrailing('b')).toBe('a');
|
|
|
|
expect(withoutTrailing('a')).toBe('a');
|
|
expect(withoutTrailing('b')).toBe('a');
|
|
|
|
setTimeout(() => {
|
|
expect(withCount).toBe(2);
|
|
expect(withoutCount).toBe(1);
|
|
done();
|
|
}, 256);
|
|
});
|
|
|
|
it('should not update `lastCalled`, at the end of the timeout, when `trailing` is `false`', (done) => {
|
|
let callCount = 0;
|
|
|
|
const throttled = throttle(
|
|
() => {
|
|
callCount++;
|
|
},
|
|
64,
|
|
{ trailing: false },
|
|
);
|
|
|
|
throttled();
|
|
throttled();
|
|
|
|
setTimeout(() => {
|
|
throttled();
|
|
throttled();
|
|
}, 96);
|
|
|
|
setTimeout(() => {
|
|
expect(callCount > 1);
|
|
done();
|
|
}, 192);
|
|
});
|
|
|
|
it('should work with a system time of `0`', (done) => {
|
|
if (!isModularize) {
|
|
let callCount = 0;
|
|
let dateCount = 0;
|
|
|
|
const lodash = runInContext({
|
|
Date: {
|
|
now: function () {
|
|
return ++dateCount < 4 ? 0 : +new Date();
|
|
},
|
|
},
|
|
});
|
|
|
|
const throttled = lodash.throttle((value) => {
|
|
callCount++;
|
|
return value;
|
|
}, 32);
|
|
|
|
const results = [throttled('a'), throttled('b'), throttled('c')];
|
|
expect(results).toEqual(['a', 'a', 'a']);
|
|
expect(callCount).toBe(1);
|
|
|
|
setTimeout(() => {
|
|
expect(callCount).toBe(2);
|
|
done();
|
|
}, 64);
|
|
} else {
|
|
done();
|
|
}
|
|
});
|
|
});
|