jquery-ui/tests/unit/widget/animation.js
Michał Gołębiowski-Owczarek e7a10c70ae
Tests: Ensure no timers are running at the end of each test (#1920)
This helps fix issues that make tooltip tests sometimes fail when run against
jQuery 3.2 or newer due to timing differences.

Details:
* Add the `moduleAfterEach` function ensuring no timers are running.
* Attach this function via `common.testWidget`.
* Attach this function to most test suites.
* Add a tooltip test helper cleaning up leftover timers.
* Rename legacy `setup`/`teardown` hooks to `beforeEach`/`afterEach`.

Closes gh-1920
2020-05-16 09:16:24 +02:00

274 lines
5.7 KiB
JavaScript

define( [
"qunit",
"jquery",
"lib/helper",
"ui/widget"
], function( QUnit, $, helper ) {
QUnit.module( "widget animation", ( function() {
var show = $.fn.show,
fadeIn = $.fn.fadeIn,
slideDown = $.fn.slideDown;
return {
beforeEach: function() {
$.widget( "ui.testWidget", {
_create: function() {
this.element.hide();
},
show: function( fn ) {
this._show( this.element, this.options.show, fn );
}
} );
$.effects = { effect: { testEffect: $.noop } };
},
afterEach: function() {
delete $.ui.testWidget;
delete $.effects.effect.testEffect;
$.fn.show = show;
$.fn.fadeIn = fadeIn;
$.fn.slideDown = slideDown;
return helper.moduleAfterEach.apply( this, arguments );
}
};
}() ) );
QUnit.test( "show: null", function( assert ) {
var ready = assert.async();
assert.expect( 4 );
var element = $( "#widget" ).testWidget(),
hasRun = false;
$.fn.show = function() {
assert.ok( true, "show called" );
assert.equal( arguments.length, 0, "no args passed to show" );
};
element
.delay( 50 )
.queue( function( next ) {
assert.ok( !hasRun, "queue before show" );
next();
} )
.testWidget( "show", function() {
hasRun = true;
} )
.queue( function( next ) {
assert.ok( hasRun, "queue after show" );
ready();
next();
} );
} );
QUnit.test( "show: true", function( assert ) {
var ready = assert.async();
assert.expect( 4 );
var element = $( "#widget" ).testWidget( {
show: true
} ),
hasRun = false;
$.fn.fadeIn = function( duration, easing, complete ) {
return this.queue( function( next ) {
assert.strictEqual( duration, undefined, "duration" );
assert.strictEqual( easing, undefined, "easing" );
complete();
next();
} );
};
element
.delay( 50 )
.queue( function( next ) {
assert.ok( !hasRun, "queue before show" );
next();
} )
.testWidget( "show", function() {
hasRun = true;
} )
.queue( function( next ) {
assert.ok( hasRun, "queue after show" );
ready();
next();
} );
} );
QUnit.test( "show: number", function( assert ) {
var ready = assert.async();
assert.expect( 4 );
var element = $( "#widget" ).testWidget( {
show: 123
} ),
hasRun = false;
$.fn.fadeIn = function( duration, easing, complete ) {
return this.queue( function( next ) {
assert.strictEqual( duration, 123, "duration" );
assert.strictEqual( easing, undefined, "easing" );
complete();
next();
} );
};
element
.delay( 50 )
.queue( function( next ) {
assert.ok( !hasRun, "queue before show" );
next();
} )
.testWidget( "show", function() {
hasRun = true;
} )
.queue( function( next ) {
assert.ok( hasRun, "queue after show" );
ready();
next();
} );
} );
QUnit.test( "show: core animation", function( assert ) {
var ready = assert.async();
assert.expect( 4 );
var element = $( "#widget" ).testWidget( {
show: "slideDown"
} ),
hasRun = false;
$.fn.slideDown = function( duration, easing, complete ) {
return this.queue( function( next ) {
assert.strictEqual( duration, undefined, "duration" );
assert.strictEqual( easing, undefined, "easing" );
complete();
next();
} );
};
element
.delay( 50 )
.queue( function( next ) {
assert.ok( !hasRun, "queue before show" );
next();
} )
.testWidget( "show", function() {
hasRun = true;
} )
.queue( function( next ) {
assert.ok( hasRun, "queue after show" );
ready();
next();
} );
} );
QUnit.test( "show: effect", function( assert ) {
var ready = assert.async();
assert.expect( 5 );
var element = $( "#widget" ).testWidget( {
show: "testEffect"
} ),
hasRun = false;
$.fn.show = function( options ) {
return this.queue( function( next ) {
assert.equal( options.effect, "testEffect", "effect" );
assert.ok( !( "duration" in options ), "duration" );
assert.ok( !( "easing" in options ), "easing" );
options.complete();
next();
} );
};
element
.delay( 50 )
.queue( function( next ) {
assert.ok( !hasRun, "queue before show" );
next();
} )
.testWidget( "show", function() {
hasRun = true;
} )
.queue( function( next ) {
assert.ok( hasRun, "queue after show" );
ready();
next();
} );
} );
QUnit.test( "show: object(core animation)", function( assert ) {
var ready = assert.async();
assert.expect( 4 );
var element = $( "#widget" ).testWidget( {
show: {
effect: "slideDown",
duration: 123,
easing: "testEasing"
}
} ),
hasRun = false;
$.fn.slideDown = function( duration, easing, complete ) {
return this.queue( function( next ) {
assert.equal( duration, 123, "duration" );
assert.equal( easing, "testEasing", "easing" );
complete();
next();
} );
};
element
.delay( 50 )
.queue( function( next ) {
assert.ok( !hasRun, "queue before show" );
next();
} )
.testWidget( "show", function() {
hasRun = true;
} )
.queue( function( next ) {
assert.ok( hasRun, "queue after show" );
ready();
next();
} );
} );
QUnit.test( "show: object(effect)", function( assert ) {
var ready = assert.async();
assert.expect( 3 );
var element = $( "#widget" ).testWidget( {
show: {
effect: "testEffect",
duration: 123,
easing: "testEasing"
}
} ),
hasRun = false;
$.fn.show = function( options ) {
return this.queue( function( next ) {
assert.deepEqual( options, {
effect: "testEffect",
duration: 123,
easing: "testEasing",
complete: options.complete
} );
options.complete();
next();
} );
};
element
.delay( 50 )
.queue( function( next ) {
assert.ok( !hasRun, "queue before show" );
next();
} )
.testWidget( "show", function() {
hasRun = true;
} )
.queue( function( next ) {
assert.ok( hasRun, "queue after show" );
ready();
next();
} );
} );
} );