2015-04-03 19:57:57 +00:00
|
|
|
define( [
|
2016-04-03 14:51:14 +00:00
|
|
|
"qunit",
|
2015-04-03 19:57:57 +00:00
|
|
|
"jquery",
|
2020-05-16 07:16:24 +00:00
|
|
|
"lib/helper",
|
2015-07-15 01:56:36 +00:00
|
|
|
"ui/widgets/autocomplete"
|
2020-05-16 07:16:24 +00:00
|
|
|
], function( QUnit, $, helper ) {
|
2021-06-06 22:58:12 +00:00
|
|
|
"use strict";
|
2010-01-20 14:00:14 +00:00
|
|
|
|
2020-05-16 07:16:24 +00:00
|
|
|
QUnit.module( "autocomplete: events", { afterEach: helper.moduleAfterEach } );
|
2010-01-20 14:00:14 +00:00
|
|
|
|
2011-03-22 18:07:49 +00:00
|
|
|
var data = [ "Clojure", "COBOL", "ColdFusion", "Java", "JavaScript", "Scala", "Scheme" ];
|
2010-01-20 14:00:14 +00:00
|
|
|
|
2015-08-21 04:11:54 +00:00
|
|
|
$.each( [
|
2011-08-31 00:45:48 +00:00
|
|
|
{
|
|
|
|
type: "input",
|
|
|
|
selector: "#autocomplete",
|
|
|
|
valueMethod: "val"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: "textarea",
|
|
|
|
selector: "#autocomplete-textarea",
|
|
|
|
valueMethod: "val"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: "contenteditable",
|
|
|
|
selector: "#autocomplete-contenteditable",
|
|
|
|
valueMethod: "text"
|
|
|
|
}
|
|
|
|
], function( i, settings ) {
|
2016-04-03 14:51:14 +00:00
|
|
|
QUnit.test( "all events - " + settings.type, function( assert ) {
|
|
|
|
var ready = assert.async();
|
|
|
|
assert.expect( 13 );
|
2011-08-31 00:45:48 +00:00
|
|
|
var element = $( settings.selector )
|
2015-08-21 04:11:54 +00:00
|
|
|
.autocomplete( {
|
2011-08-31 00:45:48 +00:00
|
|
|
autoFocus: false,
|
|
|
|
delay: 0,
|
|
|
|
source: data,
|
|
|
|
search: function( event ) {
|
2016-04-03 14:51:14 +00:00
|
|
|
assert.equal( event.originalEvent.type, "keydown", "search originalEvent" );
|
2011-08-31 00:45:48 +00:00
|
|
|
},
|
|
|
|
response: function( event, ui ) {
|
2016-04-03 14:51:14 +00:00
|
|
|
assert.deepEqual( ui.content, [
|
2011-08-31 00:45:48 +00:00
|
|
|
{ label: "Clojure", value: "Clojure" },
|
|
|
|
{ label: "Java", value: "Java" },
|
|
|
|
{ label: "JavaScript", value: "JavaScript" }
|
|
|
|
], "response ui.content" );
|
|
|
|
ui.content.splice( 0, 1 );
|
|
|
|
},
|
2012-10-23 14:36:42 +00:00
|
|
|
open: function() {
|
2016-04-03 14:51:14 +00:00
|
|
|
assert.ok( menu.is( ":visible" ), "menu open on open" );
|
2011-08-31 00:45:48 +00:00
|
|
|
},
|
|
|
|
focus: function( event, ui ) {
|
2016-04-03 14:51:14 +00:00
|
|
|
assert.equal( event.originalEvent.type, "menufocus", "focus originalEvent" );
|
|
|
|
assert.deepEqual( ui.item, { label: "Java", value: "Java" }, "focus ui.item" );
|
2011-08-31 00:45:48 +00:00
|
|
|
},
|
|
|
|
close: function( event ) {
|
2016-04-03 14:51:14 +00:00
|
|
|
assert.equal( event.originalEvent.type, "menuselect", "close originalEvent" );
|
|
|
|
assert.ok( menu.is( ":hidden" ), "menu closed on close" );
|
2011-08-31 00:45:48 +00:00
|
|
|
},
|
|
|
|
select: function( event, ui ) {
|
2016-04-03 14:51:14 +00:00
|
|
|
assert.equal( event.originalEvent.type, "menuselect", "select originalEvent" );
|
|
|
|
assert.deepEqual( ui.item, { label: "Java", value: "Java" }, "select ui.item" );
|
2011-08-31 00:45:48 +00:00
|
|
|
},
|
|
|
|
change: function( event, ui ) {
|
2016-04-03 14:51:14 +00:00
|
|
|
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();
|
2011-08-31 00:45:48 +00:00
|
|
|
}
|
2015-08-21 04:11:54 +00:00
|
|
|
} ),
|
2011-08-31 00:45:48 +00:00
|
|
|
menu = element.autocomplete( "widget" );
|
2011-10-31 21:28:00 +00:00
|
|
|
|
2015-05-14 01:54:08 +00:00
|
|
|
element.simulate( "focus" )[ settings.valueMethod ]( "j" ).trigger( "keydown" );
|
2015-08-21 04:11:54 +00:00
|
|
|
setTimeout( function() {
|
2016-04-03 14:51:14 +00:00
|
|
|
assert.ok( menu.is( ":visible" ), "menu is visible after delay" );
|
2011-08-31 00:45:48 +00:00
|
|
|
element.simulate( "keydown", { keyCode: $.ui.keyCode.DOWN } );
|
|
|
|
element.simulate( "keydown", { keyCode: $.ui.keyCode.ENTER } );
|
2024-04-30 22:54:19 +00:00
|
|
|
element.simulate( "blur" );
|
2015-08-21 04:11:54 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
} );
|
2010-01-20 14:00:14 +00:00
|
|
|
|
2016-04-03 14:51:14 +00:00
|
|
|
QUnit.test( "change without selection", function( assert ) {
|
|
|
|
var ready = assert.async();
|
|
|
|
assert.expect( 1 );
|
2015-08-21 04:11:54 +00:00
|
|
|
var element = $( "#autocomplete" ).autocomplete( {
|
2010-04-08 10:05:52 +00:00
|
|
|
delay: 0,
|
|
|
|
source: data,
|
2011-06-13 18:09:44 +00:00
|
|
|
change: function( event, ui ) {
|
2016-04-03 14:51:14 +00:00
|
|
|
assert.strictEqual( ui.item, null );
|
|
|
|
ready();
|
2010-04-08 10:05:52 +00:00
|
|
|
}
|
2015-08-21 04:11:54 +00:00
|
|
|
} );
|
2011-06-13 18:09:44 +00:00
|
|
|
element.triggerHandler( "focus" );
|
|
|
|
element.val( "ja" ).triggerHandler( "blur" );
|
2015-08-21 04:11:54 +00:00
|
|
|
} );
|
2010-04-08 10:05:52 +00:00
|
|
|
|
2016-04-03 14:51:14 +00:00
|
|
|
QUnit.test( "cancel search", function( assert ) {
|
|
|
|
var ready = assert.async();
|
|
|
|
assert.expect( 6 );
|
2011-06-13 18:09:44 +00:00
|
|
|
var first = true,
|
2015-08-21 04:11:54 +00:00
|
|
|
element = $( "#autocomplete" ).autocomplete( {
|
2011-06-13 18:09:44 +00:00
|
|
|
delay: 0,
|
|
|
|
source: data,
|
|
|
|
search: function() {
|
|
|
|
if ( first ) {
|
2016-04-03 14:51:14 +00:00
|
|
|
assert.equal( element.val(), "ja", "val on first search" );
|
2011-06-13 18:09:44 +00:00
|
|
|
first = false;
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-03 14:51:14 +00:00
|
|
|
assert.equal( element.val(), "java", "val on second search" );
|
2011-06-13 18:09:44 +00:00
|
|
|
},
|
|
|
|
open: function() {
|
2016-04-03 14:51:14 +00:00
|
|
|
assert.ok( true, "menu opened" );
|
2010-01-20 14:00:14 +00:00
|
|
|
}
|
2015-08-21 04:11:54 +00:00
|
|
|
} ),
|
2011-06-13 18:09:44 +00:00
|
|
|
menu = element.autocomplete( "widget" );
|
2015-05-14 01:54:08 +00:00
|
|
|
element.val( "ja" ).trigger( "keydown" );
|
2015-08-21 04:11:54 +00:00
|
|
|
setTimeout( function() {
|
2016-04-03 14:51:14 +00:00
|
|
|
assert.ok( menu.is( ":hidden" ), "menu is hidden after first search" );
|
2015-05-14 01:54:08 +00:00
|
|
|
element.val( "java" ).trigger( "keydown" );
|
2015-08-21 04:11:54 +00:00
|
|
|
setTimeout( function() {
|
2016-04-03 14:51:14 +00:00
|
|
|
assert.ok( menu.is( ":visible" ), "menu is visible after second search" );
|
|
|
|
assert.equal( menu.find( ".ui-menu-item" ).length, 2, "# of menu items" );
|
|
|
|
ready();
|
2015-08-21 04:11:54 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
} );
|
2010-01-20 14:00:14 +00:00
|
|
|
|
2016-04-03 14:51:14 +00:00
|
|
|
QUnit.test( "cancel focus", function( assert ) {
|
|
|
|
var ready = assert.async();
|
|
|
|
assert.expect( 1 );
|
2012-04-19 14:50:09 +00:00
|
|
|
var customVal = "custom value",
|
2015-08-21 04:11:54 +00:00
|
|
|
element = $( "#autocomplete" ).autocomplete( {
|
2011-06-13 18:09:44 +00:00
|
|
|
delay: 0,
|
|
|
|
source: data,
|
2012-10-23 14:36:42 +00:00
|
|
|
focus: function() {
|
2011-06-13 18:09:44 +00:00
|
|
|
$( this ).val( customVal );
|
|
|
|
return false;
|
|
|
|
}
|
2015-08-21 04:11:54 +00:00
|
|
|
} );
|
2015-05-14 01:54:08 +00:00
|
|
|
element.val( "ja" ).trigger( "keydown" );
|
2015-08-21 04:11:54 +00:00
|
|
|
setTimeout( function() {
|
2011-06-13 18:09:44 +00:00
|
|
|
element.simulate( "keydown", { keyCode: $.ui.keyCode.DOWN } );
|
2016-04-03 14:51:14 +00:00
|
|
|
assert.equal( element.val(), customVal );
|
|
|
|
ready();
|
2015-08-21 04:11:54 +00:00
|
|
|
} );
|
|
|
|
} );
|
2010-01-20 14:00:14 +00:00
|
|
|
|
2016-04-03 14:51:14 +00:00
|
|
|
QUnit.test( "cancel select", function( assert ) {
|
|
|
|
var ready = assert.async();
|
|
|
|
assert.expect( 1 );
|
2011-06-13 18:09:44 +00:00
|
|
|
var customVal = "custom value",
|
2015-08-21 04:11:54 +00:00
|
|
|
element = $( "#autocomplete" ).autocomplete( {
|
2011-06-13 18:09:44 +00:00
|
|
|
delay: 0,
|
|
|
|
source: data,
|
2012-10-23 14:36:42 +00:00
|
|
|
select: function() {
|
2011-06-13 18:09:44 +00:00
|
|
|
$( this ).val( customVal );
|
|
|
|
return false;
|
|
|
|
}
|
2015-08-21 04:11:54 +00:00
|
|
|
} );
|
2015-05-14 01:54:08 +00:00
|
|
|
element.val( "ja" ).trigger( "keydown" );
|
2015-08-21 04:11:54 +00:00
|
|
|
setTimeout( function() {
|
2011-06-13 18:09:44 +00:00
|
|
|
element.simulate( "keydown", { keyCode: $.ui.keyCode.DOWN } );
|
|
|
|
element.simulate( "keydown", { keyCode: $.ui.keyCode.ENTER } );
|
2016-04-03 14:51:14 +00:00
|
|
|
assert.equal( element.val(), customVal );
|
|
|
|
ready();
|
2015-08-21 04:11:54 +00:00
|
|
|
} );
|
|
|
|
} );
|
2010-01-20 14:00:14 +00:00
|
|
|
|
2016-04-03 14:51:14 +00:00
|
|
|
QUnit.test( "blur during remote search", function( assert ) {
|
|
|
|
var ready = assert.async();
|
|
|
|
assert.expect( 1 );
|
2015-08-21 04:11:54 +00:00
|
|
|
var ac = $( "#autocomplete" ).autocomplete( {
|
2011-11-23 18:39:45 +00:00
|
|
|
delay: 0,
|
|
|
|
source: function( request, response ) {
|
2016-04-03 14:51:14 +00:00
|
|
|
assert.ok( true, "trigger request" );
|
2011-11-23 18:39:45 +00:00
|
|
|
ac.simulate( "blur" );
|
2015-08-21 04:11:54 +00:00
|
|
|
setTimeout( function() {
|
|
|
|
response( [ "result" ] );
|
2016-04-03 14:51:14 +00:00
|
|
|
ready();
|
2015-08-21 04:11:54 +00:00
|
|
|
}, 25 );
|
2011-11-23 18:39:45 +00:00
|
|
|
},
|
|
|
|
open: function() {
|
2016-04-03 14:51:14 +00:00
|
|
|
assert.ok( false, "opened after a blur" );
|
2011-11-23 18:39:45 +00:00
|
|
|
}
|
2015-08-21 04:11:54 +00:00
|
|
|
} );
|
2015-05-14 01:54:08 +00:00
|
|
|
ac.val( "ro" ).trigger( "keydown" );
|
2015-08-21 04:11:54 +00:00
|
|
|
} );
|
2011-11-23 18:39:45 +00:00
|
|
|
|
2015-04-03 19:57:57 +00:00
|
|
|
} );
|