aboutsummaryrefslogtreecommitdiffstats
path: root/tests/unit/autocomplete/autocomplete_events.js
blob: a8924e192a4d831aaa258993aba100c2f51b9a37 (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
/*
 * autocomplete_events.js
 */
(function($) {

module("autocomplete: events", {
	teardown: function() {
		$( ":ui-autocomplete" ).autocomplete( "destroy" );
	}
});

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

test("all events", function() {
	expect(12);
	var ac = $("#autocomplete").autocomplete({
		delay: 0,
		source: data,
		search: function(event) {
			same(event.type, "autocompletesearch");
		},
		open: function(event) {
			same(event.type, "autocompleteopen");
		},
		focus: function(event, ui) {
			same(event.type, "autocompletefocus");
			same(ui.item, {label:"java", value:"java"});
		},
		close: function(event) {
			same(event.type, "autocompleteclose");
			same( $(".ui-menu:visible").length, 1 );
		},
		select: function(event, ui) {
			same(event.type, "autocompleteselect");
			same(ui.item, {label:"java", value:"java"});
		},
		change: function(event, ui) {
			same(event.type, "autocompletechange");
			same(ui.item, {label:"java", value:"java"});
			same( $(".ui-menu:visible").length, 0 );
			start();
		}
	});
	stop();
	ac.focus().val("ja").keydown();
	setTimeout(function() {
		same( $(".ui-menu:visible").length, 1 );
		ac.simulate("keydown", { keyCode: $.ui.keyCode.DOWN });
		ac.simulate("keydown", { keyCode: $.ui.keyCode.ENTER });
		$.browser.msie ? ac.simulate("blur") : ac.blur();
	}, 50);
});

test("change without selection", function() {
	expect(2);
	stop();
	var ac = $("#autocomplete").autocomplete({
		delay: 0,
		source: data,
		change: function(event, ui) {
			same(event.type, "autocompletechange");
			same(ui.item, null);
			start();
		}
	});
	ac.triggerHandler("focus");
	ac.val("ja").triggerHandler("blur");
});

test("cancel search", function() {
	expect(6);
	var first = true;
	var ac = $("#autocomplete").autocomplete({
		delay: 0,
		source: data,
		search: function() {
			if (first) {
				same( ac.val(), "ja" );
				first = false;
				return false;
			}
			same( ac.val(), "java" );
		},
		open: function() {
			ok(true);
		}
	});
	stop();
	ac.val("ja").keydown();
	setTimeout(function() {
		same( $(".ui-menu:visible").length, 0 );
		ac.val("java").keydown();
		setTimeout(function() {
			same( $(".ui-menu:visible").length, 1 );
			same( $(".ui-menu .ui-menu-item").length, 2 );
			start();
		}, 50);
	}, 50);
});

test("cancel focus", function() {
	expect(1);
	var customVal = 'custom value';
	var ac = $("#autocomplete").autocomplete({
		delay: 0,
		source: data,
		focus: function(event, ui) {
			$(this).val(customVal);
			return false;
		}
	});
	stop();
	ac.val("ja").keydown();
	setTimeout(function() {
		ac.simulate("keydown", { keyCode: $.ui.keyCode.DOWN });
		same( ac.val(), customVal );
		start();
	}, 50);
});

test("cancel select", function() {
	expect(1);
	var customVal = 'custom value';
	var ac = $("#autocomplete").autocomplete({
		delay: 0,
		source: data,
		select: function(event, ui) {
			$(this).val(customVal);
			return false;
		}
	});
	stop();
	ac.val("ja").keydown();
	setTimeout(function() {
		ac.simulate("keydown", { keyCode: $.ui.keyCode.DOWN });
		ac.simulate("keydown", { keyCode: $.ui.keyCode.ENTER });
		same( ac.val(), customVal );
		start();
	}, 50);
});

})(jQuery);