aboutsummaryrefslogtreecommitdiffstats
path: root/tests/unit/autocomplete/autocomplete_options.js
blob: c2cb2149df0421801e180a12960db1a02586f88c (plain)
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
(function( $ ) {

module( "autocomplete: options" );

var data = [ "c++", "java", "php", "coldfusion", "javascript", "asp", "ruby",
	"python", "c", "scala", "groovy", "haskell", "perl" ];

test( "appendTo: null", function() {
	expect( 1 );
	var element = $( "#autocomplete" ).autocomplete();
	equal( element.autocomplete( "widget" ).parent()[ 0 ], document.body,
		"defaults to body" );
	element.autocomplete( "destroy" );
});

test( "appendTo: explicit", function() {
	expect( 6 );
	var detached = $( "<div>" ),
		element = $( "#autocomplete" );

	element.autocomplete({
		appendTo: ".autocomplete-wrap"
	});
	equal( element.autocomplete( "widget" ).parent()[ 0 ],
		$( "#autocomplete-wrap1" )[ 0 ], "first found element" );
	equal( $( "#autocomplete-wrap2 .ui-autocomplete" ).length, 0,
		"only appends to one element" );
	element.autocomplete( "destroy" );

	element.autocomplete().autocomplete( "option", "appendTo", "#autocomplete-wrap1" );
	equal( element.autocomplete( "widget" ).parent()[ 0 ],
		$( "#autocomplete-wrap1" )[ 0 ], "modified after init" );
	element.autocomplete( "destroy" );

	element.autocomplete({
		appendTo: detached
	});
	equal( element.autocomplete( "widget" ).parent()[ 0 ], detached[ 0 ],
		"detached jQuery object" );
	element.autocomplete( "destroy" );

	element.autocomplete({
		appendTo: detached[ 0 ]
	});
	equal( element.autocomplete( "widget" ).parent()[ 0 ], detached[ 0 ],
		"detached DOM element" );
	element.autocomplete( "destroy" );

	element.autocomplete().autocomplete( "option", "appendTo", detached );
	equal( element.autocomplete( "widget" ).parent()[ 0 ], detached[ 0 ],
		"detached DOM element via option()" );
	element.autocomplete( "destroy" );
});

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 ],
		$( "#autocomplete-wrap2" )[ 0 ], "null, inside .ui-front" );
});

function autoFocusTest( afValue, focusedLength ) {
	var element = $( "#autocomplete" ).autocomplete({
		autoFocus: afValue,
		delay: 0,
		source: data,
		open: function() {
			equal(
				element.autocomplete( "widget" )
					.children( ".ui-menu-item.ui-state-focus" )
					.length,
				focusedLength,
				"first item is " + (afValue ? "" : "not") + " auto focused" );
			start();
		}
	});
	element.val( "ja" ).keydown();
	stop();
}

test( "autoFocus: false", function() {
	expect( 1 );
	autoFocusTest( false, 0 );
});

test( "autoFocus: true", function() {
	expect( 1 );
	autoFocusTest( true, 1 );
});

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" );

	setTimeout(function() {
		ok( menu.is( ":visible" ), "menu is open after delay" );
		start();
	}, 100 );
});

asyncTest( "disabled", function() {
	expect( 5 );
	var element = $( "#autocomplete" ).autocomplete({
			source: data,
			delay: 0
		}),
		menu = element.autocomplete( "disable" ).autocomplete( "widget" );
	element.val( "ja" ).keydown();

	ok( menu.is( ":hidden" ) );

	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" );

	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" );
});

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 );
});

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" );
});

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() {
		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"
		});
		element.autocomplete( "destroy" );
		if ( async ) {
			start();
		}
	}
	if ( async ) {
		stop();
		$( document ).one( "ajaxStop", result );
	}
	element.val( "j" ).autocomplete( "search" );
	if ( !async ) {
		result();
	}
}

test( "source, local object array, only labels", function() {
	expect( 4 );
	sourceTest([
		{ label: "java", value: null },
		{ label: "php", value: null },
		{ label: "coldfusion", value: "" },
		{ label: "javascript", value: "" },
		{ label: "clojure" }
	]);
});

test( "source, local object array, only values", function() {
	expect( 4 );
	sourceTest([
		{ value: "java", label: null },
		{ value: "php", label: null },
		{ value: "coldfusion", label: "" },
		{ value: "javascript", label: "" },
		{ value: "clojure" }
	]);
});

test( "source, url string with remote json string array", function() {
	expect( 4 );
	sourceTest( "remote_string_array.txt", true );
});

test( "source, url string with remote json object array, only value properties", function() {
	expect( 4 );
	sourceTest( "remote_object_array_values.txt", true );
});

test( "source, url string with remote json object array, only label properties", function() {
	expect( 4 );
	sourceTest( "remote_object_array_labels.txt", true );
});

test( "source, custom", function() {
	expect( 5 );
	sourceTest(function( request, response ) {
		equal( request.term, "j" );
		response([
			"java",
			{ label: "javascript", value: null },
			{ value: "clojure", label: null }
		]);
	});
});

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" );
});

}( jQuery ) );