jquery-ui/tests/unit/autocomplete/autocomplete_options.js

193 lines
5.0 KiB
JavaScript
Raw Normal View History

2011-06-13 18:09:44 +00:00
(function( $ ) {
2010-01-20 14:00:14 +00:00
2011-06-13 18:09:44 +00:00
module( "autocomplete: options" );
2010-01-20 14:00:14 +00:00
2011-06-13 18:09:44 +00:00
var data = [ "c++", "java", "php", "coldfusion", "javascript", "asp", "ruby", "python", "c", "scala", "groovy", "haskell", "perl" ];
2010-01-20 14:00:14 +00:00
test( "appendTo", function() {
2011-06-13 18:09:44 +00:00
expect( 5 );
var element = $( "#autocomplete" ).autocomplete();
equal( element.autocomplete( "widget" ).parent()[0], document.body, "defaults to body" );
element.autocomplete( "destroy" );
2011-06-13 18:09:44 +00:00
element.autocomplete({
appendTo: ".ac-wrap"
});
2011-06-13 18:09:44 +00:00
equal( element.autocomplete( "widget" ).parent()[0], $( "#ac-wrap1" )[0], "first found element" );
equal( $( "#ac-wrap2 .ui-autocomplete" ).length, 0, "only appends to one element" );
element.autocomplete( "destroy" );
2011-06-13 18:09:44 +00:00
element.autocomplete({
appendTo: null
});
2011-06-13 18:09:44 +00:00
equal( element.autocomplete( "widget" ).parent()[0], document.body, "null" );
element.autocomplete( "destroy" );
element.autocomplete().autocomplete( "option", "appendTo", "#ac-wrap1" );
equal( element.autocomplete( "widget" ).parent()[0], $( "#ac-wrap1" )[0], "modified after init" );
element.autocomplete( "destroy" );
});
function autoFocusTest( afValue, focusedLength ) {
2011-06-13 18:09:44 +00:00
var element = $( "#autocomplete" ).autocomplete({
autoFocus: afValue,
delay: 0,
source: data,
open: function( event, ui ) {
2011-06-13 18:09:44 +00:00
equal( element.autocomplete( "widget" ).children( ".ui-menu-item:first .ui-state-focus" ).length,
focusedLength, "first item is " + (afValue ? "" : "not") + " auto focused" );
start();
}
});
2011-06-13 18:09:44 +00:00
element.val( "ja" ).keydown();
stop();
}
test( "autoFocus: false", function() {
2011-06-13 18:09:44 +00:00
expect( 1 );
autoFocusTest( false, 0 );
});
test( "autoFocus: true", function() {
2011-06-13 18:09:44 +00:00
expect( 1 );
autoFocusTest( true, 1 );
});
2011-06-13 18:09:44 +00:00
asyncTest( "delay", function() {
expect( 2 );
var element = $( "#autocomplete" ).autocomplete({
source: data,
delay: 50
}),
menu = element.autocomplete( "widget" );
element.val( "ja" ).keydown();
ok( menu.is( ":hidden" ), "menu is closed immediately after search" );
2010-01-20 14:00:14 +00:00
setTimeout(function() {
2011-06-13 18:09:44 +00:00
ok( menu.is( ":visible" ), "menu is open after delay" );
start();
2011-06-13 18:09:44 +00:00
}, 100 );
});
2011-06-13 18:09:44 +00:00
asyncTest( "disabled", function() {
expect( 2 );
var element = $( "#autocomplete" ).autocomplete({
source: data,
delay: 0,
disabled: true
}),
menu = element.autocomplete( "widget" );
element.val( "ja" ).keydown();
2010-01-20 14:00:14 +00:00
2011-06-13 18:09:44 +00:00
ok( menu.is( ":hidden" ) );
2010-01-20 14:00:14 +00:00
2011-06-13 18:09:44 +00:00
setTimeout(function() {
ok( menu.is( ":hidden" ) );
start();
}, 50 );
});
test( "minLength", function() {
expect( 2 );
var element = $( "#autocomplete" ).autocomplete({
source: data
}),
menu = element.autocomplete( "widget" );
element.autocomplete( "search", "" );
ok( menu.is( ":hidden" ), "blank not enough for minLength: 1" );
element.autocomplete( "option", "minLength", 0 );
element.autocomplete( "search", "" );
ok( menu.is( ":visible" ), "blank enough for minLength: 0" );
});
test( "source, local string array", function() {
expect( 1 );
var element = $( "#autocomplete" ).autocomplete({
source: data
}),
menu = element.autocomplete( "widget" );
element.val( "ja" ).autocomplete( "search" );
equal( menu.find( ".ui-menu-item" ).text(), "javajavascript" );
});
function sourceTest( source, async ) {
var element = $( "#autocomplete" ).autocomplete({
source: source
}),
menu = element.autocomplete( "widget" );
element.val( "ja" ).autocomplete( "search" );
function result() {
equal( menu.find( ".ui-menu-item" ).text(), "javajavascript" );
element.autocomplete( "destroy" );
if ( async ) {
start();
}
2010-01-20 14:00:14 +00:00
}
2011-06-13 18:09:44 +00:00
if ( async ) {
2010-01-20 14:00:14 +00:00
stop();
2011-06-13 18:09:44 +00:00
$( document ).one( "ajaxStop", result );
2010-01-20 14:00:14 +00:00
} else {
result();
}
}
2011-06-13 18:09:44 +00:00
test( "source, local object array, only label property", function() {
expect( 1 );
sourceTest([
{ label: "java" },
{ label: "php" },
{ label: "coldfusion" },
{ label: "javascript" }
2010-01-20 14:00:14 +00:00
]);
});
2011-06-13 18:09:44 +00:00
test( "source, local object array, only value property", function() {
expect( 1 );
sourceTest([
{ value: "java" },
{ value: "php" },
{ value: "coldfusion" },
{ value: "javascript" }
2010-01-20 14:00:14 +00:00
]);
});
2011-06-13 18:09:44 +00:00
test( "source, url string with remote json string array", function() {
expect( 1 );
sourceTest( "remote_string_array.txt", true );
2010-01-20 14:00:14 +00:00
});
2011-06-13 18:09:44 +00:00
test( "source, url string with remote json object array, only value properties", function() {
expect( 1 );
sourceTest( "remote_object_array_values.txt", true );
2010-01-20 14:00:14 +00:00
});
2011-06-13 18:09:44 +00:00
test( "source, url string with remote json object array, only label properties", function() {
expect( 1 );
sourceTest( "remote_object_array_labels.txt", true );
2010-01-20 14:00:14 +00:00
});
2011-06-13 18:09:44 +00:00
test( "source, custom", function() {
expect( 2 );
sourceTest(function( request, response ) {
equal( request.term, "ja" );
response( ["java", "javascript"] );
2010-01-20 14:00:14 +00:00
});
});
2011-06-13 18:09:44 +00:00
test( "source, update after init", function() {
expect( 2 );
var element = $( "#autocomplete" ).autocomplete({
source: [ "java", "javascript", "haskell" ]
}),
menu = element.autocomplete( "widget" );
element.val( "ja" ).autocomplete( "search" );
equal( menu.find( ".ui-menu-item" ).text(), "javajavascript" );
element.autocomplete( "option", "source", [ "php", "asp" ] );
element.val( "ph" ).autocomplete( "search" );
equal( menu.find( ".ui-menu-item" ).text(), "php" );
2010-01-20 14:00:14 +00:00
});
2011-06-13 18:09:44 +00:00
}( jQuery ) );