Expose angularized debounce
through provider function. (#15149)
This commit is contained in:
parent
3bea0929ae
commit
0317cc6bd6
|
@ -2,17 +2,20 @@
|
|||
import sinon from 'sinon';
|
||||
import expect from 'expect.js';
|
||||
import ngMock from 'ng_mock';
|
||||
import { DebounceProvider } from 'ui/debounce';
|
||||
|
||||
let debounce;
|
||||
let debounceFromProvider;
|
||||
let $timeout;
|
||||
|
||||
function init() {
|
||||
ngMock.module('kibana');
|
||||
|
||||
ngMock.inject(function ($injector, _$timeout_) {
|
||||
ngMock.inject(function ($injector, _$timeout_, Private) {
|
||||
$timeout = _$timeout_;
|
||||
|
||||
debounce = $injector.get('debounce');
|
||||
debounceFromProvider = Private(DebounceProvider);
|
||||
|
||||
// ensure there is a clean slate before testing deferred tasks
|
||||
$timeout.flush();
|
||||
|
@ -22,54 +25,100 @@ function init() {
|
|||
describe('debounce service', function () {
|
||||
let spy;
|
||||
beforeEach(function () {
|
||||
spy = sinon.spy(function () {});
|
||||
spy = sinon.spy();
|
||||
init();
|
||||
});
|
||||
|
||||
describe('API', function () {
|
||||
it('should have a cancel method', function () {
|
||||
const bouncer = debounce(function () {}, 100);
|
||||
expect(bouncer).to.have.property('cancel');
|
||||
});
|
||||
it('should have a cancel method', function () {
|
||||
const bouncer = debounce(() => {}, 100);
|
||||
const bouncerFromProvider = debounceFromProvider(() => {}, 100);
|
||||
|
||||
expect(bouncer).to.have.property('cancel');
|
||||
expect(bouncerFromProvider).to.have.property('cancel');
|
||||
});
|
||||
|
||||
describe('delayed execution', function () {
|
||||
const sandbox = sinon.sandbox.create();
|
||||
|
||||
beforeEach(() => sandbox.useFakeTimers());
|
||||
afterEach(() => sandbox.restore());
|
||||
|
||||
it('should delay execution', function () {
|
||||
const bouncer = debounce(spy, 100);
|
||||
const bouncerFromProvider = debounceFromProvider(spy, 100);
|
||||
|
||||
bouncer();
|
||||
expect(spy.callCount).to.be(0);
|
||||
sinon.assert.notCalled(spy);
|
||||
$timeout.flush();
|
||||
expect(spy.callCount).to.be(1);
|
||||
sinon.assert.calledOnce(spy);
|
||||
|
||||
spy.reset();
|
||||
|
||||
bouncerFromProvider();
|
||||
sinon.assert.notCalled(spy);
|
||||
$timeout.flush();
|
||||
sinon.assert.calledOnce(spy);
|
||||
});
|
||||
|
||||
it('should fire on leading edge', function () {
|
||||
const bouncer = debounce(spy, 100, { leading: true });
|
||||
const bouncerFromProvider = debounceFromProvider(spy, 100, { leading: true });
|
||||
|
||||
bouncer();
|
||||
expect(spy.callCount).to.be(1);
|
||||
sinon.assert.calledOnce(spy);
|
||||
$timeout.flush();
|
||||
expect(spy.callCount).to.be(2);
|
||||
sinon.assert.calledTwice(spy);
|
||||
|
||||
spy.reset();
|
||||
|
||||
bouncerFromProvider();
|
||||
sinon.assert.calledOnce(spy);
|
||||
$timeout.flush();
|
||||
sinon.assert.calledTwice(spy);
|
||||
});
|
||||
|
||||
it('should only fire on leading edge', function () {
|
||||
const bouncer = debounce(spy, 100, { leading: true, trailing: false });
|
||||
const bouncerFromProvider = debounceFromProvider(spy, 100, { leading: true, trailing: false });
|
||||
|
||||
bouncer();
|
||||
expect(spy.callCount).to.be(1);
|
||||
sinon.assert.calledOnce(spy);
|
||||
$timeout.flush();
|
||||
expect(spy.callCount).to.be(1);
|
||||
sinon.assert.calledOnce(spy);
|
||||
|
||||
spy.reset();
|
||||
|
||||
bouncerFromProvider();
|
||||
sinon.assert.calledOnce(spy);
|
||||
$timeout.flush();
|
||||
sinon.assert.calledOnce(spy);
|
||||
});
|
||||
|
||||
it('should reset delayed execution', function (done) {
|
||||
it('should reset delayed execution', function () {
|
||||
const cancelSpy = sinon.spy($timeout, 'cancel');
|
||||
const bouncer = debounce(spy, 100);
|
||||
const bouncerFromProvider = debounceFromProvider(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);
|
||||
sandbox.clock.tick(1);
|
||||
|
||||
bouncer();
|
||||
sinon.assert.notCalled(spy);
|
||||
$timeout.flush();
|
||||
sinon.assert.calledOnce(spy);
|
||||
sinon.assert.calledOnce(cancelSpy);
|
||||
|
||||
spy.reset();
|
||||
cancelSpy.reset();
|
||||
|
||||
bouncerFromProvider();
|
||||
sandbox.clock.tick(1);
|
||||
|
||||
bouncerFromProvider();
|
||||
sinon.assert.notCalled(spy);
|
||||
$timeout.flush();
|
||||
sinon.assert.calledOnce(spy);
|
||||
sinon.assert.calledOnce(cancelSpy);
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -77,10 +126,22 @@ describe('debounce service', function () {
|
|||
it('should cancel the $timeout', function () {
|
||||
const cancelSpy = sinon.spy($timeout, 'cancel');
|
||||
const bouncer = debounce(spy, 100);
|
||||
const bouncerFromProvider = debounceFromProvider(spy, 100);
|
||||
|
||||
bouncer();
|
||||
bouncer.cancel();
|
||||
expect(cancelSpy.callCount).to.be(1);
|
||||
$timeout.verifyNoPendingTasks(); // throws if pending timeouts
|
||||
sinon.assert.calledOnce(cancelSpy);
|
||||
// throws if pending timeouts
|
||||
$timeout.verifyNoPendingTasks();
|
||||
|
||||
cancelSpy.reset();
|
||||
|
||||
bouncerFromProvider();
|
||||
bouncerFromProvider.cancel();
|
||||
sinon.assert.calledOnce(cancelSpy);
|
||||
// throws if pending timeouts
|
||||
$timeout.verifyNoPendingTasks();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
@ -50,3 +50,7 @@ module.service('debounce', ['$timeout', function ($timeout) {
|
|||
return debounce;
|
||||
};
|
||||
}]);
|
||||
|
||||
export function DebounceProvider(debounce) {
|
||||
return debounce;
|
||||
}
|
||||
|
|
|
@ -1 +1,3 @@
|
|||
import './debounce';
|
||||
|
||||
export { DebounceProvider } from './debounce';
|
||||
|
|
Loading…
Reference in a new issue