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

248 lines
5.9 KiB
JavaScript
Raw Normal View History

2010-01-20 14:00:14 +00:00
/*
* autocomplete_options.js
*/
(function($) {
module("autocomplete: options", {
teardown: function() {
$( ":ui-autocomplete" ).autocomplete( "destroy" );
}
});
2010-01-20 14:00:14 +00:00
/* disabled until autocomplete actually has built-in support for caching
// returns at most 4 items
function source(request) {
ok(true, "handling a request");
switch(request.term) {
case "cha":
return ["Common Pochard", "Common Chiffchaff", "Common Chaffinch", "Iberian Chiffchaff"]
case "chaf":
case "chaff":
return ["Common Chiffchaff", "Common Chaffinch", "Iberian Chiffchaff"]
case "chaffi":
return ["Common Chaffinch"]
case "schi":
return ["schifpre"]
}
}
function search(input) {
var autocomplete = input.data("autocomplete");
autocomplete.search("cha");
autocomplete.close();
autocomplete.search("chaf");
autocomplete.close();
autocomplete.search("chaff");
autocomplete.close();
autocomplete.search("chaffi");
autocomplete.close();
autocomplete.search("schi");
}
test("cache: default", function() {
expect(2);
search($("#autocomplete").autocomplete({
source: source
}));
});
test("cache: {limit:4}", function() {
expect(3);
search($("#autocomplete").autocomplete({
cache: {
limit: 4
},
source: source
}));
});
test("cache: false", function() {
expect(5);
search($("#autocomplete").autocomplete({
cache: false,
source: source
}));
});
*/
2010-03-20 14:37:18 +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() {
var ac = $( "#autocomplete" ).autocomplete();
same( ac.autocomplete( "widget" ).parent()[0], document.body, "defaults to body" );
ac.autocomplete( "destroy" );
ac.autocomplete({
appendTo: "#ac-wrap2"
});
same( ac.autocomplete( "widget" ).parent()[0], $( "#ac-wrap2" )[0], "id" );
ac.autocomplete( "destroy" );
ac.autocomplete({
appendTo: ".ac-wrap"
});
same( ac.autocomplete( "widget" ).parent()[0], $( "#ac-wrap1" )[0], "class" );
same( $( "#ac-wrap2 .ui-autocomplete").length, 0, "class - only appends to one element")
ac.autocomplete( "destroy" );
ac.autocomplete({
appendTo: null
});
same( ac.autocomplete( "widget" ).parent()[0], document.body, "null" );
ac.autocomplete( "destroy" );
ac.autocomplete().autocomplete( "option", "appendTo", "#ac-wrap1" );
same( ac.autocomplete( "widget" ).parent()[0], $( "#ac-wrap1" )[0], "modified after init" );
ac.autocomplete( "destroy" );
});
function autoFocusTest( afValue, focusedLength ) {
var ac = $( "#autocomplete" ).autocomplete({
autoFocus: afValue,
delay: 0,
source: data,
open: function( event, ui ) {
2011-11-09 16:21:18 +00:00
equal( ac.autocomplete( "widget" ).children( ".ui-menu-item:first" ).find( ".ui-state-hover" ).length, focusedLength, "first item is " + afValue ? "" : "not" + " auto focused" );
start();
}
});
ac.val( "ja" ).keydown();
stop();
}
test( "autoFocus: false", function() {
autoFocusTest( false, 0 );
});
test( "autoFocus: true", function() {
autoFocusTest( true, 1 );
});
2010-01-20 14:00:14 +00:00
test("delay", function() {
var ac = $("#autocomplete").autocomplete({
source: data,
delay: 50
});
ac.val("ja").keydown();
same( $(".ui-menu:visible").length, 0 );
2010-01-20 14:00:14 +00:00
// wait half a second for the default delay to open the menu
stop();
setTimeout(function() {
same( $(".ui-menu:visible").length, 1 );
2010-01-20 14:00:14 +00:00
ac.autocomplete("destroy");
start();
}, 100);
});
test("disabled", function() {
var ac = $("#autocomplete").autocomplete({
source: data,
delay: 0,
disabled: true
});
ac.val("ja").keydown();
same( $(".ui-menu:visible").length, 0 );
stop();
setTimeout(function() {
same( $(".ui-menu:visible").length, 0 );
ac.autocomplete("destroy");
start();
}, 50);
});
2010-01-20 14:00:14 +00:00
test("minLength", function() {
var ac = $("#autocomplete").autocomplete({
source: data
});
ac.autocomplete("search", "");
same( $(".ui-menu:visible").length, 0, "blank not enough for minLength: 1" );
2010-01-20 14:00:14 +00:00
ac.autocomplete("option", "minLength", 0);
ac.autocomplete("search", "");
same( $(".ui-menu:visible").length, 1, "blank enough for minLength: 0" );
2010-01-20 14:00:14 +00:00
ac.autocomplete("destroy");
});
test("source, local string array", function() {
var ac = $("#autocomplete").autocomplete({
source: data
});
ac.val("ja").autocomplete("search");
same( $(".ui-menu .ui-menu-item").text(), "javajavascript" );
ac.autocomplete("destroy");
});
function source_test(source, async) {
var ac = $("#autocomplete").autocomplete({
source: source
});
ac.val("ja").autocomplete("search");
function result(){
same( $(".ui-menu .ui-menu-item").text(), "javajavascript" );
ac.autocomplete("destroy");
async && start();
}
if (async) {
stop();
$(document).one("ajaxStop", result);
2010-01-20 14:00:14 +00:00
} else {
result();
}
}
test("source, local object array, only label property", function() {
source_test([
{label:"java"},
{label:"php"},
{label:"coldfusion"},
{label:"javascript"}
]);
});
test("source, local object array, only value property", function() {
source_test([
{value:"java"},
{value:"php"},
{value:"coldfusion"},
{value:"javascript"}
]);
});
test("source, url string with remote json string array", function() {
source_test("remote_string_array.txt", true);
});
test("source, url string with remote json object array, only value properties", function() {
source_test("remote_object_array_values.txt", true);
});
test("source, url string with remote json object array, only label properties", function() {
source_test("remote_object_array_labels.txt", true);
});
test("source, custom", function() {
source_test(function(request, response) {
same( request.term, "ja" );
response(["java", "javascript"]);
});
});
test("source, update after init", function() {
var ac = $("#autocomplete").autocomplete({
source: ["java", "javascript", "haskell"]
});
ac.val("ja").autocomplete("search");
same( $(".ui-menu .ui-menu-item").text(), "javajavascript" );
ac.autocomplete("option", "source", ["php", "asp"]);
ac.val("ph").autocomplete("search");
same( $(".ui-menu .ui-menu-item").text(), "php" );
ac.autocomplete("destroy");
});
})(jQuery);