jquery-ui/tests/unit/selectmenu/events.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

143 lines
3.7 KiB
JavaScript

define( [
"qunit",
"jquery",
"lib/helper",
"ui/widgets/selectmenu"
], function( QUnit, $, helper ) {
QUnit.module( "selectmenu: events", {
beforeEach: function() {
this.element = $( "#speed" );
},
afterEach: helper.moduleAfterEach
} );
QUnit.test( "change", function( assert ) {
var ready = assert.async();
assert.expect( 3 );
var button, menu, options,
optionIndex = 1;
this.element.selectmenu( {
change: function( event, ui ) {
assert.equal( ui.item.index, optionIndex, "ui.item.index contains correct option index" );
assert.equal( ui.item.element[ 0 ], options.eq( optionIndex )[ 0 ],
"ui.item.element contains original option element" );
assert.equal( ui.item.value, options.eq( optionIndex ).text(),
"ui.item.value property updated correctly" );
}
} );
button = this.element.selectmenu( "widget" );
menu = this.element.selectmenu( "menuWidget" );
options = this.element.find( "option" );
button.simulate( "focus" );
setTimeout( function() {
button.trigger( "click" );
menu.find( "li" ).eq( optionIndex ).simulate( "mouseover" ).trigger( "click" );
ready();
} );
} );
QUnit.test( "close", function( assert ) {
assert.expect( 2 );
var shouldFire;
this.element.selectmenu( {
close: function() {
assert.ok( shouldFire, "close event fired on close" );
}
} );
shouldFire = false;
this.element.selectmenu( "open" );
shouldFire = true;
this.element.selectmenu( "close" );
shouldFire = false;
this.element.selectmenu( "open" );
shouldFire = true;
$( "body" ).trigger( "mousedown" );
} );
QUnit.test( "focus", function( assert ) {
var ready = assert.async();
assert.expect( 9 );
var button, menu, links,
that = this,
optionIndex = this.element[ 0 ].selectedIndex + 1,
options = this.element.find( "option" );
this.element.selectmenu( {
focus: function( event, ui ) {
assert.ok( true, "focus event fired on element #" + optionIndex + " mouseover" );
assert.equal( ui.item.index, optionIndex, "ui.item.index contains correct option index" );
assert.equal( ui.item.element[ 0 ], options.eq( optionIndex )[ 0 ],
"ui.item.element contains original option element" );
}
} );
button = this.element.selectmenu( "widget" );
menu = this.element.selectmenu( "menuWidget" );
button.simulate( "focus" );
setTimeout( function() {
button.simulate( "keydown", { keyCode: $.ui.keyCode.DOWN } );
button.trigger( "click" );
links = menu.find( "li.ui-menu-item" );
optionIndex = 0;
links.eq( optionIndex ).simulate( "mouseover", { clientX: 2, clientY: 2 } );
optionIndex += 1;
links.eq( optionIndex ).simulate( "mouseover", { clientX: 3, clientY: 3 } );
// This tests for unwanted, additional focus event on close
that.element.selectmenu( "close" );
ready();
} );
} );
QUnit.test( "open", function( assert ) {
assert.expect( 1 );
this.element.selectmenu( {
open: function() {
assert.ok( true, "open event fired on open" );
}
} );
this.element.selectmenu( "open" );
} );
QUnit.test( "select", function( assert ) {
var ready = assert.async();
assert.expect( 3 );
this.element.selectmenu( {
select: function( event, ui ) {
assert.ok( true, "select event fired on item select" );
assert.equal( ui.item.index, optionIndex, "ui.item.index contains correct option index" );
assert.equal( ui.item.element[ 0 ], options.eq( optionIndex )[ 0 ],
"ui.item.element contains original option element" );
}
} );
var button = this.element.selectmenu( "widget" ),
menu = this.element.selectmenu( "menuWidget" ),
options = this.element.find( "option" ),
optionIndex = 1;
button.simulate( "focus" );
setTimeout( function() {
button.trigger( "click" );
menu.find( "li" ).eq( optionIndex ).simulate( "mouseover" ).trigger( "click" );
ready();
} );
} );
} );