Skip to content

Commit

Permalink
get tests passing for typeahead :P
Browse files Browse the repository at this point in the history
  • Loading branch information
fat committed Dec 8, 2012
1 parent 1747caf commit 820a3b2
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 28 deletions.
14 changes: 8 additions & 6 deletions docs/assets/js/bootstrap-typeahead.js
Expand Up @@ -33,8 +33,8 @@
this.sorter = this.options.sorter || this.sorter
this.highlighter = this.options.highlighter || this.highlighter
this.updater = this.options.updater || this.updater
this.$menu = $(this.options.menu).insertAfter(this.$element)
this.source = this.options.source
this.$menu = $(this.options.menu)
this.shown = false
this.listen()
}
Expand All @@ -60,12 +60,14 @@
height: this.$element[0].offsetHeight
})

this.$menu.css({
top: pos.top + pos.height
, left: pos.left
})
this.$menu
.insertAfter(this.$element)
.css({
top: pos.top + pos.height
, left: pos.left
})
.show()

this.$menu.show()
this.shown = true
return this
}
Expand Down
14 changes: 8 additions & 6 deletions docs/assets/js/bootstrap.js
Expand Up @@ -1752,8 +1752,8 @@
this.sorter = this.options.sorter || this.sorter
this.highlighter = this.options.highlighter || this.highlighter
this.updater = this.options.updater || this.updater
this.$menu = $(this.options.menu).insertAfter(this.$element)
this.source = this.options.source
this.$menu = $(this.options.menu)
this.shown = false
this.listen()
}
Expand All @@ -1779,12 +1779,14 @@
height: this.$element[0].offsetHeight
})

this.$menu.css({
top: pos.top + pos.height
, left: pos.left
})
this.$menu
.insertAfter(this.$element)
.css({
top: pos.top + pos.height
, left: pos.left
})
.show()

this.$menu.show()
this.shown = true
return this
}
Expand Down
2 changes: 1 addition & 1 deletion docs/assets/js/bootstrap.min.js

Large diffs are not rendered by default.

14 changes: 8 additions & 6 deletions js/bootstrap-typeahead.js
Expand Up @@ -33,8 +33,8 @@
this.sorter = this.options.sorter || this.sorter
this.highlighter = this.options.highlighter || this.highlighter
this.updater = this.options.updater || this.updater
this.$menu = $(this.options.menu).insertAfter(this.$element)
this.source = this.options.source
this.$menu = $(this.options.menu)
this.shown = false
this.listen()
}
Expand All @@ -60,12 +60,14 @@
height: this.$element[0].offsetHeight
})

this.$menu.css({
top: pos.top + pos.height
, left: pos.left
})
this.$menu
.insertAfter(this.$element)
.css({
top: pos.top + pos.height
, left: pos.left
})
.show()

this.$menu.show()
this.shown = true
return this
}
Expand Down
27 changes: 18 additions & 9 deletions js/tests/unit/bootstrap-typeahead.js
Expand Up @@ -38,7 +38,9 @@ $(function () {
})

test("should show menu when query entered", function () {
var $input = $('<input />').typeahead({
var $input = $('<input />')
.appendTo('body')
.typeahead({
source: ['aa', 'ab', 'ac']
})
, typeahead = $input.data('typeahead')
Expand All @@ -50,6 +52,7 @@ $(function () {
equals(typeahead.$menu.find('li').length, 3, 'has 3 items in menu')
equals(typeahead.$menu.find('.active').length, 1, 'one item is active')

$input.remove()
typeahead.$menu.remove()
})

Expand All @@ -58,7 +61,7 @@ $(function () {
source: function () {
return ['aa', 'ab', 'ac']
}
})
}).appendTo('body')
, typeahead = $input.data('typeahead')

$input.val('a')
Expand All @@ -68,6 +71,7 @@ $(function () {
equals(typeahead.$menu.find('li').length, 3, 'has 3 items in menu')
equals(typeahead.$menu.find('.active').length, 1, 'one item is active')

$input.remove()
typeahead.$menu.remove()
})

Expand All @@ -76,7 +80,7 @@ $(function () {
source: function (query, process) {
process(['aa', 'ab', 'ac'])
}
})
}).appendTo('body')
, typeahead = $input.data('typeahead')

$input.val('a')
Expand All @@ -86,13 +90,14 @@ $(function () {
equals(typeahead.$menu.find('li').length, 3, 'has 3 items in menu')
equals(typeahead.$menu.find('.active').length, 1, 'one item is active')

$input.remove()
typeahead.$menu.remove()
})

test("should not explode when regex chars are entered", function () {
var $input = $('<input />').typeahead({
source: ['aa', 'ab', 'ac', 'mdo*', 'fat+']
})
}).appendTo('body')
, typeahead = $input.data('typeahead')

$input.val('+')
Expand All @@ -102,14 +107,15 @@ $(function () {
equals(typeahead.$menu.find('li').length, 1, 'has 1 item in menu')
equals(typeahead.$menu.find('.active').length, 1, 'one item is active')

$input.remove()
typeahead.$menu.remove()
})

test("should hide menu when query entered", function () {
stop()
var $input = $('<input />').typeahead({
source: ['aa', 'ab', 'ac']
})
}).appendTo('body')
, typeahead = $input.data('typeahead')

$input.val('a')
Expand All @@ -126,13 +132,14 @@ $(function () {
start()
}, 200)

$input.remove()
typeahead.$menu.remove()
})

test("should set next item when down arrow is pressed", function () {
var $input = $('<input />').typeahead({
source: ['aa', 'ab', 'ac']
})
}).appendTo('body')
, typeahead = $input.data('typeahead')

$input.val('a')
Expand All @@ -159,7 +166,6 @@ $(function () {

ok(typeahead.$menu.find('li').first().next().hasClass('active'), "second item is active")


$input.trigger({
type: 'keydown'
, keyCode: 38
Expand All @@ -175,14 +181,15 @@ $(function () {

ok(typeahead.$menu.find('li').first().hasClass('active'), "first item is active")

$input.remove()
typeahead.$menu.remove()
})


test("should set input value to selected item", function () {
var $input = $('<input />').typeahead({
source: ['aa', 'ab', 'ac']
})
}).appendTo('body')
, typeahead = $input.data('typeahead')
, changed = false

Expand All @@ -197,14 +204,15 @@ $(function () {
ok(!typeahead.$menu.is(':visible'), 'the menu was hidden')
ok(changed, 'a change event was fired')

$input.remove()
typeahead.$menu.remove()
})

test("should start querying when minLength is met", function () {
var $input = $('<input />').typeahead({
source: ['aaaa', 'aaab', 'aaac'],
minLength: 3
})
}).appendTo('body')
, typeahead = $input.data('typeahead')

$input.val('aa')
Expand All @@ -217,6 +225,7 @@ $(function () {

equals(typeahead.$menu.find('li').length, 3, 'has 3 items in menu')

$input.remove()
typeahead.$menu.remove()
})
})

0 comments on commit 820a3b2

Please sign in to comment.