| 1 |
define( [ |
| 2 |
"qunit", |
| 3 |
"jquery", |
| 4 |
"lib/helper", |
| 5 |
"ui/widgets/selectmenu" |
| 6 |
], function( QUnit, $, helper ) { |
| 7 |
"use strict"; |
| 8 |
|
| 9 |
QUnit.module( "selectmenu: events", { |
| 10 |
beforeEach: function() { |
| 11 |
this.element = $( "#speed" ); |
| 12 |
}, |
| 13 |
afterEach: helper.moduleAfterEach |
| 14 |
} ); |
| 15 |
|
| 16 |
QUnit.test( "change", function( assert ) { |
| 17 |
var ready = assert.async(); |
| 18 |
assert.expect( 3 ); |
| 19 |
|
| 20 |
var button, menu, options, |
| 21 |
optionIndex = 1; |
| 22 |
|
| 23 |
this.element.selectmenu( { |
| 24 |
change: function( event, ui ) { |
| 25 |
assert.equal( ui.item.index, optionIndex, "ui.item.index contains correct option index" ); |
| 26 |
assert.equal( ui.item.element[ 0 ], options.eq( optionIndex )[ 0 ], |
| 27 |
"ui.item.element contains original option element" ); |
| 28 |
assert.equal( ui.item.value, options.eq( optionIndex ).text(), |
| 29 |
"ui.item.value property updated correctly" ); |
| 30 |
} |
| 31 |
} ); |
| 32 |
|
| 33 |
button = this.element.selectmenu( "widget" ); |
| 34 |
menu = this.element.selectmenu( "menuWidget" ); |
| 35 |
options = this.element.find( "option" ); |
| 36 |
|
| 37 |
button.simulate( "focus" ); |
| 38 |
|
| 39 |
setTimeout( function() { |
| 40 |
button.trigger( "click" ); |
| 41 |
menu.find( "li" ).eq( optionIndex ).simulate( "mouseover" ).trigger( "click" ); |
| 42 |
ready(); |
| 43 |
} ); |
| 44 |
} ); |
| 45 |
|
| 46 |
QUnit.test( "close", function( assert ) { |
| 47 |
assert.expect( 2 ); |
| 48 |
|
| 49 |
var shouldFire; |
| 50 |
|
| 51 |
this.element.selectmenu( { |
| 52 |
close: function() { |
| 53 |
assert.ok( shouldFire, "close event fired on close" ); |
| 54 |
} |
| 55 |
} ); |
| 56 |
|
| 57 |
shouldFire = false; |
| 58 |
this.element.selectmenu( "open" ); |
| 59 |
shouldFire = true; |
| 60 |
this.element.selectmenu( "close" ); |
| 61 |
shouldFire = false; |
| 62 |
this.element.selectmenu( "open" ); |
| 63 |
shouldFire = true; |
| 64 |
$( "body" ).trigger( "mousedown" ); |
| 65 |
} ); |
| 66 |
|
| 67 |
QUnit.test( "focus", function( assert ) { |
| 68 |
var ready = assert.async(); |
| 69 |
assert.expect( 9 ); |
| 70 |
|
| 71 |
var button, menu, links, |
| 72 |
that = this, |
| 73 |
optionIndex = this.element[ 0 ].selectedIndex + 1, |
| 74 |
options = this.element.find( "option" ); |
| 75 |
|
| 76 |
this.element.selectmenu( { |
| 77 |
focus: function( event, ui ) { |
| 78 |
assert.ok( true, "focus event fired on element #" + optionIndex + " mouseover" ); |
| 79 |
assert.equal( ui.item.index, optionIndex, "ui.item.index contains correct option index" ); |
| 80 |
assert.equal( ui.item.element[ 0 ], options.eq( optionIndex )[ 0 ], |
| 81 |
"ui.item.element contains original option element" ); |
| 82 |
} |
| 83 |
} ); |
| 84 |
|
| 85 |
button = this.element.selectmenu( "widget" ); |
| 86 |
menu = this.element.selectmenu( "menuWidget" ); |
| 87 |
|
| 88 |
button.simulate( "focus" ); |
| 89 |
setTimeout( function() { |
| 90 |
button.simulate( "keydown", { keyCode: $.ui.keyCode.DOWN } ); |
| 91 |
|
| 92 |
button.trigger( "click" ); |
| 93 |
links = menu.find( "li.ui-menu-item" ); |
| 94 |
optionIndex = 0; |
| 95 |
links.eq( optionIndex ).simulate( "mouseover", { clientX: 2, clientY: 2 } ); |
| 96 |
optionIndex += 1; |
| 97 |
links.eq( optionIndex ).simulate( "mouseover", { clientX: 3, clientY: 3 } ); |
| 98 |
|
| 99 |
|
| 100 |
that.element.selectmenu( "close" ); |
| 101 |
ready(); |
| 102 |
} ); |
| 103 |
} ); |
| 104 |
|
| 105 |
QUnit.test( "open", function( assert ) { |
| 106 |
assert.expect( 1 ); |
| 107 |
|
| 108 |
this.element.selectmenu( { |
| 109 |
open: function() { |
| 110 |
assert.ok( true, "open event fired on open" ); |
| 111 |
} |
| 112 |
} ); |
| 113 |
|
| 114 |
this.element.selectmenu( "open" ); |
| 115 |
} ); |
| 116 |
|
| 117 |
QUnit.test( "select", function( assert ) { |
| 118 |
var ready = assert.async(); |
| 119 |
assert.expect( 3 ); |
| 120 |
|
| 121 |
this.element.selectmenu( { |
| 122 |
select: function( event, ui ) { |
| 123 |
assert.ok( true, "select event fired on item select" ); |
| 124 |
assert.equal( ui.item.index, optionIndex, "ui.item.index contains correct option index" ); |
| 125 |
assert.equal( ui.item.element[ 0 ], options.eq( optionIndex )[ 0 ], |
| 126 |
"ui.item.element contains original option element" ); |
| 127 |
} |
| 128 |
} ); |
| 129 |
|
| 130 |
var button = this.element.selectmenu( "widget" ), |
| 131 |
menu = this.element.selectmenu( "menuWidget" ), |
| 132 |
options = this.element.find( "option" ), |
| 133 |
optionIndex = 1; |
| 134 |
|
| 135 |
button.simulate( "focus" ); |
| 136 |
setTimeout( function() { |
| 137 |
button.trigger( "click" ); |
| 138 |
menu.find( "li" ).eq( optionIndex ).simulate( "mouseover" ).trigger( "click" ); |
| 139 |
ready(); |
| 140 |
} ); |
| 141 |
} ); |
| 142 |
|
| 143 |
} ); |