2016-08-03 21:27:03 +02:00
|
|
|
/* eslint-env qunit */
|
2016-12-05 23:14:03 +02:00
|
|
|
import sinon from 'sinon';
|
2015-05-04 01:12:38 +02:00
|
|
|
import * as Fn from '../../../src/js/utils/fn.js';
|
|
|
|
|
2016-12-05 23:14:03 +02:00
|
|
|
QUnit.module('fn', {
|
|
|
|
beforeEach() {
|
|
|
|
this.clock = sinon.useFakeTimers();
|
|
|
|
},
|
|
|
|
afterEach() {
|
|
|
|
this.clock.restore();
|
|
|
|
}
|
|
|
|
});
|
2015-08-03 21:19:36 +02:00
|
|
|
|
2016-08-12 19:51:31 +02:00
|
|
|
QUnit.test('should add context to a function', function(assert) {
|
2016-08-03 21:27:03 +02:00
|
|
|
const newContext = { test: 'obj'};
|
|
|
|
const asdf = function() {
|
2016-08-12 19:51:31 +02:00
|
|
|
assert.ok(this === newContext);
|
2015-05-04 01:12:38 +02:00
|
|
|
};
|
2016-08-03 21:27:03 +02:00
|
|
|
const fdsa = Fn.bind(newContext, asdf);
|
2015-05-04 01:12:38 +02:00
|
|
|
|
|
|
|
fdsa();
|
|
|
|
});
|
2016-12-05 23:14:03 +02:00
|
|
|
|
|
|
|
QUnit.test('should throttle functions properly', function(assert) {
|
|
|
|
const tester = sinon.spy();
|
|
|
|
const throttled = Fn.throttle(tester, 100);
|
|
|
|
|
|
|
|
// We must wait a full wait period before the function can be called.
|
|
|
|
this.clock.tick(100);
|
|
|
|
throttled();
|
|
|
|
throttled();
|
|
|
|
this.clock.tick(50);
|
|
|
|
throttled();
|
|
|
|
|
|
|
|
assert.strictEqual(tester.callCount, 1, 'the throttled function has been called the correct number of times');
|
|
|
|
|
|
|
|
this.clock.tick(50);
|
|
|
|
throttled();
|
|
|
|
|
|
|
|
assert.strictEqual(tester.callCount, 2, 'the throttled function has been called the correct number of times');
|
|
|
|
|
|
|
|
throttled();
|
|
|
|
this.clock.tick(100);
|
|
|
|
throttled();
|
|
|
|
|
|
|
|
assert.strictEqual(tester.callCount, 3, 'the throttled function has been called the correct number of times');
|
|
|
|
});
|