aboutsummaryrefslogtreecommitdiffstats
path: root/tests/unit/autocomplete/autocomplete_options.js
blob: 6639e90147884109880369c79f98fe52e2b6e9b9 (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
/*
 * 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("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("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();
		setTimeout(result, 100);
	} 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);