Commit 4801e9a7 authored by Robert Knight's avatar Robert Knight

Remove unused `scope-timeout` module

parent 47c8ea91
/**
* Sets a timeout which is linked to the lifetime of an Angular scope.
*
* When the scope is destroyed, the timeout will be cleared if it has
* not already fired.
*
* The callback is not invoked within a $scope.$apply() context. It is up
* to the caller to do that if necessary.
*
* @param {Scope} $scope - An Angular scope
* @param {Function} fn - Callback to invoke with setTimeout
* @param {number} delay - Delay argument to pass to setTimeout
*/
export default function scopeTimeout(
$scope,
fn,
delay,
setTimeoutFn,
clearTimeoutFn
) {
setTimeoutFn = setTimeoutFn || setTimeout;
clearTimeoutFn = clearTimeoutFn || clearTimeout;
let removeDestroyHandler;
const id = setTimeoutFn(function () {
removeDestroyHandler();
fn();
}, delay);
removeDestroyHandler = $scope.$on('$destroy', function () {
clearTimeoutFn(id);
});
}
import scopeTimeout from '../scope-timeout';
function FakeScope() {
this.listeners = {};
this.$on = function (event, fn) {
this.listeners[event] = this.listeners[event] || [];
this.listeners[event].push(fn);
return function () {
this.listeners[event] = this.listeners[event].filter(function (otherFn) {
return otherFn !== fn;
});
}.bind(this);
};
}
describe('scope-timeout', function () {
let fakeSetTimeout;
let fakeClearTimeout;
beforeEach(function () {
fakeSetTimeout = sinon.stub().returns(42);
fakeClearTimeout = sinon.stub();
});
it('schedules a timeout', function () {
const $scope = new FakeScope();
const callback = sinon.stub();
scopeTimeout($scope, callback, 0, fakeSetTimeout, fakeClearTimeout);
assert.calledOnce(fakeSetTimeout);
const timeoutFn = fakeSetTimeout.args[0][0];
timeoutFn();
assert.called(callback);
});
it('removes the scope listener when the timeout fires', function () {
const $scope = new FakeScope();
const callback = sinon.stub();
scopeTimeout($scope, callback, 0, fakeSetTimeout, fakeClearTimeout);
assert.equal($scope.listeners.$destroy.length, 1);
const timeoutFn = fakeSetTimeout.args[0][0];
timeoutFn();
assert.equal($scope.listeners.$destroy.length, 0);
});
it('clears the timeout when the scope is destroyed', function () {
const $scope = new FakeScope();
const callback = sinon.stub();
scopeTimeout($scope, callback, 0, fakeSetTimeout, fakeClearTimeout);
const destroyFn = $scope.listeners.$destroy[0];
destroyFn();
assert.calledWith(fakeClearTimeout, 42);
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment