3 module("bootstrap-typeahead")
5 test("should provide no conflict", function () {
6 var typeahead = $.fn.typeahead.noConflict()
7 ok(!$.fn.typeahead, 'typeahead was set back to undefined (org value)')
8 $.fn.typeahead = typeahead
11 test("should be defined on jquery object", function () {
12 ok($(document.body).typeahead, 'alert method is defined')
15 test("should return element", function () {
16 ok($(document.body).typeahead()[0] == document.body, 'document.body returned')
19 test("should listen to an input", function () {
20 var $input = $('<input />')
22 ok($._data($input[0], 'events').blur, 'has a blur event')
23 ok($._data($input[0], 'events').keypress, 'has a keypress event')
24 ok($._data($input[0], 'events').keyup, 'has a keyup event')
27 test("should create a menu", function () {
28 var $input = $('<input />')
29 ok($input.typeahead().data('typeahead').$menu, 'has a menu')
32 test("should listen to the menu", function () {
33 var $input = $('<input />')
34 , $menu = $input.typeahead().data('typeahead').$menu
36 ok($._data($menu[0], 'events').mouseover, 'has a mouseover(pseudo: mouseenter)')
37 ok($._data($menu[0], 'events').click, 'has a click')
40 test("should show menu when query entered", function () {
41 var $input = $('<input />')
44 source: ['aa', 'ab', 'ac']
46 , typeahead = $input.data('typeahead')
51 ok(typeahead.$menu.is(":visible"), 'typeahead is visible')
52 equals(typeahead.$menu.find('li').length, 3, 'has 3 items in menu')
53 equals(typeahead.$menu.find('.active').length, 1, 'one item is active')
56 typeahead.$menu.remove()
59 test("should accept data source via synchronous function", function () {
60 var $input = $('<input />').typeahead({
62 return ['aa', 'ab', 'ac']
65 , typeahead = $input.data('typeahead')
70 ok(typeahead.$menu.is(":visible"), 'typeahead is visible')
71 equals(typeahead.$menu.find('li').length, 3, 'has 3 items in menu')
72 equals(typeahead.$menu.find('.active').length, 1, 'one item is active')
75 typeahead.$menu.remove()
78 test("should accept data source via asynchronous function", function () {
79 var $input = $('<input />').typeahead({
80 source: function (query, process) {
81 process(['aa', 'ab', 'ac'])
84 , typeahead = $input.data('typeahead')
89 ok(typeahead.$menu.is(":visible"), 'typeahead is visible')
90 equals(typeahead.$menu.find('li').length, 3, 'has 3 items in menu')
91 equals(typeahead.$menu.find('.active').length, 1, 'one item is active')
94 typeahead.$menu.remove()
97 test("should not explode when regex chars are entered", function () {
98 var $input = $('<input />').typeahead({
99 source: ['aa', 'ab', 'ac', 'mdo*', 'fat+']
101 , typeahead = $input.data('typeahead')
106 ok(typeahead.$menu.is(":visible"), 'typeahead is visible')
107 equals(typeahead.$menu.find('li').length, 1, 'has 1 item in menu')
108 equals(typeahead.$menu.find('.active').length, 1, 'one item is active')
111 typeahead.$menu.remove()
114 test("should hide menu when query entered", function () {
116 var $input = $('<input />').typeahead({
117 source: ['aa', 'ab', 'ac']
119 , typeahead = $input.data('typeahead')
124 ok(typeahead.$menu.is(":visible"), 'typeahead is visible')
125 equals(typeahead.$menu.find('li').length, 3, 'has 3 items in menu')
126 equals(typeahead.$menu.find('.active').length, 1, 'one item is active')
130 setTimeout(function () {
131 ok(!typeahead.$menu.is(":visible"), "typeahead is no longer visible")
136 typeahead.$menu.remove()
139 test("should set next item when down arrow is pressed", function () {
140 var $input = $('<input />').typeahead({
141 source: ['aa', 'ab', 'ac']
143 , typeahead = $input.data('typeahead')
148 ok(typeahead.$menu.is(":visible"), 'typeahead is visible')
149 equals(typeahead.$menu.find('li').length, 3, 'has 3 items in menu')
150 equals(typeahead.$menu.find('.active').length, 1, 'one item is active')
151 ok(typeahead.$menu.find('li').first().hasClass('active'), "first item is active")
153 // simulate entire key pressing event
167 ok(typeahead.$menu.find('li').first().next().hasClass('active'), "second item is active")
182 ok(typeahead.$menu.find('li').first().hasClass('active'), "first item is active")
185 typeahead.$menu.remove()
189 test("should set input value to selected item", function () {
190 var $input = $('<input />').typeahead({
191 source: ['aa', 'ab', 'ac']
193 , typeahead = $input.data('typeahead')
201 $input.change(function() { changed = true });
202 $input.focus(function() { focus = true; blur = false });
203 $input.blur(function() { blur = true; focus = false });
205 $(typeahead.$menu.find('li')[2]).mouseover().click()
207 equals($input.val(), 'ac', 'input value was correctly set')
208 ok(!typeahead.$menu.is(':visible'), 'the menu was hidden')
209 ok(changed, 'a change event was fired')
210 ok(focus && !blur, 'focus is still set')
213 typeahead.$menu.remove()
216 test("should start querying when minLength is met", function () {
217 var $input = $('<input />').typeahead({
218 source: ['aaaa', 'aaab', 'aaac'],
221 , typeahead = $input.data('typeahead')
226 equals(typeahead.$menu.find('li').length, 0, 'has 0 items in menu')
231 equals(typeahead.$menu.find('li').length, 3, 'has 3 items in menu')
234 typeahead.$menu.remove()