Skip to content

Commit c13274d

Browse files
author
Kai Cataldo
committed
Enable options by fixing dispatchEvent
1 parent 55bf078 commit c13274d

File tree

2 files changed

+90
-10
lines changed

2 files changed

+90
-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: 87 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
(function() {
2+
var key = jQuery.simulate.keyCode,
3+
clickOptions = [ "ctrlKey", "altKey", "shiftKey", "metaKey" ],
4+
keyEvents = [ "keydown", "keyup", "keypress" ],
5+
keyOptions = [ "ctrlKey", "altKey", "shiftKey", "metaKey" ],
6+
i = 0,
7+
j = 0,
8+
k = 0;
29

310
module( "mouse events" );
411

@@ -25,26 +32,99 @@ 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+
ok( true, "click event fired" );
39+
equal( event.button, 0, "click event was fired with left mouse button" );
40+
equal( event.ctrlKey, false, "click event was fired without control key" );
41+
equal( event.metaKey, false, "click event was fired without meta key" );
42+
equal( event.shiftKey, false, "click event was fired without shift key" );
43+
equal( event.altKey, false, "click event was fired without alt key" );
44+
}).appendTo( "#qunit-fixture" ).simulate( "click" );
45+
});
46+
47+
test( "click with middle mouse button", function() {
48+
expect( 2 );
49+
jQuery( "<div></div>" ).bind( "click", function( event ) {
50+
ok( true, "click event fired" );
51+
equal( event.button, 1, "click event was fired with middle mouse button" );
52+
}).appendTo( "#qunit-fixture" ).simulate( "click", {
53+
button: 1
54+
});
55+
});
56+
57+
test( "click with right mouse button", function() {
58+
expect( 2 );
59+
jQuery( "<div></div>" ).bind( "click", function( event ) {
60+
ok( true, "click event fired" );
61+
equal( event.button, 2, "click event was fired with right mouse button" );
62+
}).appendTo( "#qunit-fixture" ).simulate( "click", {
63+
button: 2
64+
});
65+
});
66+
67+
function testClickEvent( clickOption ) {
68+
var options = {};
69+
options[ clickOption ] = true;
70+
71+
test( "click with " + clickOption, function() {
72+
expect( 2 );
73+
jQuery( "<div></div>" ).bind( "click", function( event ) {
74+
ok( true, "click event fired" );
75+
equal( event[ clickOption ], true, "click event was fired with " + clickOption );
76+
}).appendTo( "#qunit-fixture" ).simulate( "click", options);
77+
});
78+
}
79+
80+
for (i ; i < clickOptions.length; i++) {
81+
testClickEvent( clickOptions[ i ]);
82+
}
3183

3284
module( "key events" );
3385

3486
function testKeyEvent ( keyEvent ) {
3587
test( keyEvent, function() {
36-
expect( 2 );
88+
expect( 2 + keyOptions.length );
3789
jQuery("<div></div>").bind( keyEvent, function( event ) {
90+
var i = 0;
91+
3892
ok( true, keyEvent + " event fired" );
3993
equal( event.keyCode, key.PAGE_UP, keyEvent + " event has correct keyCode" );
94+
95+
for ( ; i < keyOptions.length; i++ ) {
96+
equal( event[ keyOptions[ i ] ], false, keyEvent + " event fired without " + keyOptions[ i ] );
97+
}
4098
}).appendTo("#qunit-fixture").simulate( keyEvent, {
4199
keyCode: key.PAGE_UP
42100
});
43101
});
44102
}
45103

46-
for ( ; i < keyEvents.length; i++ ) {
47-
testKeyEvent( keyEvents[ i ] );
104+
function testKeyEventOption ( keyEvent, keyOption ) {
105+
test( keyEvent + " with " + keyOption, function() {
106+
var options = {
107+
keyCode: key.PAGE_UP
108+
};
109+
options[ keyOption ] = true;
110+
111+
expect( 3 );
112+
jQuery("<div></div>").bind( keyEvent, function( event ) {
113+
ok( true, keyEvent + " event fired" );
114+
equal( event.keyCode, key.PAGE_UP, keyEvent + " event has correct keyCode" );
115+
equal( event[keyOption], true, keyEvent + " event fired with " + keyOption );
116+
}).appendTo("#qunit-fixture").simulate( keyEvent, options);
117+
});
118+
}
119+
120+
for ( j; j < keyEvents.length; j++ ) {
121+
k = 0;
122+
123+
testKeyEvent( keyEvents[ j ] );
124+
125+
for ( k; k < keyOptions.length; k++ ) {
126+
testKeyEventOption( keyEvents[ j ], keyOptions[ k ] );
127+
}
48128
}
49129

50130
module( "complex events" );
@@ -98,4 +178,4 @@ asyncTest( "drag moves option", function() {
98178
});
99179
});
100180

101-
})();
181+
})();

0 commit comments

Comments
 (0)