Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions jquery.simulate.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,10 @@ $.extend( $.simulate.prototype, {
},

dispatchEvent: function( elem, type, event ) {
if ( elem[ type ] ) {
elem[ type ]();
} else if ( elem.dispatchEvent ) {
if ( elem.dispatchEvent ) {
elem.dispatchEvent( event );
} else if ( type === "click" && elem.click && elem.nodeName.toLowerCase() === "input" ) {
elem.click();
} else if ( elem.fireEvent ) {
elem.fireEvent( "on" + type, event );
}
Expand Down
100 changes: 93 additions & 7 deletions test/unit/simulate.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
(function() {
var IE8Check = window.attachEvent && !window.addEventListener,
key = jQuery.simulate.keyCode,
clickOptions,
keyEvents,
keyOptions;

module( "mouse events" );

clickOptions = [ "ctrlKey", "altKey", "shiftKey", "metaKey" ];

test( "click on checkbox triggers change", function() {
var input = $( "#radiocheckbox-3" ),
checked = input.prop( "checked" );
Expand All @@ -25,28 +32,107 @@ test( "click on radio triggers change", function() {
notEqual( checked, firstRadio.prop( "checked" ), "radio state changed" );
});

var key = jQuery.simulate.keyCode,
keyEvents = [ "keydown", "keyup", "keypress" ],
i = 0;
test( "click", function() {
expect( 6 );
jQuery( "<div></div>" ).bind( "click", function( event ) {
var value = IE8Check ? 1 : 0;

ok( true, "click event fired" );
equal( event.button, value, "click event was fired with left mouse button" );
equal( event.ctrlKey, false, "click event was fired without control key" );
equal( event.metaKey, false, "click event was fired without meta key" );
equal( event.shiftKey, false, "click event was fired without shift key" );
equal( event.altKey, false, "click event was fired without alt key" );
}).appendTo( "#qunit-fixture" ).simulate( "click" );
});

test( "click with middle mouse button", function() {
expect( 2 );
jQuery( "<div></div>" ).bind( "click", function( event ) {
var value = IE8Check ? 4 : 1;

ok( true, "click event fired" );
equal( event.button, value, "click event was fired with middle mouse button" );
}).appendTo( "#qunit-fixture" ).simulate( "click", {
button: 1
});
});

test( "click with right mouse button", function() {
expect( 2 );
jQuery( "<div></div>" ).bind( "click", function( event ) {
ok( true, "click event fired" );
equal( event.button, 2, "click event was fired with right mouse button" );
}).appendTo( "#qunit-fixture" ).simulate( "click", {
button: 2
});
});

function testClickEvent( clickOption ) {
var options = {};
options[ clickOption ] = true;

test( "click with " + clickOption, function() {
expect( 2 );
jQuery( "<div></div>" ).bind( "click", function( event ) {
ok( true, "click event fired" );
equal( event[ clickOption ], true, "click event was fired with " + clickOption );
}).appendTo( "#qunit-fixture" ).simulate( "click", options);
});
}

jQuery.each(clickOptions, function( index, clickOption ) {
testClickEvent( clickOption );
});

module( "key events" );

keyEvents = [ "keydown", "keyup", "keypress" ];
keyOptions = [ "ctrlKey", "altKey", "shiftKey", "metaKey" ];

function testKeyEvent ( keyEvent ) {
test( keyEvent, function() {
expect( 2 );
expect( 2 + keyOptions.length );
jQuery("<div></div>").bind( keyEvent, function( event ) {
var i = 0;

ok( true, keyEvent + " event fired" );
equal( event.keyCode, key.PAGE_UP, keyEvent + " event has correct keyCode" );

for ( i; i < keyOptions.length; i++ ) {
equal( event[ keyOptions[ i ] ], false, keyEvent + " event fired without " + keyOptions[ i ] );
}
}).appendTo("#qunit-fixture").simulate( keyEvent, {
keyCode: key.PAGE_UP
});
});
}

for ( ; i < keyEvents.length; i++ ) {
testKeyEvent( keyEvents[ i ] );
function testKeyEventOption ( keyEvent, keyOption ) {
test( keyEvent + " with " + keyOption, function() {
var options = {
keyCode: key.PAGE_UP
};
options[ keyOption ] = true;

expect( 3 );
jQuery("<div></div>").bind( keyEvent, function( event ) {
ok( true, keyEvent + " event fired" );
equal( event.keyCode, key.PAGE_UP, keyEvent + " event has correct keyCode" );
equal( event[keyOption], true, keyEvent + " event fired with " + keyOption );
}).appendTo("#qunit-fixture").simulate( keyEvent, options);
});
}

jQuery.each(keyEvents, function( index, keyEvent ) {
testKeyEvent( keyEvent );

jQuery.each(keyOptions, function( index, keyOption ) {
testKeyEventOption( keyEvent, keyOption );
});
});


module( "complex events" );

asyncTest( "drag moves option", function() {
Expand Down Expand Up @@ -98,4 +184,4 @@ asyncTest( "drag moves option", function() {
});
});

})();
})();