2015-05-18 21:11:21 +00:00
|
|
|
( function() {
|
|
|
|
|
|
|
|
// Can't test what ain't there
|
|
|
|
if ( !jQuery.fx ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var oldRaf = window.requestAnimationFrame;
|
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
QUnit.module( "tween", {
|
2019-02-18 18:02:38 +00:00
|
|
|
beforeEach: function() {
|
2019-04-04 14:53:38 +00:00
|
|
|
this.sandbox = sinon.createSandbox();
|
2015-05-18 21:11:21 +00:00
|
|
|
this.clock = this.sandbox.useFakeTimers( 505877050 );
|
|
|
|
this._oldInterval = jQuery.fx.interval;
|
2019-04-04 14:53:38 +00:00
|
|
|
window.requestAnimationFrame = null;
|
2015-05-18 21:11:21 +00:00
|
|
|
jQuery.fx.step = {};
|
|
|
|
jQuery.fx.interval = 10;
|
|
|
|
},
|
2019-02-18 18:02:38 +00:00
|
|
|
afterEach: function() {
|
2015-05-18 21:11:21 +00:00
|
|
|
this.sandbox.restore();
|
|
|
|
jQuery.fx.stop();
|
|
|
|
jQuery.fx.interval = this._oldInterval;
|
|
|
|
window.requestAnimationFrame = oldRaf;
|
|
|
|
return moduleTeardown.apply( this, arguments );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
QUnit.test( "jQuery.Tween - Default propHooks on plain objects", function( assert ) {
|
|
|
|
assert.expect( 8 );
|
2015-05-18 21:11:21 +00:00
|
|
|
var propHooks, defaultHook, testObject, fakeTween, stepSpy;
|
|
|
|
|
|
|
|
propHooks = jQuery.Tween.propHooks;
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( typeof propHooks, "object", "jQuery.Tween.propHooks exists" );
|
2015-05-18 21:11:21 +00:00
|
|
|
|
|
|
|
defaultHook = propHooks._default;
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( defaultHook, "_default propHook exists" );
|
2015-05-18 21:11:21 +00:00
|
|
|
|
|
|
|
testObject = { test: 0 };
|
|
|
|
fakeTween = { elem: testObject, prop: "test", now: 10, unit: "px" };
|
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( defaultHook.get( fakeTween ), 0, "Can get property of object" );
|
2015-05-18 21:11:21 +00:00
|
|
|
|
|
|
|
fakeTween.prop = "testMissing";
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( defaultHook.get( fakeTween ), undefined, "Can get missing property on object" );
|
2015-05-18 21:11:21 +00:00
|
|
|
|
|
|
|
defaultHook.set( fakeTween );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( testObject.testMissing, 10, "Sets missing value properly on plain object" );
|
2015-05-18 21:11:21 +00:00
|
|
|
|
|
|
|
fakeTween.prop = "opacity";
|
|
|
|
defaultHook.set( fakeTween );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( testObject.opacity, 10, "Correctly set opacity on plain object" );
|
2015-05-18 21:11:21 +00:00
|
|
|
|
|
|
|
fakeTween.prop = "test";
|
|
|
|
stepSpy = jQuery.fx.step.test = this.sandbox.spy();
|
|
|
|
|
|
|
|
defaultHook.set( fakeTween );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( stepSpy.calledWith( fakeTween ), "Step function called with Tween" );
|
|
|
|
assert.equal( testObject.test, 0, "Because step didn't set, value is unchanged" );
|
2015-05-18 21:11:21 +00:00
|
|
|
} );
|
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
QUnit.test( "jQuery.Tween - Default propHooks on elements", function( assert ) {
|
|
|
|
assert.expect( 19 );
|
2015-05-18 21:11:21 +00:00
|
|
|
var propHooks, defaultHook, testElement, fakeTween, cssStub, styleStub, stepSpy;
|
|
|
|
|
|
|
|
propHooks = jQuery.Tween.propHooks;
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( typeof propHooks, "object", "jQuery.Tween.propHooks exists" );
|
2015-05-18 21:11:21 +00:00
|
|
|
|
|
|
|
defaultHook = propHooks._default;
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( defaultHook, "_default propHook exists" );
|
2015-05-18 21:11:21 +00:00
|
|
|
|
|
|
|
testElement = jQuery( "<div>" )[ 0 ];
|
|
|
|
fakeTween = { elem: testElement, prop: "height", now: 10, unit: "px" };
|
|
|
|
|
|
|
|
cssStub = this.sandbox.stub( jQuery, "css" ).returns( 10 );
|
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( defaultHook.get( fakeTween ), 10, "Gets expected style value" );
|
|
|
|
assert.ok( cssStub.calledWith( testElement, "height", "" ), "Calls jQuery.css correctly" );
|
2015-05-18 21:11:21 +00:00
|
|
|
|
|
|
|
fakeTween.prop = "testOpti";
|
|
|
|
testElement.testOpti = 15;
|
2019-02-18 18:02:38 +00:00
|
|
|
cssStub.resetHistory();
|
2015-05-18 21:11:21 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( defaultHook.get( fakeTween ), 15, "Gets expected value not defined on style" );
|
|
|
|
assert.equal( cssStub.callCount, 0, "Did not call jQuery.css" );
|
2015-05-18 21:11:21 +00:00
|
|
|
|
|
|
|
fakeTween.prop = "testMissing";
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( defaultHook.get( fakeTween ), 10, "Can get missing property on element" );
|
|
|
|
assert.ok( cssStub.calledWith( testElement, "testMissing", "" ), "...using jQuery.css" );
|
2015-05-18 21:11:21 +00:00
|
|
|
|
|
|
|
cssStub.returns( "" );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( defaultHook.get( fakeTween ), 0, "Uses 0 for empty string" );
|
2015-05-18 21:11:21 +00:00
|
|
|
|
|
|
|
cssStub.returns( "auto" );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( defaultHook.get( fakeTween ), 0, "Uses 0 for 'auto'" );
|
2015-05-18 21:11:21 +00:00
|
|
|
|
|
|
|
cssStub.returns( null );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( defaultHook.get( fakeTween ), 0, "Uses 0 for null" );
|
2015-05-18 21:11:21 +00:00
|
|
|
|
|
|
|
cssStub.returns( undefined );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( defaultHook.get( fakeTween ), 0, "Uses 0 for undefined" );
|
2015-05-18 21:11:21 +00:00
|
|
|
|
2019-02-18 18:02:38 +00:00
|
|
|
cssStub.resetHistory();
|
2015-05-18 21:11:21 +00:00
|
|
|
|
|
|
|
// Setters
|
|
|
|
styleStub = this.sandbox.stub( jQuery, "style" );
|
|
|
|
fakeTween.prop = "height";
|
|
|
|
|
|
|
|
defaultHook.set( fakeTween );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( styleStub.calledWith( testElement, "height", "10px" ),
|
2015-05-18 21:11:21 +00:00
|
|
|
"Calls jQuery.style with elem, prop, now+unit" );
|
|
|
|
|
2019-02-18 18:02:38 +00:00
|
|
|
styleStub.resetHistory();
|
2015-05-18 21:11:21 +00:00
|
|
|
fakeTween.prop = "testMissing";
|
|
|
|
|
|
|
|
defaultHook.set( fakeTween );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( styleStub.callCount, 0, "Did not call jQuery.style for non css property" );
|
|
|
|
assert.equal( testElement.testMissing, 10, "Instead, set value on element directly" );
|
2015-05-18 21:11:21 +00:00
|
|
|
|
|
|
|
jQuery.cssHooks.testMissing = jQuery.noop;
|
|
|
|
fakeTween.now = 11;
|
|
|
|
|
|
|
|
defaultHook.set( fakeTween );
|
|
|
|
delete jQuery.cssHooks.testMissing;
|
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( styleStub.calledWith( testElement, "testMissing", "11px" ),
|
2015-05-18 21:11:21 +00:00
|
|
|
"Presence of cssHooks causes jQuery.style with elem, prop, now+unit" );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( testElement.testMissing, 10, "And value was unchanged" );
|
2015-05-18 21:11:21 +00:00
|
|
|
|
|
|
|
stepSpy = jQuery.fx.step.test = this.sandbox.spy();
|
2019-02-18 18:02:38 +00:00
|
|
|
styleStub.resetHistory();
|
2015-05-18 21:11:21 +00:00
|
|
|
|
|
|
|
fakeTween.prop = "test";
|
|
|
|
defaultHook.set( fakeTween );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( stepSpy.calledWith( fakeTween ), "Step function called with Tween" );
|
|
|
|
assert.equal( styleStub.callCount, 0, "Did not call jQuery.style" );
|
2015-05-18 21:11:21 +00:00
|
|
|
} );
|
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
QUnit.test( "jQuery.Tween - Plain Object", function( assert ) {
|
|
|
|
assert.expect( 13 );
|
2015-05-18 21:11:21 +00:00
|
|
|
var testObject = { test: 100 },
|
|
|
|
testOptions = { duration: 100 },
|
|
|
|
tween, easingSpy;
|
|
|
|
|
|
|
|
tween = jQuery.Tween( testObject, testOptions, "test", 0, "linear" );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( tween.elem, testObject, "Sets .element" );
|
|
|
|
assert.equal( tween.options, testOptions, "sets .options" );
|
|
|
|
assert.equal( tween.prop, "test", "sets .prop" );
|
|
|
|
assert.equal( tween.end, 0, "sets .end" );
|
2015-05-18 21:11:21 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( tween.easing, "linear", "sets .easing when provided" );
|
2015-05-18 21:11:21 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( tween.start, 100, "Reads .start value during construction" );
|
|
|
|
assert.equal( tween.now, 100, "Reads .now value during construction" );
|
2015-05-18 21:11:21 +00:00
|
|
|
|
|
|
|
easingSpy = this.sandbox.spy( jQuery.easing, "linear" );
|
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( tween.run( 0.1 ), tween, ".run() returns this" );
|
2015-05-18 21:11:21 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( tween.now, 90, "Calculated tween" );
|
2015-05-18 21:11:21 +00:00
|
|
|
|
2016-04-27 20:21:56 +00:00
|
|
|
assert.ok( easingSpy.calledWith( 0.1, 0.1 * testOptions.duration, 0, 1, testOptions.duration ),
|
|
|
|
"...using jQuery.easing.linear with back-compat arguments" );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( testObject.test, 90, "Set value" );
|
2015-05-18 21:11:21 +00:00
|
|
|
|
|
|
|
tween.run( 1 );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( testObject.test, 0, "Checking another value" );
|
2015-05-18 21:11:21 +00:00
|
|
|
|
|
|
|
tween.run( 0 );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( testObject.test, 100, "Can even go back in time" );
|
2015-05-18 21:11:21 +00:00
|
|
|
} );
|
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
QUnit.test( "jQuery.Tween - Element", function( assert ) {
|
|
|
|
assert.expect( 15 );
|
2015-05-18 21:11:21 +00:00
|
|
|
var testElement = jQuery( "<div>" ).css( "height", 100 )[ 0 ],
|
|
|
|
testOptions = { duration: 100 },
|
|
|
|
tween, easingSpy, eased;
|
|
|
|
|
|
|
|
tween = jQuery.Tween( testElement, testOptions, "height", 0 );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( tween.elem, testElement, "Sets .element" );
|
|
|
|
assert.equal( tween.options, testOptions, "sets .options" );
|
|
|
|
assert.equal( tween.prop, "height", "sets .prop" );
|
|
|
|
assert.equal( tween.end, 0, "sets .end" );
|
2015-05-18 21:11:21 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal(
|
|
|
|
tween.easing,
|
|
|
|
jQuery.easing._default,
|
|
|
|
"sets .easing to default when not provided"
|
|
|
|
);
|
|
|
|
assert.equal( tween.unit, "px", "sets .unit to px when not provided" );
|
2015-05-18 21:11:21 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( tween.start, 100, "Reads .start value during construction" );
|
|
|
|
assert.equal( tween.now, 100, "Reads .now value during construction" );
|
2015-05-18 21:11:21 +00:00
|
|
|
|
|
|
|
easingSpy = this.sandbox.spy( jQuery.easing, "swing" );
|
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( tween.run( 0.1 ), tween, ".run() returns this" );
|
|
|
|
assert.equal( tween.pos, jQuery.easing.swing( 0.1 ), "set .pos" );
|
2015-05-18 21:11:21 +00:00
|
|
|
eased = 100 - ( jQuery.easing.swing( 0.1 ) * 100 );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( tween.now, eased, "Calculated tween" );
|
2015-05-18 21:11:21 +00:00
|
|
|
|
2016-04-27 20:21:56 +00:00
|
|
|
assert.ok(
|
|
|
|
easingSpy.calledWith( 0.1, 0.1 * testOptions.duration, 0, 1, testOptions.duration ),
|
|
|
|
"...using jQuery.easing.linear with back-compat arguments"
|
|
|
|
);
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal(
|
|
|
|
parseFloat( testElement.style.height ).toFixed( 2 ),
|
|
|
|
eased.toFixed( 2 ), "Set value"
|
|
|
|
);
|
2015-05-18 21:11:21 +00:00
|
|
|
|
|
|
|
tween.run( 1 );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( testElement.style.height, "0px", "Checking another value" );
|
2015-05-18 21:11:21 +00:00
|
|
|
|
|
|
|
tween.run( 0 );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( testElement.style.height, "100px", "Can even go back in time" );
|
2015-05-18 21:11:21 +00:00
|
|
|
} );
|
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
QUnit.test( "jQuery.Tween - No duration", function( assert ) {
|
|
|
|
assert.expect( 3 );
|
2015-05-18 21:11:21 +00:00
|
|
|
|
|
|
|
var testObject = { test: 100 },
|
|
|
|
testOptions = { duration: 0 },
|
|
|
|
tween, easingSpy;
|
|
|
|
|
|
|
|
tween = jQuery.Tween( testObject, testOptions, "test", 0 );
|
|
|
|
easingSpy = this.sandbox.spy( jQuery.easing, "swing" );
|
|
|
|
tween.run( 0.5 );
|
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( tween.pos, 0.5, "set .pos correctly" );
|
|
|
|
assert.equal( testObject.test, 50, "set value on object correctly" );
|
|
|
|
assert.equal( easingSpy.callCount, 0, "didn't ease the value" );
|
2015-05-18 21:11:21 +00:00
|
|
|
} );
|
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
QUnit.test( "jQuery.Tween - step function option", function( assert ) {
|
|
|
|
assert.expect( 4 );
|
2015-05-18 21:11:21 +00:00
|
|
|
|
|
|
|
var testObject = { test: 100 },
|
|
|
|
testOptions = { duration: 100, step: this.sandbox.spy() },
|
|
|
|
tween, propHookSpy;
|
|
|
|
|
|
|
|
propHookSpy = this.sandbox.spy( jQuery.Tween.propHooks._default, "set" );
|
|
|
|
|
|
|
|
tween = jQuery.Tween( testObject, testOptions, "test", 0, "linear" );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( testOptions.step.callCount, 0, "didn't call step on create" );
|
2015-05-18 21:11:21 +00:00
|
|
|
|
|
|
|
tween.run( 0.5 );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok(
|
|
|
|
testOptions.step.calledOn( testObject ),
|
|
|
|
"Called step function in context of animated object"
|
|
|
|
);
|
|
|
|
assert.ok(
|
|
|
|
testOptions.step.calledWith( 50, tween ),
|
|
|
|
"Called step function with correct parameters"
|
|
|
|
);
|
|
|
|
assert.ok(
|
|
|
|
testOptions.step.calledBefore( propHookSpy ),
|
|
|
|
"Called step function before calling propHook.set"
|
|
|
|
);
|
2015-05-18 21:11:21 +00:00
|
|
|
|
|
|
|
} );
|
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
QUnit.test( "jQuery.Tween - custom propHooks", function( assert ) {
|
|
|
|
assert.expect( 3 );
|
2015-05-18 21:11:21 +00:00
|
|
|
|
|
|
|
var testObject = {},
|
|
|
|
testOptions = { duration: 100, step: this.sandbox.spy() },
|
|
|
|
propHook = {
|
|
|
|
get: sinon.stub().returns( 100 ),
|
|
|
|
set: sinon.stub()
|
|
|
|
},
|
|
|
|
tween;
|
|
|
|
|
|
|
|
jQuery.Tween.propHooks.testHooked = propHook;
|
|
|
|
|
|
|
|
tween = jQuery.Tween( testObject, testOptions, "testHooked", 0, "linear" );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( propHook.get.calledWith( tween ), "called propHook.get on create" );
|
|
|
|
assert.equal( tween.now, 100, "Used return value from propHook.get" );
|
2015-05-18 21:11:21 +00:00
|
|
|
|
|
|
|
tween.run( 0.5 );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok(
|
|
|
|
propHook.set.calledWith( tween ),
|
|
|
|
"Called propHook.set function with correct parameters"
|
|
|
|
);
|
2015-05-18 21:11:21 +00:00
|
|
|
|
|
|
|
delete jQuery.Tween.propHooks.testHooked;
|
|
|
|
} );
|
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
QUnit.test( "jQuery.Tween - custom propHooks - advanced values", function( assert ) {
|
|
|
|
assert.expect( 5 );
|
2015-05-18 21:11:21 +00:00
|
|
|
|
|
|
|
var testObject = {},
|
|
|
|
testOptions = { duration: 100, step: this.sandbox.spy() },
|
|
|
|
propHook = {
|
|
|
|
get: sinon.stub().returns( [ 0, 0 ] ),
|
|
|
|
set: sinon.spy()
|
|
|
|
},
|
|
|
|
tween;
|
|
|
|
|
|
|
|
jQuery.Tween.propHooks.testHooked = propHook;
|
|
|
|
|
|
|
|
tween = jQuery.Tween( testObject, testOptions, "testHooked", [ 1, 1 ], "linear" );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( propHook.get.calledWith( tween ), "called propHook.get on create" );
|
|
|
|
assert.deepEqual( tween.start, [ 0, 0 ], "Used return value from get" );
|
2015-05-18 21:11:21 +00:00
|
|
|
|
|
|
|
tween.run( 0.5 );
|
|
|
|
|
|
|
|
// Some day this NaN assumption might change - perhaps add a "calc" helper to the hooks?
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( isNaN( tween.now ), "Used return value from propHook.get" );
|
|
|
|
assert.equal( tween.pos, 0.5, "But the eased percent is still available" );
|
|
|
|
assert.ok(
|
|
|
|
propHook.set.calledWith( tween ),
|
|
|
|
"Called propHook.set function with correct parameters"
|
|
|
|
);
|
2015-05-18 21:11:21 +00:00
|
|
|
|
|
|
|
delete jQuery.Tween.propHooks.testHooked;
|
|
|
|
} );
|
|
|
|
|
|
|
|
} )();
|