Skip to content

Instantly share code, notes, and snippets.

@sheltonial
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sheltonial/eed85da3101935d11188 to your computer and use it in GitHub Desktop.
Save sheltonial/eed85da3101935d11188 to your computer and use it in GitHub Desktop.
angular debounce service tests
/*
The following code tests functionality for the following angular implementation of debounce:
http://stackoverflow.com/a/13320016/2415971
app.factory('debounce', ['$timeout','$q', function($timeout, $q) {
return function debounce(func, wait, immediate) {
var timeout;
var deferred = $q.defer();
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if(!immediate) {
deferred.resolve(func.apply(context, args));
deferred = $q.defer();
}
};
var callNow = immediate && !timeout;
if ( timeout ) {
$timeout.cancel(timeout);
}
timeout = $timeout(later, wait);
if (callNow) {
deferred.resolve(func.apply(context,args));
deferred = $q.defer();
}
return deferred.promise;
};
};
}]);
*/
'use strict';
describe('Service: debounce', function() {
beforeEach(module('the.name.of.the.angular.module.goes.here'));
var $rootScope, $q, $timeout, debounce;
beforeEach(function () {
inject(function ($injector) {
$rootScope = $injector.get('$rootScope');
$q = $injector.get('$q');
$timeout = $injector.get('$timeout');
debounce = $injector.get('debounce');
});
});
it('should execute function passed to debounce', function() {
var callback = jasmine.createSpy('callback');
var debounceFn = debounce(callback);
debounceFn();
$timeout.flush();
expect(callback).toHaveBeenCalled();
});
describe('wait', function() {
it('should not execute the function before the wait time has passed', function() {
var callback = jasmine.createSpy('callback');
var debounceFn = debounce(callback, 100);
debounceFn();
$timeout.flush(99);
expect(callback).not.toHaveBeenCalled();
});
it('should execute the function when the wait time has passed', function() {
var callback = jasmine.createSpy('callback');
var debounceFn = debounce(callback, 100);
debounceFn();
$timeout.flush(100);
expect(callback).toHaveBeenCalled();
});
it('should reset wait time when subsequent debounce called within wait time', function() {
var callback = jasmine.createSpy('callback');
var debounceFn = debounce(callback, 100);
debounceFn();
$timeout.flush(99);
debounceFn();
$timeout.flush(1);
expect(callback).not.toHaveBeenCalled();
$timeout.flush(99);
expect(callback).toHaveBeenCalled();
});
it('should execute the trailing edge debounced function', function() {
var callback1 = jasmine.createSpy('callback1');
var callback2 = jasmine.createSpy('callback2');
var debounceFn = debounce(function(callback) {
callback();
}, 100);
debounceFn(callback1);
$timeout.flush(99);
debounceFn(callback2);
$timeout.flush(100);
expect(callback1).not.toHaveBeenCalled();
expect(callback2).toHaveBeenCalled();
});
it('should execute both functions so long as wait time has passed', function() {
var callback1 = jasmine.createSpy('callback1');
var callback2 = jasmine.createSpy('callback2');
var debounceFn = debounce(function(callback) {
callback();
}, 100);
debounceFn(callback1);
$timeout.flush(100);
debounceFn(callback2);
$timeout.flush(100);
expect(callback1).toHaveBeenCalled();
expect(callback2).toHaveBeenCalled();
});
});
describe('immediate', function() {
it('should execute the leading edge debounced function', function() {
var callback1 = jasmine.createSpy('callback1');
var callback2 = jasmine.createSpy('callback2');
var debounceFn = debounce(function(callback) {
callback();
}, 100, true);
debounceFn(callback1);
$timeout.flush(99);
debounceFn(callback2);
$timeout.flush(100);
expect(callback1).toHaveBeenCalled();
expect(callback2).not.toHaveBeenCalled();
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment