diff --git a/js/utils/throttle.js b/js/utils/throttle.js index cd7a2a08..6c74e88b 100644 --- a/js/utils/throttle.js +++ b/js/utils/throttle.js @@ -6,6 +6,12 @@ * @param {function} func function to invoke */ function throttle(throttleTime, func) { + if (typeof throttleTime !== 'number' || throttleTime <= 0) { + throw Error('throttle: invalid throttleTime arg, must be a number: ' + throttleTime); + } + if (typeof func !== 'function') { + throw Error('throttle: invalid func arg, must be a function: ' + func); + } let timer, lastInvoke = 0; return function() { let args = arguments; diff --git a/tests/utils/throttle.test.js b/tests/utils/throttle.test.js index 35bb0c41..719725e3 100644 --- a/tests/utils/throttle.test.js +++ b/tests/utils/throttle.test.js @@ -55,6 +55,35 @@ describe('throttle tests', function() { expect(callback.mock.calls.length).toBe(2); }); + describe('expect to throw exception', function() { + it('when calling throttle with time equal to zero', function(done) { + try { + throttle(0, function() {}); + } catch(error) { + expect(error.message).toBeDefined(); + done(); + } + }); + + it('when calling throttle with time less than zero', function(done) { + try { + throttle(-1, function() {}); + } catch(error) { + expect(error.message).toBeDefined(); + done(); + } + }); + + it('when calling throttle without a function callback', function(done) { + try { + throttle(1, 'not a func'); + } catch(error) { + expect(error.message).toBeDefined(); + done(); + } + }); + }); + afterEach(function() { // restore orig Date.now = origNow;