Selectmenu: Test cleanup

This commit is contained in:
Scott González 2014-04-21 20:15:36 -04:00
parent fd7e1e3040
commit d8062dc4cf

View File

@ -14,11 +14,11 @@ asyncTest( "accessibility", function() {
expect( 12 + links.length * 2 ); expect( 12 + links.length * 2 );
setTimeout(function() { setTimeout(function() {
equal( button.attr( "role" ), "combobox", "button link role" ); equal( button.attr( "role" ), "combobox", "button role" );
equal( button.attr( "aria-haspopup" ), "true", "button link aria-haspopup" ); equal( button.attr( "aria-haspopup" ), "true", "button aria-haspopup" );
equal( button.attr( "aria-expanded" ), "false", "button link aria-expanded" ); equal( button.attr( "aria-expanded" ), "false", "button aria-expanded" );
equal( button.attr( "aria-autocomplete" ), "list", "button link aria-autocomplete" ); equal( button.attr( "aria-autocomplete" ), "list", "button aria-autocomplete" );
equal( button.attr( "aria-owns" ), menu.attr("id"), "button link aria-owns" ); equal( button.attr( "aria-owns" ), menu.attr( "id" ), "button aria-owns" );
equal( equal(
button.attr( "aria-labelledby" ), button.attr( "aria-labelledby" ),
links.eq( element[ 0 ].selectedIndex ).attr( "id" ), links.eq( element[ 0 ].selectedIndex ).attr( "id" ),
@ -35,9 +35,10 @@ asyncTest( "accessibility", function() {
links.eq( element[ 0 ].selectedIndex ).attr( "id" ), links.eq( element[ 0 ].selectedIndex ).attr( "id" ),
"menu aria-activedescendant" "menu aria-activedescendant"
); );
$.each( links, function( index ){ $.each( links, function( index ) {
equal( $( this ).attr( "role" ), "option", "menu link #" + index +" role" ); var link = $( this );
equal( $( this ).attr( "tabindex" ), -1, "menu link #" + index +" tabindex" ); equal( link.attr( "role" ), "option", "menu link #" + index +" role" );
equal( link.attr( "tabindex" ), -1, "menu link #" + index +" tabindex" );
}); });
start(); start();
}); });
@ -54,18 +55,21 @@ $.each([
selector: "#files" selector: "#files"
} }
], function( i, settings ) { ], function( i, settings ) {
asyncTest( "state synchronization - after keydown on button - " + settings.type, function () { asyncTest( "state synchronization - after keydown on button - " + settings.type, function() {
expect( 4 ); expect( 4 );
var links, var links,
element = $( settings.selector ).selectmenu(), element = $( settings.selector ).selectmenu(),
button = element.selectmenu( "widget" ), button = element.selectmenu( "widget" ),
menu = element.selectmenu( "menuWidget" ), menu = element.selectmenu( "menuWidget" ),
selected = element.find( "option:selected" );
// Get the option after the currently selected option because
// we simulate pressing DOWN.
selected = element.find( "option:selected" ).next();
button.simulate( "focus" ); button.simulate( "focus" );
setTimeout(function() { setTimeout(function() {
links = menu.find("li.ui-menu-item"); links = menu.find( "li.ui-menu-item" );
button.simulate( "keydown", { keyCode: $.ui.keyCode.DOWN } ); button.simulate( "keydown", { keyCode: $.ui.keyCode.DOWN } );
equal( equal(
@ -80,25 +84,26 @@ $.each([
); );
equal( equal(
element.find( "option:selected" ).val(), element.find( "option:selected" ).val(),
selected.next( "option" ).val() , selected.val() ,
"original select state" "original select state"
); );
equal( button.text(), selected.next( "option" ).text(), "button text" ); equal( button.text(), selected.text(), "button text" );
start(); start();
}, 1 ); });
}); });
asyncTest( "state synchronization - after click on item - " + settings.type, function () { asyncTest( "state synchronization - after click on item - " + settings.type, function() {
expect( 4 ); expect( 4 );
var links, var links,
element = $( settings.selector ).selectmenu(), element = $( settings.selector ).selectmenu(),
button = element.selectmenu( "widget" ), button = element.selectmenu( "widget" ),
menu = element.selectmenu( "menuWidget" ); menu = element.selectmenu( "menuWidget" ),
selected = element.find( "option" ).last();
button.simulate( "focus" ); button.simulate( "focus" );
setTimeout(function() { setTimeout(function() {
links = menu.find("li.ui-menu-item"); links = menu.find( "li.ui-menu-item" );
button.trigger( "click" ); button.trigger( "click" );
menu.find( "li" ).last().simulate( "mouseover" ).trigger( "click" ); menu.find( "li" ).last().simulate( "mouseover" ).trigger( "click" );
@ -114,15 +119,16 @@ $.each([
); );
equal( equal(
element.find( "option:selected" ).val(), element.find( "option:selected" ).val(),
element.find( "option" ).last().val(), selected.val(),
"original select state" "original select state"
); );
equal( button.text(), element.find( "option" ).last().text(), "button text" ); equal( button.text(), selected.text(), "button text" );
start(); start();
}, 1 ); }, 1 );
}); });
asyncTest( "state synchronization - after focus item and keydown on button - " + settings.type, function () { asyncTest( "state synchronization - " +
"after focus item and keydown on button - " + settings.type, function() {
expect( 4 ); expect( 4 );
var links, var links,
@ -131,60 +137,66 @@ $.each([
menu = element.selectmenu( "menuWidget" ), menu = element.selectmenu( "menuWidget" ),
options = element.find( "option" ); options = element.find( "option" );
// init menu // Init menu
button.simulate( "focus" ); button.simulate( "focus" );
setTimeout(function() { setTimeout(function() {
links = menu.find( "li.ui-menu-item" ); links = menu.find( "li.ui-menu-item" );
// open menu and click first item
// Open menu and click first item
button.trigger( "click" ); button.trigger( "click" );
links.first().simulate( "mouseover" ).trigger( "click" ); links.first().simulate( "mouseover" ).trigger( "click" );
// open menu again and hover item
// Open menu again and hover item
button.trigger( "click" ); button.trigger( "click" );
links.eq( 3 ).simulate( "mouseover" ); links.eq( 3 ).simulate( "mouseover" );
// close and use keyboard control on button
// Close and use keyboard control on button
button.simulate( "keydown", { keyCode: $.ui.keyCode.ESCAPE } ); button.simulate( "keydown", { keyCode: $.ui.keyCode.ESCAPE } );
button.simulate( "focus" ); button.simulate( "focus" );
setTimeout(function() { setTimeout(function() {
button.simulate( "keydown", { keyCode: $.ui.keyCode.DOWN } ); button.simulate( "keydown", { keyCode: $.ui.keyCode.DOWN } );
equal( menu.attr( "aria-activedescendant" ), links.eq( 1 ).attr( "id" ), "menu aria-activedescendant" ); equal( menu.attr( "aria-activedescendant" ), links.eq( 1 ).attr( "id" ),
equal( button.attr( "aria-activedescendant" ), links.eq( 1 ).attr( "id" ), "button aria-activedescendant" ); "menu aria-activedescendant" );
equal( element.find( "option:selected" ).val(), options.eq( 1 ).val() , "original select state" ); equal( button.attr( "aria-activedescendant" ), links.eq( 1 ).attr( "id" ),
"button aria-activedescendant" );
equal( element.find( "option:selected" ).val(), options.eq( 1 ).val() ,
"original select state" );
equal( button.text(), options.eq( 1 ).text(), "button text" ); equal( button.text(), options.eq( 1 ).text(), "button text" );
start(); start();
}, 1 ); });
}, 1 ); });
}); });
asyncTest( "item looping - " + settings.type, function () { asyncTest( "item looping - " + settings.type, function() {
expect( 2 ); expect( 4 );
var links, var links,
element = $( settings.selector ).selectmenu(), element = $( settings.selector ).selectmenu(),
button = element.selectmenu( "widget" ), button = element.selectmenu( "widget" ),
menu = element.selectmenu( "menuWidget" ); menu = element.selectmenu( "menuWidget" );
// init menu
button.simulate( "focus" ); button.simulate( "focus" );
setTimeout(function() { setTimeout(function() {
links = menu.find( "li.ui-menu-item" ); links = menu.find( "li.ui-menu-item" );
button.trigger( "click" ); button.trigger( "click" );
links.first().simulate( "mouseover" ).trigger( "click" ); links.first().simulate( "mouseover" ).trigger( "click" );
equal( element[ 0 ].selectedIndex, 0, "First item is selected" );
button.simulate( "keydown", { keyCode: $.ui.keyCode.UP } ); button.simulate( "keydown", { keyCode: $.ui.keyCode.UP } );
equal( element[ 0 ].selectedIndex, 0, "No looping beyond first item" ); equal( element[ 0 ].selectedIndex, 0, "No looping beyond first item" );
button.trigger( "click" ); button.trigger( "click" );
links.last().simulate( "mouseover" ).trigger( "click" ); links.last().simulate( "mouseover" ).trigger( "click" );
equal( element[ 0 ].selectedIndex, links.length - 1, "Last item is selected" );
button.simulate( "keydown", { keyCode: $.ui.keyCode.DOWN } ); button.simulate( "keydown", { keyCode: $.ui.keyCode.DOWN } );
equal( element[ 0 ].selectedIndex + 1, links.length, "No looping behind last item" ); equal( element[ 0 ].selectedIndex, links.length - 1, "No looping behind last item" );
start(); start();
}, 1 ); });
}); });
asyncTest( "item focus and active state - " + settings.type, function () { asyncTest( "item focus and active state - " + settings.type, function() {
expect( 4 ); expect( 4 );
var items, focusedItem, var items, focusedItem,
@ -192,9 +204,7 @@ $.each([
button = element.selectmenu( "widget" ), button = element.selectmenu( "widget" ),
menu = element.selectmenu( "menuWidget" ); menu = element.selectmenu( "menuWidget" );
// Initialize menu
button.simulate( "focus" ); button.simulate( "focus" );
setTimeout(function() { setTimeout(function() {
items = menu.find( "li.ui-menu-item" ); items = menu.find( "li.ui-menu-item" );
@ -216,47 +226,48 @@ $.each([
setTimeout(function() { setTimeout(function() {
checkItemClasses(); checkItemClasses();
start(); start();
}, 350 ); });
}, 350 ); });
}, 1 ); });
function checkItemClasses() { function checkItemClasses() {
focusedItem = menu.find( "li.ui-state-focus" ); focusedItem = menu.find( "li.ui-state-focus" );
equal( focusedItem.length, 1, "only one item has ui-state-focus class" ); equal( focusedItem.length, 1, "only one item has ui-state-focus class" );
equal( focusedItem.attr( "id" ), items.eq( element[ 0 ].selectedIndex ).attr( "id" ), "selected item has ui-state-focus class" ); equal( focusedItem.attr( "id" ), items.eq( element[ 0 ].selectedIndex ).attr( "id" ),
"selected item has ui-state-focus class" );
} }
}); });
asyncTest( "empty option - " + settings.type, function () { asyncTest( "empty option - " + settings.type, function() {
expect( 7 ); expect( 7 );
var element = $( settings.selector ), var button, menu, links, link,
button, menu, links, link; element = $( settings.selector );
element.find( "option" ).first().text( "" ); element.find( "option" ).first().text( "" );
element.selectmenu(); element.selectmenu();
button = element.selectmenu( "widget" ); button = element.selectmenu( "widget" );
menu = element.selectmenu( "menuWidget" ); menu = element.selectmenu( "menuWidget" );
// init menu
button.simulate( "focus" ); button.simulate( "focus" );
setTimeout(function() { setTimeout(function() {
links = menu.find( "li:not(.ui-selectmenu-optgroup)" ); links = menu.find( "li:not(.ui-selectmenu-optgroup)" );
link = links.first(); link = links.first();
button.trigger( "click" ); button.trigger( "click" );
equal( links.length, element.find( "option" ).length, "correct amount of list elements" ); equal( links.length, element.find( "option" ).length,
"correct amount of list elements" );
ok( link.outerHeight() > 10, "empty item seems to have reasonable height" ); ok( link.outerHeight() > 10, "empty item seems to have reasonable height" );
ok( link.attr( "id" ), "empty item has id attribute" ); ok( link.attr( "id" ), "empty item has id attribute" );
ok( link.hasClass( "ui-menu-item" ), "empty item has ui-menu-item class" ); ok( link.hasClass( "ui-menu-item" ), "empty item has ui-menu-item class" );
ok( !link.hasClass( "ui-menu-divider" ), "empty item has not ui-menu-divider class" ); ok( !link.hasClass( "ui-menu-divider" ),
"empty item does not have ui-menu-divider class" );
equal( link.attr( "tabindex" ), -1, "empty item has tabindex" ); equal( link.attr( "tabindex" ), -1, "empty item has tabindex" );
equal( link.attr( "role" ), "option", "empty item has role option" ); equal( link.attr( "role" ), "option", "empty item has role option" );
start(); start();
}, 1 ); });
}); });
}); });