1
0
mirror of https://github.com/videojs/video.js.git synced 2024-12-23 02:04:34 +02:00
video.js/test/unit/utils/fn.test.js

114 lines
3.4 KiB
JavaScript
Raw Permalink Normal View History

/* eslint-env qunit */
import sinon from 'sinon';
import * as Fn from '../../../src/js/utils/fn';
Broke up Lib and Util into smaller libraries of functions Broke out bind, guid, and element data functions from Lib Separated out more dom functions in to dom.js Broke out URL functions into url.js Removed setLocalStorage since it wasn't being used Moved browser tests out of lib Moved log functions into their own file Removed trim() since it wasn't being used Moved formatTime into its own file Moved round into its own file and renamed roundFloat() Moved capitalize into its own file and renamed as toTitleCase() Moved createTimeRange into its own file Removed Lib.arr.forEach infavor of the native forEach Removed Lib.obj.create in favor of native Object.create (ES6-sham) Removed obj.each in favor of native Object.getOwnPropertyNames().forEach() Removed obj.merge and copy. Using lodash.assign instead. Replaced Lib.obj.isPlain with lodash.isPlainObject Removed Lib.obj.isArray in favor of the native Array.isArray Also removed the lib.js tests file as all tests have been moved or removed. Removed Lib.isEmpty in favor of !Object.getOwnPropertyNames().length Switched Util.mergeOptions and deepMerge to use new mergeOptions() Moved Lib.TEST_VID to Html5.TEST_VID Removed Lib references everywhere. Woo! Attempting to fix sourcemap test errors by setting grunt-browserify version Switched to object.assign from lodash.assign Removed unused 'inherits' dependency Reorganzied test files and added '.test' to file names Combined js/core.js and js/video.js Moved events.js into the utils directory
2015-05-04 01:12:38 +02:00
QUnit.module('utils/fn', {
beforeEach() {
this.clock = sinon.useFakeTimers();
},
afterEach() {
this.clock.restore();
}
});
QUnit.test('should add context to a function', function(assert) {
assert.expect(1);
const newContext = { test: 'obj'};
const asdf = function() {
assert.ok(this === newContext);
Broke up Lib and Util into smaller libraries of functions Broke out bind, guid, and element data functions from Lib Separated out more dom functions in to dom.js Broke out URL functions into url.js Removed setLocalStorage since it wasn't being used Moved browser tests out of lib Moved log functions into their own file Removed trim() since it wasn't being used Moved formatTime into its own file Moved round into its own file and renamed roundFloat() Moved capitalize into its own file and renamed as toTitleCase() Moved createTimeRange into its own file Removed Lib.arr.forEach infavor of the native forEach Removed Lib.obj.create in favor of native Object.create (ES6-sham) Removed obj.each in favor of native Object.getOwnPropertyNames().forEach() Removed obj.merge and copy. Using lodash.assign instead. Replaced Lib.obj.isPlain with lodash.isPlainObject Removed Lib.obj.isArray in favor of the native Array.isArray Also removed the lib.js tests file as all tests have been moved or removed. Removed Lib.isEmpty in favor of !Object.getOwnPropertyNames().length Switched Util.mergeOptions and deepMerge to use new mergeOptions() Moved Lib.TEST_VID to Html5.TEST_VID Removed Lib references everywhere. Woo! Attempting to fix sourcemap test errors by setting grunt-browserify version Switched to object.assign from lodash.assign Removed unused 'inherits' dependency Reorganzied test files and added '.test' to file names Combined js/core.js and js/video.js Moved events.js into the utils directory
2015-05-04 01:12:38 +02:00
};
const fdsa = Fn.bind_(newContext, asdf);
Broke up Lib and Util into smaller libraries of functions Broke out bind, guid, and element data functions from Lib Separated out more dom functions in to dom.js Broke out URL functions into url.js Removed setLocalStorage since it wasn't being used Moved browser tests out of lib Moved log functions into their own file Removed trim() since it wasn't being used Moved formatTime into its own file Moved round into its own file and renamed roundFloat() Moved capitalize into its own file and renamed as toTitleCase() Moved createTimeRange into its own file Removed Lib.arr.forEach infavor of the native forEach Removed Lib.obj.create in favor of native Object.create (ES6-sham) Removed obj.each in favor of native Object.getOwnPropertyNames().forEach() Removed obj.merge and copy. Using lodash.assign instead. Replaced Lib.obj.isPlain with lodash.isPlainObject Removed Lib.obj.isArray in favor of the native Array.isArray Also removed the lib.js tests file as all tests have been moved or removed. Removed Lib.isEmpty in favor of !Object.getOwnPropertyNames().length Switched Util.mergeOptions and deepMerge to use new mergeOptions() Moved Lib.TEST_VID to Html5.TEST_VID Removed Lib references everywhere. Woo! Attempting to fix sourcemap test errors by setting grunt-browserify version Switched to object.assign from lodash.assign Removed unused 'inherits' dependency Reorganzied test files and added '.test' to file names Combined js/core.js and js/video.js Moved events.js into the utils directory
2015-05-04 01:12:38 +02:00
fdsa();
});
QUnit.test('should throttle functions properly', function(assert) {
assert.expect(3);
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');
});
QUnit.test('should debounce functions properly', function(assert) {
assert.expect(6);
const tester = sinon.spy();
const debounced = Fn.debounce(tester, 100);
// Called twice on each assertion to ensure that multiple calls only result
// in a call to the inner function.
debounced();
debounced();
assert.strictEqual(tester.callCount, 0, 'the debounced function was NOT called because no time has elapsed');
this.clock.tick(100);
assert.strictEqual(tester.callCount, 1, 'the debounced function was called because enough time elapsed');
this.clock.tick(100);
assert.strictEqual(tester.callCount, 1, 'the debounced function was NOT called again even though time elapsed');
debounced();
debounced();
assert.strictEqual(tester.callCount, 1, 'the debounced function was NOT called because no time has elapsed since invocation');
this.clock.tick(50);
assert.strictEqual(tester.callCount, 1, 'the debounced function was NOT called because the clock has NOT ticket forward enough');
this.clock.tick(50);
assert.strictEqual(tester.callCount, 2, 'the debounced function was called because the clock ticked forward enough');
});
QUnit.test('may immediately invoke debounced functions', function(assert) {
assert.expect(2);
const tester = sinon.spy();
const debounced = Fn.debounce(tester, 100, true);
// Called twice on each assertion to ensure that multiple calls only result
// in a call to the inner function.
debounced();
debounced();
assert.strictEqual(tester.callCount, 1, 'the debounced function was called because true was passed');
this.clock.tick(100);
assert.strictEqual(tester.callCount, 1, 'the debounced function was NOT called because it has only been invoked once');
});
QUnit.test('may cancel debounced functions', function(assert) {
assert.expect(2);
const tester = sinon.spy();
const debounced = Fn.debounce(tester, 100);
debounced();
this.clock.tick(50);
debounced.cancel();
this.clock.tick(50);
assert.strictEqual(tester.callCount, 0, 'the debounced function was NOT called because it was cancelled');
debounced();
this.clock.tick(100);
assert.strictEqual(tester.callCount, 1, 'the debounced function was called because it was NOT cancelled');
});