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
|
define( [
"qunit",
"jquery",
"lib/helper",
"ui/widgets/autocomplete"
], function( QUnit, $, helper ) {
"use strict";
QUnit.module( "autocomplete: events", { afterEach: helper.moduleAfterEach } );
var data = [ "Clojure", "COBOL", "ColdFusion", "Java", "JavaScript", "Scala", "Scheme" ];
$.each( [
{
type: "input",
selector: "#autocomplete",
valueMethod: "val"
},
{
type: "textarea",
selector: "#autocomplete-textarea",
valueMethod: "val"
},
{
type: "contenteditable",
selector: "#autocomplete-contenteditable",
valueMethod: "text"
}
], function( i, settings ) {
QUnit.test( "all events - " + settings.type, function( assert ) {
var ready = assert.async();
assert.expect( 13 );
var element = $( settings.selector )
.autocomplete( {
autoFocus: false,
delay: 0,
source: data,
search: function( event ) {
assert.equal( event.originalEvent.type, "keydown", "search originalEvent" );
},
response: function( event, ui ) {
assert.deepEqual( ui.content, [
{ label: "Clojure", value: "Clojure" },
{ label: "Java", value: "Java" },
{ label: "JavaScript", value: "JavaScript" }
], "response ui.content" );
ui.content.splice( 0, 1 );
},
open: function() {
assert.ok( menu.is( ":visible" ), "menu open on open" );
},
focus: function( event, ui ) {
assert.equal( event.originalEvent.type, "menufocus", "focus originalEvent" );
assert.deepEqual( ui.item, { label: "Java", value: "Java" }, "focus ui.item" );
},
close: function( event ) {
assert.equal( event.originalEvent.type, "menuselect", "close originalEvent" );
assert.ok( menu.is( ":hidden" ), "menu closed on close" );
},
select: function( event, ui ) {
assert.equal( event.originalEvent.type, "menuselect", "select originalEvent" );
assert.deepEqual( ui.item, { label: "Java", value: "Java" }, "select ui.item" );
},
change: function( event, ui ) {
assert.equal( event.originalEvent.type, "blur", "change originalEvent" );
assert.deepEqual( ui.item, { label: "Java", value: "Java" }, "change ui.item" );
assert.ok( menu.is( ":hidden" ), "menu closed on change" );
ready();
}
} ),
menu = element.autocomplete( "widget" );
element.simulate( "focus" )[ settings.valueMethod ]( "j" ).trigger( "keydown" );
setTimeout( function() {
assert.ok( menu.is( ":visible" ), "menu is visible after delay" );
element.simulate( "keydown", { keyCode: $.ui.keyCode.DOWN } );
element.simulate( "keydown", { keyCode: $.ui.keyCode.ENTER } );
// Blur must be async for IE to handle it properly
setTimeout( function() {
element.simulate( "blur" );
} );
} );
} );
} );
QUnit.test( "change without selection", function( assert ) {
var ready = assert.async();
assert.expect( 1 );
var element = $( "#autocomplete" ).autocomplete( {
delay: 0,
source: data,
change: function( event, ui ) {
assert.strictEqual( ui.item, null );
ready();
}
} );
element.triggerHandler( "focus" );
element.val( "ja" ).triggerHandler( "blur" );
} );
QUnit.test( "cancel search", function( assert ) {
var ready = assert.async();
assert.expect( 6 );
var first = true,
element = $( "#autocomplete" ).autocomplete( {
delay: 0,
source: data,
search: function() {
if ( first ) {
assert.equal( element.val(), "ja", "val on first search" );
first = false;
return false;
}
assert.equal( element.val(), "java", "val on second search" );
},
open: function() {
assert.ok( true, "menu opened" );
}
} ),
menu = element.autocomplete( "widget" );
element.val( "ja" ).trigger( "keydown" );
setTimeout( function() {
assert.ok( menu.is( ":hidden" ), "menu is hidden after first search" );
element.val( "java" ).trigger( "keydown" );
setTimeout( function() {
assert.ok( menu.is( ":visible" ), "menu is visible after second search" );
assert.equal( menu.find( ".ui-menu-item" ).length, 2, "# of menu items" );
ready();
} );
} );
} );
QUnit.test( "cancel focus", function( assert ) {
var ready = assert.async();
assert.expect( 1 );
var customVal = "custom value",
element = $( "#autocomplete" ).autocomplete( {
delay: 0,
source: data,
focus: function() {
$( this ).val( customVal );
return false;
}
} );
element.val( "ja" ).trigger( "keydown" );
setTimeout( function() {
element.simulate( "keydown", { keyCode: $.ui.keyCode.DOWN } );
assert.equal( element.val(), customVal );
ready();
} );
} );
QUnit.test( "cancel select", function( assert ) {
var ready = assert.async();
assert.expect( 1 );
var customVal = "custom value",
element = $( "#autocomplete" ).autocomplete( {
delay: 0,
source: data,
select: function() {
$( this ).val( customVal );
return false;
}
} );
element.val( "ja" ).trigger( "keydown" );
setTimeout( function() {
element.simulate( "keydown", { keyCode: $.ui.keyCode.DOWN } );
element.simulate( "keydown", { keyCode: $.ui.keyCode.ENTER } );
assert.equal( element.val(), customVal );
ready();
} );
} );
QUnit.test( "blur during remote search", function( assert ) {
var ready = assert.async();
assert.expect( 1 );
var ac = $( "#autocomplete" ).autocomplete( {
delay: 0,
source: function( request, response ) {
assert.ok( true, "trigger request" );
ac.simulate( "blur" );
setTimeout( function() {
response( [ "result" ] );
ready();
}, 25 );
},
open: function() {
assert.ok( false, "opened after a blur" );
}
} );
ac.val( "ro" ).trigger( "keydown" );
} );
} );
|