aboutsummaryrefslogtreecommitdiff
path: root/js/tests
diff options
context:
space:
mode:
authorJacob Thornton <[email protected]>2012-01-06 18:30:32 -0800
committerJacob Thornton <[email protected]>2012-01-06 18:30:32 -0800
commitdd99e2d0488c503d929909e431a544e472782178 (patch)
tree986cb3242687d60d7f6b833ddb5432610463511a /js/tests
parent64c8623533752ab155e452335457285fa06c1b70 (diff)
downloadbootstrap-dd99e2d0488c503d929909e431a544e472782178.tar.xz
bootstrap-dd99e2d0488c503d929909e431a544e472782178.zip
start of autocomplete plugin
Diffstat (limited to 'js/tests')
-rw-r--r--js/tests/index.html2
-rw-r--r--js/tests/unit/bootstrap-typeahead.js122
2 files changed, 124 insertions, 0 deletions
diff --git a/js/tests/index.html b/js/tests/index.html
index e8ed2c5fe..27c2b3412 100644
--- a/js/tests/index.html
+++ b/js/tests/index.html
@@ -22,6 +22,7 @@
<script src="../../js/bootstrap-tab.js"></script>
<script src="../../js/bootstrap-twipsy.js"></script>
<script src="../../js/bootstrap-popover.js"></script>
+ <script src="../../js/bootstrap-typeahead.js"></script>
<!-- unit tests -->
<script src="unit/bootstrap-transition.js"></script>
@@ -34,6 +35,7 @@
<script src="unit/bootstrap-tab.js"></script>
<script src="unit/bootstrap-twipsy.js"></script>
<script src="unit/bootstrap-popover.js"></script>
+ <script src="unit/bootstrap-typeahead.js"></script>
<body>
<div>
diff --git a/js/tests/unit/bootstrap-typeahead.js b/js/tests/unit/bootstrap-typeahead.js
new file mode 100644
index 000000000..dc46a7990
--- /dev/null
+++ b/js/tests/unit/bootstrap-typeahead.js
@@ -0,0 +1,122 @@
+$(function () {
+
+ module("bootstrap-typeahead")
+
+ test("should be defined on jquery object", function () {
+ ok($(document.body).typeahead, 'alert method is defined')
+ })
+
+ test("should return element", function () {
+ ok($(document.body).typeahead()[0] == document.body, 'document.body returned')
+ })
+
+ test("should listen to an input", function () {
+ var $input = $('<input />')
+ $input.typeahead()
+ ok($input.data('events').focus, 'has a focus event')
+ ok($input.data('events').blur, 'has a blur event')
+ ok($input.data('events').keypress, 'has a keypress event')
+ ok($input.data('events').keyup, 'has a keyup event')
+ ok($input.data('events').change, 'has a change event')
+ if ($.browser.webkit || $.browser.msie) {
+ ok($input.data('events').keydown, 'has a keydown event')
+ } else {
+ ok($input.data('events').keydown, 'does not have a keydown event')
+ }
+ })
+
+ test("should create a menu", function () {
+ var $input = $('<input />')
+ ok($input.typeahead().data('typeahead').$menu, 'has a menu')
+ })
+
+ test("should listen to the menu", function () {
+ var $input = $('<input />')
+ , $menu = $input.typeahead().data('typeahead').$menu
+
+ ok($menu.data('events').mouseover, 'has a mouseover(pseudo: mouseenter)')
+ ok($menu.data('events').click, 'has a click')
+ })
+
+ test("should show menu when query entered", function () {
+ var $input = $('<input />').typeahead({
+ data: ['aa', 'ab', 'ac']
+ })
+ , typeahead = $input.data('typeahead')
+
+ $input.val('a').change()
+
+ ok(typeahead.$menu.is(":visible"), 'typeahead is visible')
+ equals(typeahead.$menu.find('li').length, 3, 'has 3 items in menu')
+ equals(typeahead.$menu.find('.active').length, 1, 'one item is active')
+
+ typeahead.$menu.remove()
+ })
+
+ test("should hide menu when query entered", function () {
+ var $input = $('<input />').typeahead({
+ data: ['aa', 'ab', 'ac']
+ })
+ , typeahead = $input.data('typeahead')
+
+ $input.val('a').change()
+
+ ok(typeahead.$menu.is(":visible"), 'typeahead is visible')
+ equals(typeahead.$menu.find('li').length, 3, 'has 3 items in menu')
+ equals(typeahead.$menu.find('.active').length, 1, 'one item is active')
+
+ $input.blur()
+
+ ok(!typeahead.$menu.is(":visible"), "typeahead is no longer visible")
+
+ typeahead.$menu.remove()
+ })
+
+ test("should set next item when down arrow is pressed", function () {
+ var $input = $('<input />').typeahead({
+ data: ['aa', 'ab', 'ac']
+ })
+ , typeahead = $input.data('typeahead')
+
+ $input.val('a').change()
+
+ ok(typeahead.$menu.is(":visible"), 'typeahead is visible')
+ equals(typeahead.$menu.find('li').length, 3, 'has 3 items in menu')
+ equals(typeahead.$menu.find('.active').length, 1, 'one item is active')
+ ok(typeahead.$menu.find('li').first().hasClass('active'), "first item is active")
+
+ $input.trigger({
+ type: 'keypress'
+ , keyCode: 40
+ })
+
+ ok(typeahead.$menu.find('li').first().next().hasClass('active'), "second item is active")
+
+
+ $input.trigger({
+ type: 'keypress'
+ , keyCode: 38
+ })
+
+ ok(typeahead.$menu.find('li').first().hasClass('active'), "first item is active")
+
+ typeahead.$menu.remove()
+ })
+
+
+ test("should set input value to selected item", function () {
+ var $input = $('<input />').typeahead({
+ data: ['aa', 'ab', 'ac']
+ })
+ , typeahead = $input.data('typeahead')
+
+ $input.val('a').change()
+
+ $(typeahead.$menu.find('li')[2]).trigger('click')
+
+ equals($input.val(), 'ac', 'input value was correctly set')
+ ok(!typeahead.$menu.is(':visible'), 'the menu was hidden')
+
+ typeahead.$menu.remove()
+ })
+}) \ No newline at end of file