Skip to content

Commit ee8f38f

Browse files
Kai Cataldoscottgonzalez
Kai Cataldo
authored andcommitted
Enable options by fixing dispatchEvent
Closes gh-40
1 parent 55bf078 commit ee8f38f

File tree

2 files changed

+96
-10
lines changed

2 files changed

+96
-10
lines changed

jquery.simulate.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,10 @@ $.extend( $.simulate.prototype, {
201201
},
202202

203203
dispatchEvent: function( elem, type, event ) {
204-
if ( elem[ type ] ) {
205-
elem[ type ]();
206-
} else if ( elem.dispatchEvent ) {
204+
if ( elem.dispatchEvent ) {
207205
elem.dispatchEvent( event );
206+
} else if ( type === "click" && elem.click && elem.nodeName.toLowerCase() === "input" ) {
207+
elem.click();
208208
} else if ( elem.fireEvent ) {
209209
elem.fireEvent( "on" + type, event );
210210
}

test/unit/simulate.js

Lines changed: 93 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
(function() {
2+
var IE8Check = window.attachEvent && !window.addEventListener,
3+
key = jQuery.simulate.keyCode,
4+
clickOptions,
5+
keyEvents,
6+
keyOptions;
27

38
module( "mouse events" );
49

10+
clickOptions = [ "ctrlKey", "altKey", "shiftKey", "metaKey" ];
11+
512
test( "click on checkbox triggers change", function() {
613
var input = $( "#radiocheckbox-3" ),
714
checked = input.prop( "checked" );
@@ -25,28 +32,107 @@ test( "click on radio triggers change", function() {
2532
notEqual( checked, firstRadio.prop( "checked" ), "radio state changed" );
2633
});
2734

28-
var key = jQuery.simulate.keyCode,
29-
keyEvents = [ "keydown", "keyup", "keypress" ],
30-
i = 0;
35+
test( "click", function() {
36+
expect( 6 );
37+
jQuery( "<div></div>" ).bind( "click", function( event ) {
38+
var value = IE8Check ? 1 : 0;
39+
40+
ok( true, "click event fired" );
41+
equal( event.button, value, "click event was fired with left mouse button" );
42+
equal( event.ctrlKey, false, "click event was fired without control key" );
43+
equal( event.metaKey, false, "click event was fired without meta key" );
44+
equal( event.shiftKey, false, "click event was fired without shift key" );
45+
equal( event.altKey, false, "click event was fired without alt key" );
46+
}).appendTo( "#qunit-fixture" ).simulate( "click" );
47+
});
48+
49+
test( "click with middle mouse button", function() {
50+
expect( 2 );
51+
jQuery( "<div></div>" ).bind( "click", function( event ) {
52+
var value = IE8Check ? 4 : 1;
53+
54+
ok( true, "click event fired" );
55+
equal( event.button, value, "click event was fired with middle mouse button" );
56+
}).appendTo( "#qunit-fixture" ).simulate( "click", {
57+
button: 1
58+
});
59+
});
60+
61+
test( "click with right mouse button", function() {
62+
expect( 2 );
63+
jQuery( "<div></div>" ).bind( "click", function( event ) {
64+
ok( true, "click event fired" );
65+
equal( event.button, 2, "click event was fired with right mouse button" );
66+
}).appendTo( "#qunit-fixture" ).simulate( "click", {
67+
button: 2
68+
});
69+
});
70+
71+
function testClickEvent( clickOption ) {
72+
var options = {};
73+
options[ clickOption ] = true;
74+
75+
test( "click with " + clickOption, function() {
76+
expect( 2 );
77+
jQuery( "<div></div>" ).bind( "click", function( event ) {
78+
ok( true, "click event fired" );
79+
equal( event[ clickOption ], true, "click event was fired with " + clickOption );
80+
}).appendTo( "#qunit-fixture" ).simulate( "click", options);
81+
});
82+
}
83+
84+
jQuery.each(clickOptions, function( index, clickOption ) {
85+
testClickEvent( clickOption );
86+
});
3187

3288
module( "key events" );
3389

90+
keyEvents = [ "keydown", "keyup", "keypress" ];
91+
keyOptions = [ "ctrlKey", "altKey", "shiftKey", "metaKey" ];
92+
3493
function testKeyEvent ( keyEvent ) {
3594
test( keyEvent, function() {
36-
expect( 2 );
95+
expect( 2 + keyOptions.length );
3796
jQuery("<div></div>").bind( keyEvent, function( event ) {
97+
var i = 0;
98+
3899
ok( true, keyEvent + " event fired" );
39100
equal( event.keyCode, key.PAGE_UP, keyEvent + " event has correct keyCode" );
101+
102+
for ( i; i < keyOptions.length; i++ ) {
103+
equal( event[ keyOptions[ i ] ], false, keyEvent + " event fired without " + keyOptions[ i ] );
104+
}
40105
}).appendTo("#qunit-fixture").simulate( keyEvent, {
41106
keyCode: key.PAGE_UP
42107
});
43108
});
44109
}
45110

46-
for ( ; i < keyEvents.length; i++ ) {
47-
testKeyEvent( keyEvents[ i ] );
111+
function testKeyEventOption ( keyEvent, keyOption ) {
112+
test( keyEvent + " with " + keyOption, function() {
113+
var options = {
114+
keyCode: key.PAGE_UP
115+
};
116+
options[ keyOption ] = true;
117+
118+
expect( 3 );
119+
jQuery("<div></div>").bind( keyEvent, function( event ) {
120+
ok( true, keyEvent + " event fired" );
121+
equal( event.keyCode, key.PAGE_UP, keyEvent + " event has correct keyCode" );
122+
equal( event[keyOption], true, keyEvent + " event fired with " + keyOption );
123+
}).appendTo("#qunit-fixture").simulate( keyEvent, options);
124+
});
48125
}
49126

127+
jQuery.each(keyEvents, function( index, keyEvent ) {
128+
testKeyEvent( keyEvent );
129+
130+
jQuery.each(keyOptions, function( index, keyOption ) {
131+
testKeyEventOption( keyEvent, keyOption );
132+
});
133+
});
134+
135+
50136
module( "complex events" );
51137

52138
asyncTest( "drag moves option", function() {
@@ -98,4 +184,4 @@ asyncTest( "drag moves option", function() {
98184
});
99185
});
100186

101-
})();
187+
})();

0 commit comments

Comments
 (0)