aboutsummaryrefslogtreecommitdiffstats
path: root/tests/unit/autocomplete
diff options
context:
space:
mode:
authorScott González <scott.gonzalez@gmail.com>2010-01-14 17:23:11 +0000
committerScott González <scott.gonzalez@gmail.com>2010-01-14 17:23:11 +0000
commit6024fdf2564b274af2f2d306f420e44a2c88d7b1 (patch)
treee2bace3eacb81bd9732226fc19dfd5089d19fe3f /tests/unit/autocomplete
parent7f5fe2f2f14406a0c028e92ab6203ae0b72a1e96 (diff)
downloadjquery-ui-6024fdf2564b274af2f2d306f420e44a2c88d7b1.tar.gz
jquery-ui-6024fdf2564b274af2f2d306f420e44a2c88d7b1.zip
Copied autocomplete from dev branch.
Diffstat (limited to 'tests/unit/autocomplete')
-rw-r--r--tests/unit/autocomplete/autocomplete.html35
-rw-r--r--tests/unit/autocomplete/autocomplete_core.js39
-rw-r--r--tests/unit/autocomplete/autocomplete_defaults.js12
-rw-r--r--tests/unit/autocomplete/autocomplete_events.js120
-rw-r--r--tests/unit/autocomplete/autocomplete_methods.js35
-rw-r--r--tests/unit/autocomplete/autocomplete_options.js174
-rw-r--r--tests/unit/autocomplete/autocomplete_tickets.js10
-rw-r--r--tests/unit/autocomplete/remote_object_array_labels.txt1
-rw-r--r--tests/unit/autocomplete/remote_object_array_values.txt1
-rw-r--r--tests/unit/autocomplete/remote_string_array.txt1
10 files changed, 428 insertions, 0 deletions
diff --git a/tests/unit/autocomplete/autocomplete.html b/tests/unit/autocomplete/autocomplete.html
new file mode 100644
index 000000000..cf03d8770
--- /dev/null
+++ b/tests/unit/autocomplete/autocomplete.html
@@ -0,0 +1,35 @@
+<!doctype html>
+<html lang="en">
+<head>
+ <title>jQuery UI Autocomplete Test Suite</title>
+
+ <script type="text/javascript" src="../../../jquery-1.3.2.js"></script>
+ <script type="text/javascript" src="../../../ui/jquery.ui.core.js"></script>
+ <script type="text/javascript" src="../../../ui/jquery.ui.widget.js"></script>
+ <script type="text/javascript" src="../../../ui/jquery.ui.position.js"></script>
+ <script type="text/javascript" src="../../../ui/jquery.ui.menu.js"></script>
+ <script type="text/javascript" src="../../../ui/jquery.ui.autocomplete.js"></script>
+
+ <link type="text/css" href="../testsuite.css" rel="stylesheet" />
+ <script type="text/javascript" src="../../../external/testrunner-r6588.js"></script>
+ <script type="text/javascript" src="../../jquery.simulate.js"></script>
+ <script type="text/javascript" src="../testsuite.js"></script>
+
+ <script type="text/javascript" src="autocomplete_core.js"></script>
+ <script type="text/javascript" src="autocomplete_defaults.js"></script>
+ <script type="text/javascript" src="autocomplete_events.js"></script>
+ <script type="text/javascript" src="autocomplete_methods.js"></script>
+ <script type="text/javascript" src="autocomplete_options.js"></script>
+ <script type="text/javascript" src="autocomplete_tickets.js"></script>
+
+</head>
+<body>
+
+<div id="main">
+
+ <div><input id="autocomplete" class="foo" /></div>
+
+</div>
+
+</body>
+</html>
diff --git a/tests/unit/autocomplete/autocomplete_core.js b/tests/unit/autocomplete/autocomplete_core.js
new file mode 100644
index 000000000..6c5cccfe4
--- /dev/null
+++ b/tests/unit/autocomplete/autocomplete_core.js
@@ -0,0 +1,39 @@
+/*
+ * autocomplete_core.js
+ */
+
+
+(function($) {
+
+module("autocomplete: core");
+
+test("close-on-blur is properly delayed", function() {
+ var ac = $("#autocomplete").autocomplete({
+ source: ["java", "javascript"]
+ }).val("ja").autocomplete("search");
+ same( $(".ui-menu:visible").length, 1 );
+ ac.blur();
+ same( $(".ui-menu:visible").length, 1 );
+ stop();
+ setTimeout(function() {
+ same( $(".ui-menu:visible").length, 0 );
+ start();
+ }, 200);
+})
+
+test("close-on-blur is cancelled when starting a search", function() {
+ var ac = $("#autocomplete").autocomplete({
+ source: ["java", "javascript"]
+ }).val("ja").autocomplete("search");
+ same( $(".ui-menu:visible").length, 1 );
+ ac.blur();
+ same( $(".ui-menu:visible").length, 1 );
+ ac.autocomplete("search");
+ stop();
+ setTimeout(function() {
+ same( $(".ui-menu:visible").length, 1 );
+ start();
+ }, 200);
+})
+
+})(jQuery);
diff --git a/tests/unit/autocomplete/autocomplete_defaults.js b/tests/unit/autocomplete/autocomplete_defaults.js
new file mode 100644
index 000000000..dc941b9e4
--- /dev/null
+++ b/tests/unit/autocomplete/autocomplete_defaults.js
@@ -0,0 +1,12 @@
+/*
+ * autocomplete_defaults.js
+ */
+
+var autocomplete_defaults = {
+ delay: 300,
+ disabled: false,
+ minLength: 1,
+ source: undefined
+};
+
+commonWidgetTests('autocomplete', { defaults: autocomplete_defaults });
diff --git a/tests/unit/autocomplete/autocomplete_events.js b/tests/unit/autocomplete/autocomplete_events.js
new file mode 100644
index 000000000..09dd2ef11
--- /dev/null
+++ b/tests/unit/autocomplete/autocomplete_events.js
@@ -0,0 +1,120 @@
+/*
+ * autocomplete_events.js
+ */
+(function($) {
+
+module("autocomplete: events");
+
+var data = ["c++", "java", "php", "coldfusion", "javascript", "asp", "ruby", "python", "c", "scala", "groovy", "haskell", "pearl"];
+
+test("all events", function() {
+ expect(11);
+ 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").length, 1 );
+ },
+ select: function(event, ui) {
+ same(event.type, "autocompleteselect");
+ same(ui.item, {label:"java", value:"java"});
+ },
+ change: function(event) {
+ same(event.type, "autocompletechange");
+ same( $(".ui-menu").length, 0 );
+ }
+ });
+ stop();
+ ac.val("ja").keydown();
+ setTimeout(function() {
+ same( $(".ui-menu").length, 1 );
+ ac.simulate("keydown", { keyCode: $.ui.keyCode.DOWN });
+ ac.simulate("keydown", { keyCode: $.ui.keyCode.ENTER });
+ start();
+ }, 50);
+});
+
+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").length, 0 );
+ ac.val("java").keydown();
+ setTimeout(function() {
+ same( $(".ui-menu").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);
diff --git a/tests/unit/autocomplete/autocomplete_methods.js b/tests/unit/autocomplete/autocomplete_methods.js
new file mode 100644
index 000000000..61cf2a33c
--- /dev/null
+++ b/tests/unit/autocomplete/autocomplete_methods.js
@@ -0,0 +1,35 @@
+/*
+ * autocomplete_methods.js
+ */
+(function($) {
+
+
+module("autocomplete: methods");
+
+test("destroy", function() {
+ var beforeHtml = $("#autocomplete").parent().html();
+ var afterHtml = $("#autocomplete").autocomplete().autocomplete("destroy").parent().html();
+ same( beforeHtml, afterHtml );
+})
+
+var data = ["c++", "java", "php", "coldfusion", "javascript", "asp", "ruby", "python", "c", "scala", "groovy", "haskell", "pearl"];
+
+test("search", function() {
+ var ac = $("#autocomplete").autocomplete({
+ source: data,
+ minLength: 0
+ });
+ ac.autocomplete("search");
+ same( $(".ui-menu .ui-menu-item").length, data.length, "all items for a blank search" );
+
+ ac.val("has");
+ ac.autocomplete("search")
+ same( $(".ui-menu .ui-menu-item").text(), "haskell", "only one item for set input value" );
+
+ ac.autocomplete("search", "ja");
+ same( $(".ui-menu .ui-menu-item").length, 2, "only java and javascript for 'ja'" );
+
+ $("#autocomplete").autocomplete("destroy");
+})
+
+})(jQuery);
diff --git a/tests/unit/autocomplete/autocomplete_options.js b/tests/unit/autocomplete/autocomplete_options.js
new file mode 100644
index 000000000..1b6857b22
--- /dev/null
+++ b/tests/unit/autocomplete/autocomplete_options.js
@@ -0,0 +1,174 @@
+/*
+ * autocomplete_options.js
+ */
+(function($) {
+
+module("autocomplete: options");
+
+
+/* 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", "pearl"];
+
+test("delay", function() {
+ var ac = $("#autocomplete").autocomplete({
+ source: data,
+ delay: 50
+ });
+ ac.val("ja").keydown();
+
+ same( $(".ui-menu").length, 0 );
+
+ // wait half a second for the default delay to open the menu
+ stop();
+ setTimeout(function() {
+ same( $(".ui-menu").length, 1 );
+ ac.autocomplete("destroy");
+ start();
+ }, 100);
+});
+
+test("minLength", function() {
+ var ac = $("#autocomplete").autocomplete({
+ source: data
+ });
+ ac.autocomplete("search", "");
+ same( $(".ui-menu").length, 0, "blank not enough for minLength: 1" );
+
+ ac.autocomplete("option", "minLength", 0);
+ ac.autocomplete("search", "");
+ same( $(".ui-menu").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);
diff --git a/tests/unit/autocomplete/autocomplete_tickets.js b/tests/unit/autocomplete/autocomplete_tickets.js
new file mode 100644
index 000000000..78f57418d
--- /dev/null
+++ b/tests/unit/autocomplete/autocomplete_tickets.js
@@ -0,0 +1,10 @@
+/*
+ * autocomplete_tickets.js
+ */
+(function($) {
+
+module("autocomplete: tickets");
+
+
+
+})(jQuery);
diff --git a/tests/unit/autocomplete/remote_object_array_labels.txt b/tests/unit/autocomplete/remote_object_array_labels.txt
new file mode 100644
index 000000000..502496c25
--- /dev/null
+++ b/tests/unit/autocomplete/remote_object_array_labels.txt
@@ -0,0 +1 @@
+[{"label":"java"},{"label":"javascript"}] \ No newline at end of file
diff --git a/tests/unit/autocomplete/remote_object_array_values.txt b/tests/unit/autocomplete/remote_object_array_values.txt
new file mode 100644
index 000000000..029cbb9df
--- /dev/null
+++ b/tests/unit/autocomplete/remote_object_array_values.txt
@@ -0,0 +1 @@
+[{"value":"java"},{"value":"javascript"}] \ No newline at end of file
diff --git a/tests/unit/autocomplete/remote_string_array.txt b/tests/unit/autocomplete/remote_string_array.txt
new file mode 100644
index 000000000..3b24c8e4b
--- /dev/null
+++ b/tests/unit/autocomplete/remote_string_array.txt
@@ -0,0 +1 @@
+["java", "javascript"] \ No newline at end of file