Skip to content

Commit 8e4c17f

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

File tree

2 files changed

+86
-10
lines changed

2 files changed

+86
-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: 83 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
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" ];
26

37
module( "mouse events" );
48

@@ -25,28 +29,100 @@ test( "click on radio triggers change", function() {
2529
notEqual( checked, firstRadio.prop( "checked" ), "radio state changed" );
2630
});
2731

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

3281
module( "key events" );
3382

3483
function testKeyEvent ( keyEvent ) {
3584
test( keyEvent, function() {
36-
expect( 2 );
85+
expect( 2 + keyOptions.length );
3786
jQuery("<div></div>").bind( keyEvent, function( event ) {
87+
var i = 0;
88+
3889
ok( true, keyEvent + " event fired" );
3990
equal( event.keyCode, key.PAGE_UP, keyEvent + " event has correct keyCode" );
91+
92+
for ( i; i < keyOptions.length; i++ ) {
93+
equal( event[ keyOptions[ i ] ], false, keyEvent + " event fired without " + keyOptions[ i ] );
94+
}
4095
}).appendTo("#qunit-fixture").simulate( keyEvent, {
4196
keyCode: key.PAGE_UP
4297
});
4398
});
4499
}
45100

46-
for ( ; i < keyEvents.length; i++ ) {
47-
testKeyEvent( keyEvents[ i ] );
101+
function testKeyEventOption ( keyEvent, keyOption ) {
102+
test( keyEvent + " with " + keyOption, function() {
103+
var options = {
104+
keyCode: key.PAGE_UP
105+
};
106+
options[ keyOption ] = true;
107+
108+
expect( 3 );
109+
jQuery("<div></div>").bind( keyEvent, function( event ) {
110+
ok( true, keyEvent + " event fired" );
111+
equal( event.keyCode, key.PAGE_UP, keyEvent + " event has correct keyCode" );
112+
equal( event[keyOption], true, keyEvent + " event fired with " + keyOption );
113+
}).appendTo("#qunit-fixture").simulate( keyEvent, options);
114+
});
48115
}
49116

117+
keyEvents.forEach( function( keyEvent ) {
118+
testKeyEvent( keyEvent );
119+
120+
keyOptions.forEach( function( keyOption ) {
121+
testKeyEventOption( keyEvent, keyOption );
122+
});
123+
});
124+
125+
50126
module( "complex events" );
51127

52128
asyncTest( "drag moves option", function() {
@@ -98,4 +174,4 @@ asyncTest( "drag moves option", function() {
98174
});
99175
});
100176

101-
})();
177+
})();

0 commit comments

Comments
 (0)