1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
/*
* autocomplete_options.js
*/
(function($) {
module("autocomplete: options", {
teardown: function() {
$( ":ui-autocomplete" ).autocomplete( "destroy" );
}
});
/* 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
}));
});
*/
var data = ["c++", "java", "php", "coldfusion", "javascript", "asp", "ruby", "python", "c", "scala", "groovy", "haskell", "perl"];
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" );
});
test( "autoFocus: false", function() {
var ac = $( "#autocomplete" ).autocomplete({
autoFocus: false,
delay: 0,
source: data,
open: function( event, ui ) {
equals( 0, ac.autocomplete( "widget" ).children( ".ui-menu-item:first .ui-state-focus" ).length, "first item is not auto focused" );
start();
}
});
ac.val( "ja" ).keydown();
stop();
});
test( "autoFocus: true", function() {
var ac = $( "#autocomplete" ).autocomplete({
autoFocus: true,
delay: 0,
source: data,
open: function( event, ui ) {
equals( 1, ac.autocomplete( "widget" ).children( ".ui-menu-item:first .ui-state-focus" ).length, "first item is auto focused" );
start();
}
});
ac.val( "ja" ).keydown();
stop();
});
test("delay", function() {
var ac = $("#autocomplete").autocomplete({
source: data,
delay: 50
});
ac.val("ja").keydown();
same( $(".ui-menu:visible").length, 0 );
// wait half a second for the default delay to open the menu
stop();
setTimeout(function() {
same( $(".ui-menu:visible").length, 1 );
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);
});
test("minLength", function() {
var ac = $("#autocomplete").autocomplete({
source: data
});
ac.autocomplete("search", "");
same( $(".ui-menu:visible").length, 0, "blank not enough for minLength: 1" );
ac.autocomplete("option", "minLength", 0);
ac.autocomplete("search", "");
same( $(".ui-menu:visible").length, 1, "blank enough for minLength: 0" );
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);
} 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);
|