From dd99e2d0488c503d929909e431a544e472782178 Mon Sep 17 00:00:00 2001 From: Jacob Thornton Date: Fri, 6 Jan 2012 18:30:32 -0800 Subject: start of autocomplete plugin --- js/tests/unit/bootstrap-typeahead.js | 122 +++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 js/tests/unit/bootstrap-typeahead.js (limited to 'js/tests/unit/bootstrap-typeahead.js') 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.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 = $('') + ok($input.typeahead().data('typeahead').$menu, 'has a menu') + }) + + test("should listen to the menu", function () { + var $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 = $('').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 = $('').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 = $('').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 = $('').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 -- cgit v1.2.3 From 4478df768168fe41599508688046612bf5f1e526 Mon Sep 17 00:00:00 2001 From: Jacob Thornton Date: Sun, 8 Jan 2012 00:49:38 -0800 Subject: first pass at ultra basic autocomplete --- js/tests/unit/bootstrap-typeahead.js | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) (limited to 'js/tests/unit/bootstrap-typeahead.js') diff --git a/js/tests/unit/bootstrap-typeahead.js b/js/tests/unit/bootstrap-typeahead.js index dc46a7990..10b2a3f17 100644 --- a/js/tests/unit/bootstrap-typeahead.js +++ b/js/tests/unit/bootstrap-typeahead.js @@ -13,11 +13,9 @@ $(function () { test("should listen to an input", function () { var $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 { @@ -44,7 +42,8 @@ $(function () { }) , typeahead = $input.data('typeahead') - $input.val('a').change() + $input.val('a') + typeahead.lookup() ok(typeahead.$menu.is(":visible"), 'typeahead is visible') equals(typeahead.$menu.find('li').length, 3, 'has 3 items in menu') @@ -54,12 +53,14 @@ $(function () { }) test("should hide menu when query entered", function () { + stop() var $input = $('').typeahead({ data: ['aa', 'ab', 'ac'] }) , typeahead = $input.data('typeahead') - $input.val('a').change() + $input.val('a') + typeahead.lookup() ok(typeahead.$menu.is(":visible"), 'typeahead is visible') equals(typeahead.$menu.find('li').length, 3, 'has 3 items in menu') @@ -67,7 +68,10 @@ $(function () { $input.blur() - ok(!typeahead.$menu.is(":visible"), "typeahead is no longer visible") + setTimeout(function () { + ok(!typeahead.$menu.is(":visible"), "typeahead is no longer visible") + start() + }, 200) typeahead.$menu.remove() }) @@ -78,7 +82,8 @@ $(function () { }) , typeahead = $input.data('typeahead') - $input.val('a').change() + $input.val('a') + typeahead.lookup() ok(typeahead.$menu.is(":visible"), 'typeahead is visible') equals(typeahead.$menu.find('li').length, 3, 'has 3 items in menu') @@ -110,9 +115,10 @@ $(function () { }) , typeahead = $input.data('typeahead') - $input.val('a').change() + $input.val('a') + typeahead.lookup() - $(typeahead.$menu.find('li')[2]).trigger('click') + $(typeahead.$menu.find('li')[2]).mouseover().click() equals($input.val(), 'ac', 'input value was correctly set') ok(!typeahead.$menu.is(':visible'), 'the menu was hidden') -- cgit v1.2.3 From 3451dcbc2816e86fc649d30bd9d6813ef5466b58 Mon Sep 17 00:00:00 2001 From: Jacob Thornton Date: Sat, 28 Jan 2012 00:03:26 -0800 Subject: fix failing typeahead tests --- js/tests/unit/bootstrap-typeahead.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'js/tests/unit/bootstrap-typeahead.js') diff --git a/js/tests/unit/bootstrap-typeahead.js b/js/tests/unit/bootstrap-typeahead.js index 10b2a3f17..455ed415b 100644 --- a/js/tests/unit/bootstrap-typeahead.js +++ b/js/tests/unit/bootstrap-typeahead.js @@ -38,7 +38,7 @@ $(function () { test("should show menu when query entered", function () { var $input = $('').typeahead({ - data: ['aa', 'ab', 'ac'] + source: ['aa', 'ab', 'ac'] }) , typeahead = $input.data('typeahead') @@ -55,7 +55,7 @@ $(function () { test("should hide menu when query entered", function () { stop() var $input = $('').typeahead({ - data: ['aa', 'ab', 'ac'] + source: ['aa', 'ab', 'ac'] }) , typeahead = $input.data('typeahead') @@ -78,7 +78,7 @@ $(function () { test("should set next item when down arrow is pressed", function () { var $input = $('').typeahead({ - data: ['aa', 'ab', 'ac'] + source: ['aa', 'ab', 'ac'] }) , typeahead = $input.data('typeahead') @@ -111,7 +111,7 @@ $(function () { test("should set input value to selected item", function () { var $input = $('').typeahead({ - data: ['aa', 'ab', 'ac'] + source: ['aa', 'ab', 'ac'] }) , typeahead = $input.data('typeahead') -- cgit v1.2.3