Skip to content

Commit

Permalink
CRM-19829 - Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
colemanw committed Jan 5, 2017
1 parent faf62f6 commit b6204d4
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions tests/karma/unit/crmUtilSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,75 @@ describe('crmUtil', function() {
});
});

describe('crmThrottle', function() {
var crmThrottle, $q, $rootScope, $timeout, i;

beforeEach(inject(function(_crmThrottle_, _$rootScope_, _$q_, _$timeout_) {
crmThrottle = _crmThrottle_;
$rootScope = _$rootScope_;
$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);
$rootScope.$apply();
$timeout.flush(100);
expect(i).toBe(1);
$timeout.verifyNoPendingTasks();
});

it('executes the function again', function() {
i = 0;
crmThrottle(resolveAfterTimeout);
$rootScope.$apply();
$timeout.flush(100);
expect(i).toBe(1);
crmThrottle(resolveAfterTimeout);
$rootScope.$apply();
$timeout.flush(20);
expect(i).toBe(1);
$rootScope.$apply();
$timeout.flush(100);
expect(i).toBe(2);
$timeout.verifyNoPendingTasks();
});

it('executes the first and last function', function() {
i = 0;
crmThrottle(resolveAfterTimeout);
$rootScope.$apply();
$timeout.flush(10);
crmThrottle(resolveAfterTimeout);
crmThrottle(resolveAfterTimeout);
crmThrottle(resolveAfterTimeout);
crmThrottle(resolveAfterTimeout);
expect(i).toBe(0);
$rootScope.$apply();
$timeout.flush(100);
expect(i).toBe(1);
$rootScope.$apply();
$timeout.flush(100);
$rootScope.$apply();
$timeout.flush(100);
$rootScope.$apply();
$timeout.flush(100);
$rootScope.$apply();
$timeout.flush(100);
expect(i).toBe(2);
$timeout.verifyNoPendingTasks();
});

});

});

0 comments on commit b6204d4

Please sign in to comment.