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
|
|
|
|
2013-04-30 14:27:22 +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
|
|
|
|
2014-01-20 21:13:55 +00:00
|
|
|
test( "appendTo: null", function() {
|
|
|
|
expect( 1 );
|
|
|
|
var element = $( "#autocomplete" ).autocomplete();
|
2013-04-30 14:27:22 +00:00
|
|
|
equal( element.autocomplete( "widget" ).parent()[ 0 ], document.body,
|
|
|
|
"defaults to body" );
|
2011-06-13 18:09:44 +00:00
|
|
|
element.autocomplete( "destroy" );
|
2014-01-20 21:13:55 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test( "appendTo: explicit", function() {
|
|
|
|
expect( 6 );
|
|
|
|
var detached = $( "<div>" ),
|
|
|
|
element = $( "#autocomplete" );
|
2010-07-21 18:54:20 +00:00
|
|
|
|
2011-06-13 18:09:44 +00:00
|
|
|
element.autocomplete({
|
2013-04-30 14:27:22 +00:00
|
|
|
appendTo: ".autocomplete-wrap"
|
2010-07-21 18:54:20 +00:00
|
|
|
});
|
2013-04-30 14:27:22 +00:00
|
|
|
equal( element.autocomplete( "widget" ).parent()[ 0 ],
|
|
|
|
$( "#autocomplete-wrap1" )[ 0 ], "first found element" );
|
|
|
|
equal( $( "#autocomplete-wrap2 .ui-autocomplete" ).length, 0,
|
|
|
|
"only appends to one element" );
|
2011-06-13 18:09:44 +00:00
|
|
|
element.autocomplete( "destroy" );
|
2010-07-21 18:54:20 +00:00
|
|
|
|
2013-04-30 14:27:22 +00:00
|
|
|
element.autocomplete().autocomplete( "option", "appendTo", "#autocomplete-wrap1" );
|
|
|
|
equal( element.autocomplete( "widget" ).parent()[ 0 ],
|
|
|
|
$( "#autocomplete-wrap1" )[ 0 ], "modified after init" );
|
2011-06-13 18:09:44 +00:00
|
|
|
element.autocomplete( "destroy" );
|
2012-11-28 15:33:44 +00:00
|
|
|
|
|
|
|
element.autocomplete({
|
|
|
|
appendTo: detached
|
|
|
|
});
|
2013-04-30 14:27:22 +00:00
|
|
|
equal( element.autocomplete( "widget" ).parent()[ 0 ], detached[ 0 ],
|
|
|
|
"detached jQuery object" );
|
2012-11-28 15:33:44 +00:00
|
|
|
element.autocomplete( "destroy" );
|
|
|
|
|
|
|
|
element.autocomplete({
|
2013-04-30 14:27:22 +00:00
|
|
|
appendTo: detached[ 0 ]
|
2012-11-28 15:33:44 +00:00
|
|
|
});
|
2013-04-30 14:27:22 +00:00
|
|
|
equal( element.autocomplete( "widget" ).parent()[ 0 ], detached[ 0 ],
|
|
|
|
"detached DOM element" );
|
2012-11-28 15:33:44 +00:00
|
|
|
element.autocomplete( "destroy" );
|
2012-12-07 16:58:58 +00:00
|
|
|
|
|
|
|
element.autocomplete().autocomplete( "option", "appendTo", detached );
|
2013-04-30 14:27:22 +00:00
|
|
|
equal( element.autocomplete( "widget" ).parent()[ 0 ], detached[ 0 ],
|
|
|
|
"detached DOM element via option()" );
|
2012-12-07 16:58:58 +00:00
|
|
|
element.autocomplete( "destroy" );
|
2010-07-21 18:54:20 +00:00
|
|
|
});
|
|
|
|
|
2014-01-20 21:13:55 +00:00
|
|
|
test( "appendTo: ui-front", function() {
|
|
|
|
expect( 2 );
|
|
|
|
var element = $( "#autocomplete" );
|
|
|
|
|
|
|
|
$( "#autocomplete-wrap2" ).addClass( "ui-front" );
|
|
|
|
element.autocomplete();
|
|
|
|
equal( element.autocomplete( "widget" ).parent()[ 0 ],
|
|
|
|
$( "#autocomplete-wrap2" )[ 0 ], "null, inside .ui-front" );
|
|
|
|
element.autocomplete( "destroy" );
|
|
|
|
|
|
|
|
element.autocomplete({
|
|
|
|
appendTo: $()
|
|
|
|
});
|
|
|
|
equal( element.autocomplete( "widget" ).parent()[ 0 ],
|
2014-02-21 12:35:04 +00:00
|
|
|
$( "#autocomplete-wrap2" )[ 0 ], "empty jQuery object, inside .ui-front" );
|
2014-01-20 21:13:55 +00:00
|
|
|
});
|
|
|
|
|
2011-03-15 13:36:36 +00:00
|
|
|
function autoFocusTest( afValue, focusedLength ) {
|
2011-06-13 18:09:44 +00:00
|
|
|
var element = $( "#autocomplete" ).autocomplete({
|
2011-03-15 13:36:36 +00:00
|
|
|
autoFocus: afValue,
|
2011-03-15 12:42:28 +00:00
|
|
|
delay: 0,
|
|
|
|
source: data,
|
2012-10-23 14:36:42 +00:00
|
|
|
open: function() {
|
2013-04-30 14:27:22 +00:00
|
|
|
equal(
|
|
|
|
element.autocomplete( "widget" )
|
2013-10-06 03:19:57 +00:00
|
|
|
.children( ".ui-menu-item.ui-state-focus" )
|
2013-04-30 14:27:22 +00:00
|
|
|
.length,
|
|
|
|
focusedLength,
|
|
|
|
"first item is " + (afValue ? "" : "not") + " auto focused" );
|
2011-06-13 18:09:44 +00:00
|
|
|
start();
|
2011-03-15 12:42:28 +00:00
|
|
|
}
|
|
|
|
});
|
2011-06-13 18:09:44 +00:00
|
|
|
element.val( "ja" ).keydown();
|
2011-03-15 12:42:28 +00:00
|
|
|
stop();
|
2011-03-15 13:36:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
test( "autoFocus: false", function() {
|
2011-06-13 18:09:44 +00:00
|
|
|
expect( 1 );
|
2011-03-15 13:36:36 +00:00
|
|
|
autoFocusTest( false, 0 );
|
2011-03-15 12:42:28 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
test( "autoFocus: true", function() {
|
2011-06-13 18:09:44 +00:00
|
|
|
expect( 1 );
|
2011-03-15 13:36:36 +00:00
|
|
|
autoFocusTest( true, 1 );
|
2011-03-15 12:42:28 +00:00
|
|
|
});
|
2010-07-21 18:54:20 +00:00
|
|
|
|
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
|
|
|
|
2010-07-30 16:59:33 +00:00
|
|
|
setTimeout(function() {
|
2011-06-13 18:09:44 +00:00
|
|
|
ok( menu.is( ":visible" ), "menu is open after delay" );
|
2010-07-30 16:59:33 +00:00
|
|
|
start();
|
2011-06-13 18:09:44 +00:00
|
|
|
}, 100 );
|
2010-07-30 16:59:33 +00:00
|
|
|
});
|
|
|
|
|
2011-06-13 18:09:44 +00:00
|
|
|
asyncTest( "disabled", function() {
|
2013-03-14 23:08:21 +00:00
|
|
|
expect( 5 );
|
2011-06-13 18:09:44 +00:00
|
|
|
var element = $( "#autocomplete" ).autocomplete({
|
|
|
|
source: data,
|
2013-03-14 23:08:21 +00:00
|
|
|
delay: 0
|
2011-06-13 18:09:44 +00:00
|
|
|
}),
|
2013-03-14 23:08:21 +00:00
|
|
|
menu = element.autocomplete( "disable" ).autocomplete( "widget" );
|
2011-06-13 18:09:44 +00:00
|
|
|
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
|
|
|
|
2013-03-14 23:08:21 +00:00
|
|
|
ok( !element.is( ".ui-state-disabled" ), "element doesn't get ui-state-disabled" );
|
|
|
|
ok( !element.attr( "aria-disabled" ), "element doesn't get aria-disabled" );
|
|
|
|
ok( menu.is( ".ui-autocomplete-disabled" ), "element gets ui-autocomplete-disabled" );
|
|
|
|
|
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" );
|
|
|
|
});
|
|
|
|
|
2012-02-11 15:12:51 +00:00
|
|
|
asyncTest( "minLength, exceed then drop below", function() {
|
|
|
|
expect( 4 );
|
|
|
|
var element = $( "#autocomplete" ).autocomplete({
|
|
|
|
minLength: 2,
|
|
|
|
source: function( req, res ) {
|
|
|
|
equal( req.term, "12", "correct search term" );
|
|
|
|
setTimeout(function() {
|
|
|
|
res([ "item" ]);
|
|
|
|
}, 1 );
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
menu = element.autocomplete( "widget" );
|
|
|
|
|
|
|
|
ok( menu.is( ":hidden" ), "menu is hidden before first search" );
|
|
|
|
element.autocomplete( "search", "12" );
|
|
|
|
|
|
|
|
ok( menu.is( ":hidden" ), "menu is hidden before second search" );
|
|
|
|
element.autocomplete( "search", "1" );
|
|
|
|
|
|
|
|
setTimeout(function() {
|
|
|
|
ok( menu.is( ":hidden" ), "menu is hidden after searches" );
|
|
|
|
start();
|
|
|
|
}, 50 );
|
|
|
|
});
|
|
|
|
|
2012-02-15 16:41:30 +00:00
|
|
|
test( "minLength, exceed then drop below then exceed", function() {
|
|
|
|
expect( 3 );
|
|
|
|
var _res = [],
|
|
|
|
element = $( "#autocomplete" ).autocomplete({
|
|
|
|
minLength: 2,
|
|
|
|
source: function( req, res ) {
|
|
|
|
_res.push( res );
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
menu = element.autocomplete( "widget" );
|
|
|
|
|
|
|
|
// trigger a valid search
|
|
|
|
ok( menu.is( ":hidden" ), "menu is hidden before first search" );
|
|
|
|
element.autocomplete( "search", "12" );
|
|
|
|
|
|
|
|
// trigger a search below the minLength, to turn on cancelSearch flag
|
|
|
|
ok( menu.is( ":hidden" ), "menu is hidden before second search" );
|
|
|
|
element.autocomplete( "search", "1" );
|
|
|
|
|
|
|
|
// trigger a valid search
|
|
|
|
element.autocomplete( "search", "13" );
|
|
|
|
// react as if the first search was cancelled (default ajax behavior)
|
|
|
|
_res[ 0 ]([]);
|
|
|
|
// react to second search
|
|
|
|
_res[ 1 ]([ "13" ]);
|
|
|
|
|
|
|
|
ok( menu.is( ":visible" ), "menu is visible after searches" );
|
|
|
|
});
|
2012-02-11 15:12:51 +00:00
|
|
|
|
2011-06-13 18:09:44 +00:00
|
|
|
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" );
|
|
|
|
function result() {
|
2014-01-22 15:40:19 +00:00
|
|
|
var items = menu.find( ".ui-menu-item" );
|
|
|
|
equal( items.length, 3, "Should find three results." );
|
|
|
|
deepEqual( items.eq( 0 ).data( "ui-autocomplete-item" ), {
|
|
|
|
label: "java",
|
|
|
|
value: "java"
|
|
|
|
});
|
|
|
|
deepEqual( items.eq( 1 ).data( "ui-autocomplete-item" ), {
|
|
|
|
label: "javascript",
|
|
|
|
value: "javascript"
|
|
|
|
});
|
|
|
|
deepEqual( items.eq( 2 ).data( "ui-autocomplete-item" ), {
|
|
|
|
label: "clojure",
|
|
|
|
value: "clojure"
|
|
|
|
});
|
2011-06-13 18:09:44 +00:00
|
|
|
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 );
|
2011-08-03 20:12:46 +00:00
|
|
|
}
|
2014-01-22 15:40:19 +00:00
|
|
|
element.val( "j" ).autocomplete( "search" );
|
2011-08-03 20:12:46 +00:00
|
|
|
if ( !async ) {
|
2010-01-20 14:00:14 +00:00
|
|
|
result();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-22 15:40:19 +00:00
|
|
|
test( "source, local object array, only labels", function() {
|
|
|
|
expect( 4 );
|
2011-06-13 18:09:44 +00:00
|
|
|
sourceTest([
|
2014-01-22 15:40:19 +00:00
|
|
|
{ label: "java", value: null },
|
|
|
|
{ label: "php", value: null },
|
|
|
|
{ label: "coldfusion", value: "" },
|
|
|
|
{ label: "javascript", value: "" },
|
|
|
|
{ label: "clojure" }
|
2010-01-20 14:00:14 +00:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
2014-01-22 15:40:19 +00:00
|
|
|
test( "source, local object array, only values", function() {
|
|
|
|
expect( 4 );
|
2011-06-13 18:09:44 +00:00
|
|
|
sourceTest([
|
2014-01-22 15:40:19 +00:00
|
|
|
{ value: "java", label: null },
|
|
|
|
{ value: "php", label: null },
|
|
|
|
{ value: "coldfusion", label: "" },
|
|
|
|
{ value: "javascript", label: "" },
|
|
|
|
{ value: "clojure" }
|
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() {
|
2014-01-22 15:40:19 +00:00
|
|
|
expect( 4 );
|
2011-06-13 18:09:44 +00:00
|
|
|
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() {
|
2014-01-22 15:40:19 +00:00
|
|
|
expect( 4 );
|
2011-06-13 18:09:44 +00:00
|
|
|
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() {
|
2014-01-22 15:40:19 +00:00
|
|
|
expect( 4 );
|
2011-06-13 18:09:44 +00:00
|
|
|
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() {
|
2014-01-22 15:40:19 +00:00
|
|
|
expect( 5 );
|
2011-06-13 18:09:44 +00:00
|
|
|
sourceTest(function( request, response ) {
|
2014-01-22 15:40:19 +00:00
|
|
|
equal( request.term, "j" );
|
|
|
|
response([
|
|
|
|
"java",
|
|
|
|
{ label: "javascript", value: null },
|
|
|
|
{ value: "clojure", label: null }
|
|
|
|
]);
|
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 ) );
|