X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=tests%2Fkarma%2Funit%2FcrmUtilSpec.js;h=99a5b2f2af0f0f75e972c7432f9110e12aee0510;hb=90e32c05e671eef87b4e839d507a87102f75cec7;hp=3e32dbf06dae0d72e47f915c65c5870d6e4d119f;hpb=26c3d90804afbe06e861a3141e18d7d73e86a24e;p=civicrm-core.git diff --git a/tests/karma/unit/crmUtilSpec.js b/tests/karma/unit/crmUtilSpec.js index 3e32dbf06d..99a5b2f2af 100644 --- a/tests/karma/unit/crmUtilSpec.js +++ b/tests/karma/unit/crmUtilSpec.js @@ -145,4 +145,64 @@ describe('crmUtil', function() { }); }); + describe('crmThrottle', function() { + var crmThrottle, $q, $timeout, i; + + beforeEach(inject(function(_crmThrottle_, _$q_, _$timeout_) { + crmThrottle = _crmThrottle_; + $q = _$q_; + $timeout = _$timeout_; + })); + + function resolveAfterTimeout() { + var dfr = $q.defer(); + $timeout(function(){ + dfr.resolve(i++); + }, 80); + return dfr.promise; + } + + it('executes the function once', function() { + i = 0; + crmThrottle(resolveAfterTimeout); + expect(i).toBe(0); + $timeout.flush(100); + expect(i).toBe(1); + $timeout.verifyNoPendingTasks(); + }); + + it('executes the function again', function() { + i = 0; + crmThrottle(resolveAfterTimeout); + $timeout.flush(100); + expect(i).toBe(1); + crmThrottle(resolveAfterTimeout); + $timeout.flush(20); + expect(i).toBe(1); + $timeout.flush(100); + expect(i).toBe(2); + $timeout.verifyNoPendingTasks(); + }); + + it('executes the first and last function', function() { + i = 0; + crmThrottle(resolveAfterTimeout); + $timeout.flush(10); + crmThrottle(resolveAfterTimeout); + crmThrottle(resolveAfterTimeout); + crmThrottle(resolveAfterTimeout); + crmThrottle(resolveAfterTimeout); + expect(i).toBe(0); + $timeout.flush(100); + expect(i).toBe(1); + $timeout.flush(100); + $timeout.flush(100); + $timeout.flush(100); + $timeout.flush(100); + expect(i).toBe(2); + $timeout.verifyNoPendingTasks(); + }); + + }); + });