X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=tests%2Fkarma%2Funit%2FcrmUtilSpec.js;h=99a5b2f2af0f0f75e972c7432f9110e12aee0510;hb=82fe8be2496c351b6f155d0b0b7bdc198c66a30a;hp=8c0daedb4fdd7830ab9cbc676a3dfd606fdc4bd1;hpb=82fd7aef5afb57464121d60e237c626cf5752333;p=civicrm-core.git diff --git a/tests/karma/unit/crmUtilSpec.js b/tests/karma/unit/crmUtilSpec.js index 8c0daedb4f..99a5b2f2af 100644 --- a/tests/karma/unit/crmUtilSpec.js +++ b/tests/karma/unit/crmUtilSpec.js @@ -103,4 +103,106 @@ describe('crmUtil', function() { }); }); + + describe('crmQueue', function() { + var crmQueue, $q, $rootScope, $timeout; + + beforeEach(inject(function(_crmQueue_, _$rootScope_, _$q_, _$timeout_) { + crmQueue = _crmQueue_; + $rootScope = _$rootScope_; + $q = _$q_; + $timeout = _$timeout_; + })); + + function addAfterTimeout(a, b, ms) { + var dfr = $q.defer(); + $timeout(function(){ + dfr.resolve(a+b); + }, ms); + return dfr.promise; + } + + it('returns in order', function(done) { + var last = null; + var queuedFunc = crmQueue(addAfterTimeout); + // note: the queueing order is more important the timeout-durations (15ms vs 5ms) + queuedFunc(1, 2, 25).then(function(sum) { + expect(last).toBe(null); + expect(sum).toBe(3); + last = sum; + }); + queuedFunc(3, 4, 5).then(function(sum){ + expect(last).toBe(3); + expect(sum).toBe(7); + last = sum; + done(); + }); + + for (var i = 0; i < 5; i++) { + $rootScope.$apply(); + $timeout.flush(20); + } + }); + }); + + 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(); + }); + + }); + });