@spalger/kibana
Version:
Kibana is an open source (Apache Licensed), browser based analytics and search dashboard for Elasticsearch. Kibana is a snap to setup and start using. Kibana strives to be easy to get started with, while also being flexible and powerful, just like Elastic
86 lines (73 loc) • 2.23 kB
JavaScript
var sinon = require('auto-release-sinon');
var expect = require('expect.js');
var ngMock = require('ngMock');
var debounce;
var $timeout;
var $timeoutSpy;
function init() {
ngMock.module('kibana');
ngMock.inject(function ($injector, _$timeout_) {
$timeout = _$timeout_;
$timeoutSpy = sinon.spy($timeout);
debounce = $injector.get('debounce');
});
}
describe('debounce service', function () {
var spy;
beforeEach(function () {
spy = sinon.spy(function () {});
init();
});
describe('API', function () {
it('should have a cancel method', function () {
var bouncer = debounce(function () {}, 100);
expect(bouncer).to.have.property('cancel');
});
});
describe('delayed execution', function () {
it('should delay execution', function () {
var bouncer = debounce(spy, 100);
bouncer();
expect(spy.callCount).to.be(0);
$timeout.flush();
expect(spy.callCount).to.be(1);
});
it('should fire on leading edge', function () {
var bouncer = debounce(spy, 100, { leading: true });
bouncer();
expect(spy.callCount).to.be(1);
$timeout.flush();
expect(spy.callCount).to.be(2);
});
it('should only fire on leading edge', function () {
var bouncer = debounce(spy, 100, { leading: true, trailing: false });
bouncer();
expect(spy.callCount).to.be(1);
$timeout.flush();
expect(spy.callCount).to.be(1);
});
it('should reset delayed execution', function (done) {
var cancelSpy = sinon.spy($timeout, 'cancel');
var bouncer = debounce(spy, 100);
bouncer();
setTimeout(function () {
bouncer();
expect(spy.callCount).to.be(0);
$timeout.flush();
expect(spy.callCount).to.be(1);
expect(cancelSpy.callCount).to.be(1);
done();
}, 1);
});
});
describe('cancel', function () {
it('should cancel the $timeout', function () {
var cancelSpy = sinon.spy($timeout, 'cancel');
var bouncer = debounce(spy, 100);
bouncer();
bouncer.cancel();
expect(cancelSpy.callCount).to.be(1);
$timeout.verifyNoPendingTasks(); // throws if pending timeouts
});
});
});