jquery-ui/tests/unit/selectmenu/events.js

144 lines
3.7 KiB
JavaScript
Raw Normal View History

define( [
2016-04-06 13:16:23 +00:00
"qunit",
"jquery",
"lib/helper",
"ui/widgets/selectmenu"
], function( QUnit, $, helper ) {
"use strict";
2016-04-06 13:16:23 +00:00
QUnit.module( "selectmenu: events", {
beforeEach: function() {
this.element = $( "#speed" );
},
afterEach: helper.moduleAfterEach
2015-08-24 12:50:24 +00:00
} );
2016-04-06 13:16:23 +00:00
QUnit.test( "change", function( assert ) {
var ready = assert.async();
assert.expect( 3 );
2014-04-22 16:19:12 +00:00
var button, menu, options,
optionIndex = 1;
2015-08-24 12:50:24 +00:00
this.element.selectmenu( {
change: function( event, ui ) {
2016-04-06 13:16:23 +00:00
assert.equal( ui.item.index, optionIndex, "ui.item.index contains correct option index" );
assert.equal( ui.item.element[ 0 ], options.eq( optionIndex )[ 0 ],
2014-04-22 16:19:12 +00:00
"ui.item.element contains original option element" );
2016-04-06 13:16:23 +00:00
assert.equal( ui.item.value, options.eq( optionIndex ).text(),
2014-04-22 16:19:12 +00:00
"ui.item.value property updated correctly" );
}
2015-08-24 12:50:24 +00:00
} );
button = this.element.selectmenu( "widget" );
2014-04-22 16:19:12 +00:00
menu = this.element.selectmenu( "menuWidget" );
options = this.element.find( "option" );
button.simulate( "focus" );
2015-08-24 12:50:24 +00:00
setTimeout( function() {
button.trigger( "click" );
2013-10-09 23:01:41 +00:00
menu.find( "li" ).eq( optionIndex ).simulate( "mouseover" ).trigger( "click" );
2016-04-06 13:16:23 +00:00
ready();
2015-08-24 12:50:24 +00:00
} );
} );
2016-04-06 13:16:23 +00:00
QUnit.test( "close", function( assert ) {
assert.expect( 2 );
2014-04-22 16:19:12 +00:00
var shouldFire;
2015-08-24 12:50:24 +00:00
this.element.selectmenu( {
2014-04-22 16:19:12 +00:00
close: function() {
2016-04-06 13:16:23 +00:00
assert.ok( shouldFire, "close event fired on close" );
}
2015-08-24 12:50:24 +00:00
} );
2014-04-22 16:19:12 +00:00
shouldFire = false;
2014-04-22 16:57:05 +00:00
this.element.selectmenu( "open" );
2014-04-22 16:19:12 +00:00
shouldFire = true;
this.element.selectmenu( "close" );
shouldFire = false;
this.element.selectmenu( "open" );
2014-04-22 16:19:12 +00:00
shouldFire = true;
$( "body" ).trigger( "mousedown" );
2015-08-24 12:50:24 +00:00
} );
2016-04-06 13:16:23 +00:00
QUnit.test( "focus", function( assert ) {
var ready = assert.async();
assert.expect( 9 );
2014-04-22 16:19:12 +00:00
var button, menu, links,
that = this,
optionIndex = this.element[ 0 ].selectedIndex + 1,
2014-04-22 16:19:12 +00:00
options = this.element.find( "option" );
2015-08-24 12:50:24 +00:00
this.element.selectmenu( {
2014-04-22 16:19:12 +00:00
focus: function( event, ui ) {
2016-04-06 13:16:23 +00:00
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 ],
2014-04-22 16:19:12 +00:00
"ui.item.element contains original option element" );
}
2015-08-24 12:50:24 +00:00
} );
2013-06-04 19:16:13 +00:00
button = this.element.selectmenu( "widget" );
menu = this.element.selectmenu( "menuWidget" );
2013-06-04 19:16:13 +00:00
button.simulate( "focus" );
2015-08-24 12:50:24 +00:00
setTimeout( function() {
button.simulate( "keydown", { keyCode: $.ui.keyCode.DOWN } );
button.trigger( "click" );
2013-10-09 23:01:41 +00:00
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 } );
2014-04-22 16:19:12 +00:00
// This tests for unwanted, additional focus event on close
that.element.selectmenu( "close" );
2016-04-06 13:16:23 +00:00
ready();
2015-08-24 12:50:24 +00:00
} );
} );
2016-04-06 13:16:23 +00:00
QUnit.test( "open", function( assert ) {
assert.expect( 1 );
2015-08-24 12:50:24 +00:00
this.element.selectmenu( {
2014-04-22 16:19:12 +00:00
open: function() {
2016-04-06 13:16:23 +00:00
assert.ok( true, "open event fired on open" );
}
2015-08-24 12:50:24 +00:00
} );
this.element.selectmenu( "open" );
2015-08-24 12:50:24 +00:00
} );
2016-04-06 13:16:23 +00:00
QUnit.test( "select", function( assert ) {
var ready = assert.async();
assert.expect( 3 );
2015-08-24 12:50:24 +00:00
this.element.selectmenu( {
2014-04-22 16:19:12 +00:00
select: function( event, ui ) {
2016-04-06 13:16:23 +00:00
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 ],
2014-04-22 16:19:12 +00:00
"ui.item.element contains original option element" );
}
2015-08-24 12:50:24 +00:00
} );
var button = this.element.selectmenu( "widget" ),
2014-04-22 16:19:12 +00:00
menu = this.element.selectmenu( "menuWidget" ),
options = this.element.find( "option" ),
optionIndex = 1;
button.simulate( "focus" );
2015-08-24 12:50:24 +00:00
setTimeout( function() {
button.trigger( "click" );
2013-10-09 23:01:41 +00:00
menu.find( "li" ).eq( optionIndex ).simulate( "mouseover" ).trigger( "click" );
2016-04-06 13:16:23 +00:00
ready();
2015-08-24 12:50:24 +00:00
} );
} );
2011-11-24 15:50:20 +00:00
} );