Нет описания

animation.js 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. ( function() {
  2. // Can't test what ain't there
  3. if ( !jQuery.fx ) {
  4. return;
  5. }
  6. var oldRaf = window.requestAnimationFrame,
  7. defaultPrefilter = jQuery.Animation.prefilters[ 0 ],
  8. defaultTweener = jQuery.Animation.tweeners[ "*" ][ 0 ],
  9. startTime = 505877050;
  10. // This module tests jQuery.Animation and the corresponding 1.8+ effects APIs
  11. QUnit.module( "animation", {
  12. beforeEach: function() {
  13. window.requestAnimationFrame = null;
  14. this.sandbox = sinon.sandbox.create();
  15. this.clock = this.sandbox.useFakeTimers( startTime );
  16. this._oldInterval = jQuery.fx.interval;
  17. jQuery.fx.step = {};
  18. jQuery.fx.interval = 10;
  19. jQuery.Animation.prefilters = [ defaultPrefilter ];
  20. jQuery.Animation.tweeners = { "*": [ defaultTweener ] };
  21. },
  22. afterEach: function() {
  23. this.sandbox.restore();
  24. jQuery.fx.stop();
  25. jQuery.fx.interval = this._oldInterval;
  26. window.requestAnimationFrame = oldRaf;
  27. return moduleTeardown.apply( this, arguments );
  28. }
  29. } );
  30. QUnit.test( "Animation( subject, props, opts ) - shape", function( assert ) {
  31. assert.expect( 20 );
  32. var subject = { test: 0 },
  33. props = { test: 1 },
  34. opts = { queue: "fx", duration: 100 },
  35. animation = jQuery.Animation( subject, props, opts );
  36. assert.equal(
  37. animation.elem,
  38. subject,
  39. ".elem is set to the exact object passed"
  40. );
  41. assert.equal(
  42. animation.originalOptions,
  43. opts,
  44. ".originalOptions is set to options passed"
  45. );
  46. assert.equal(
  47. animation.originalProperties,
  48. props,
  49. ".originalProperties is set to props passed"
  50. );
  51. assert.notEqual( animation.props, props, ".props is not the original however" );
  52. assert.deepEqual( animation.props, props, ".props is a copy of the original" );
  53. assert.deepEqual( animation.opts, {
  54. duration: 100,
  55. queue: "fx",
  56. specialEasing: { test: undefined },
  57. easing: jQuery.easing._default
  58. }, ".options is filled with default easing and specialEasing" );
  59. assert.equal( animation.startTime, startTime, "startTime was set" );
  60. assert.equal( animation.duration, 100, ".duration is set" );
  61. assert.equal( animation.tweens.length, 1, ".tweens has one Tween" );
  62. assert.equal( typeof animation.tweens[ 0 ].run, "function", "which has a .run function" );
  63. assert.equal( typeof animation.createTween, "function", ".createTween is a function" );
  64. assert.equal( typeof animation.stop, "function", ".stop is a function" );
  65. assert.equal( typeof animation.done, "function", ".done is a function" );
  66. assert.equal( typeof animation.fail, "function", ".fail is a function" );
  67. assert.equal( typeof animation.always, "function", ".always is a function" );
  68. assert.equal( typeof animation.progress, "function", ".progress is a function" );
  69. assert.equal( jQuery.timers.length, 1, "Added a timers function" );
  70. assert.equal( jQuery.timers[ 0 ].elem, subject, "...with .elem as the subject" );
  71. assert.equal( jQuery.timers[ 0 ].anim, animation, "...with .anim as the animation" );
  72. assert.equal( jQuery.timers[ 0 ].queue, opts.queue, "...with .queue" );
  73. // Cleanup after ourselves by ticking to the end
  74. this.clock.tick( 100 );
  75. } );
  76. QUnit.test( "Animation.prefilter( fn ) - calls prefilter after defaultPrefilter",
  77. function( assert ) {
  78. assert.expect( 1 );
  79. var prefilter = this.sandbox.stub(),
  80. defaultSpy = this.sandbox.spy( jQuery.Animation.prefilters, 0 );
  81. jQuery.Animation.prefilter( prefilter );
  82. jQuery.Animation( {}, {}, {} );
  83. assert.ok( prefilter.calledAfter( defaultSpy ), "our prefilter called after" );
  84. }
  85. );
  86. QUnit.test( "Animation.prefilter( fn, true ) - calls prefilter before defaultPrefilter",
  87. function( assert ) {
  88. assert.expect( 1 );
  89. var prefilter = this.sandbox.stub(),
  90. defaultSpy = this.sandbox.spy( jQuery.Animation.prefilters, 0 );
  91. jQuery.Animation.prefilter( prefilter, true );
  92. jQuery.Animation( {}, {}, {} );
  93. assert.ok( prefilter.calledBefore( defaultSpy ), "our prefilter called before" );
  94. }
  95. );
  96. QUnit.test( "Animation.prefilter - prefilter return hooks", function( assert ) {
  97. assert.expect( 34 );
  98. var animation, realAnimation, element,
  99. sandbox = this.sandbox,
  100. ourAnimation = { stop: this.sandbox.spy() },
  101. target = { height: 50 },
  102. props = { height: 100 },
  103. opts = { duration: 100 },
  104. prefilter = this.sandbox.spy( function() {
  105. realAnimation = this;
  106. sandbox.spy( realAnimation, "createTween" );
  107. assert.deepEqual( realAnimation.originalProperties, props, "originalProperties" );
  108. assert.equal( arguments[ 0 ], this.elem, "first param elem" );
  109. assert.equal( arguments[ 1 ], this.props, "second param props" );
  110. assert.equal( arguments[ 2 ], this.opts, "third param opts" );
  111. return ourAnimation;
  112. } ),
  113. defaultSpy = sandbox.spy( jQuery.Animation.prefilters, 0 ),
  114. queueSpy = sandbox.spy( function( next ) {
  115. next();
  116. } ),
  117. TweenSpy = sandbox.spy( jQuery, "Tween" );
  118. jQuery.Animation.prefilter( prefilter, true );
  119. sandbox.stub( jQuery.fx, "timer" );
  120. animation = jQuery.Animation( target, props, opts );
  121. assert.equal( prefilter.callCount, 1, "Called prefilter" );
  122. assert.equal(
  123. defaultSpy.callCount,
  124. 0,
  125. "Returning something from a prefilter caused remaining prefilters to not run"
  126. );
  127. assert.equal( jQuery.fx.timer.callCount, 0, "Returning something never queues a timer" );
  128. assert.equal(
  129. animation,
  130. ourAnimation,
  131. "Returning something returned it from jQuery.Animation"
  132. );
  133. assert.equal(
  134. realAnimation.createTween.callCount,
  135. 0,
  136. "Returning something never creates tweens"
  137. );
  138. assert.equal( TweenSpy.callCount, 0, "Returning something never creates tweens" );
  139. // Test overridden usage on queues:
  140. prefilter.reset();
  141. element = jQuery( "<div>" )
  142. .css( "height", 50 )
  143. .animate( props, 100 )
  144. .queue( queueSpy )
  145. .animate( props, 100 )
  146. .queue( queueSpy )
  147. .animate( props, 100 )
  148. .queue( queueSpy );
  149. assert.equal( prefilter.callCount, 1, "Called prefilter" );
  150. assert.equal( queueSpy.callCount, 0, "Next function in queue not called" );
  151. realAnimation.opts.complete.call( realAnimation.elem );
  152. assert.equal( queueSpy.callCount, 1, "Next function in queue called after complete" );
  153. assert.equal( prefilter.callCount, 2, "Called prefilter again - animation #2" );
  154. assert.equal( ourAnimation.stop.callCount, 0, ".stop() on our animation hasn't been called" );
  155. element.stop();
  156. assert.equal( ourAnimation.stop.callCount, 1, ".stop() called ourAnimation.stop()" );
  157. assert.ok(
  158. !ourAnimation.stop.args[ 0 ][ 0 ],
  159. ".stop( falsy ) (undefined or false are both valid)"
  160. );
  161. assert.equal( queueSpy.callCount, 2, "Next queue function called" );
  162. assert.ok( queueSpy.calledAfter( ourAnimation.stop ), "After our animation was told to stop" );
  163. // ourAnimation.stop.reset();
  164. assert.equal( prefilter.callCount, 3, "Got the next animation" );
  165. ourAnimation.stop.reset();
  166. // do not clear queue, gotoEnd
  167. element.stop( false, true );
  168. assert.ok( ourAnimation.stop.calledWith( true ), ".stop(true) calls .stop(true)" );
  169. assert.ok( queueSpy.calledAfter( ourAnimation.stop ),
  170. "and the next queue function ran after we were told" );
  171. } );
  172. QUnit.test( "Animation.tweener( fn ) - unshifts a * tweener", function( assert ) {
  173. assert.expect( 2 );
  174. var starTweeners = jQuery.Animation.tweeners[ "*" ];
  175. jQuery.Animation.tweener( jQuery.noop );
  176. assert.equal( starTweeners.length, 2 );
  177. assert.deepEqual( starTweeners, [ jQuery.noop, defaultTweener ] );
  178. } );
  179. QUnit.test( "Animation.tweener( 'prop', fn ) - unshifts a 'prop' tweener", function( assert ) {
  180. assert.expect( 4 );
  181. var tweeners = jQuery.Animation.tweeners,
  182. fn = function() {};
  183. jQuery.Animation.tweener( "prop", jQuery.noop );
  184. assert.equal( tweeners.prop.length, 1 );
  185. assert.deepEqual( tweeners.prop, [ jQuery.noop ] );
  186. jQuery.Animation.tweener( "prop", fn );
  187. assert.equal( tweeners.prop.length, 2 );
  188. assert.deepEqual( tweeners.prop, [ fn, jQuery.noop ] );
  189. } );
  190. QUnit.test(
  191. "Animation.tweener( 'list of props', fn ) - unshifts a tweener to each prop",
  192. function( assert ) {
  193. assert.expect( 2 );
  194. var tweeners = jQuery.Animation.tweeners,
  195. fn = function() {};
  196. jQuery.Animation.tweener( "list of props", jQuery.noop );
  197. assert.deepEqual( tweeners, {
  198. list: [ jQuery.noop ],
  199. of: [ jQuery.noop ],
  200. props: [ jQuery.noop ],
  201. "*": [ defaultTweener ]
  202. } );
  203. // Test with extra whitespaces
  204. jQuery.Animation.tweener( " list\t of \tprops\n*", fn );
  205. assert.deepEqual( tweeners, {
  206. list: [ fn, jQuery.noop ],
  207. of: [ fn, jQuery.noop ],
  208. props: [ fn, jQuery.noop ],
  209. "*": [ fn, defaultTweener ]
  210. } );
  211. }
  212. );
  213. } )();