Skip to content

Commit

Permalink
Add a way to fire native events using dispatchEvent/fireEvent in test…
Browse files Browse the repository at this point in the history
…init.js; fixes failing click test in FF3.6
  • Loading branch information
timmywil committed Oct 28, 2011
1 parent fa0e801 commit 38f087b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 23 deletions.
16 changes: 15 additions & 1 deletion test/data/testinit.js
Expand Up @@ -2,7 +2,7 @@ var jQuery = this.jQuery || "jQuery", // For testing .noConflict()
$ = this.$ || "$",
originaljQuery = jQuery,
original$ = $,
amdDefined;
amdDefined;

/**
* Set up a mock AMD define function for testing AMD registration.
Expand Down Expand Up @@ -44,6 +44,20 @@ function t(a,b,c) {
same(f, q.apply(q,c), a + " (" + b + ")");
}

var fireNative;
if ( document.createEvent ) {
fireNative = function( node, type ) {
var event = document.createEvent('HTMLEvents');
event.initEvent( type, true, true );
node.dispatchEvent( event );
};
} else {
fireNative = function( node, type ) {
var event = document.createEventObject();
node.fireEvent( 'on' + type, event );
};
}

/**
* Add random number to url to stop IE from caching
*
Expand Down
46 changes: 24 additions & 22 deletions test/unit/event.js
Expand Up @@ -19,8 +19,8 @@ test("bind(),live(),delegate() with non-null,defined data", function() {
expect(3);

var handler = function( event, data ) {
equal( data, 0, "non-null, defined data (zero) is correctly passed" );
};
equal( data, 0, "non-null, defined data (zero) is correctly passed" );
};

jQuery("#foo").bind("foo", handler);
jQuery("#foo").live("foo", handler);
Expand All @@ -35,14 +35,14 @@ test("bind(),live(),delegate() with non-null,defined data", function() {
});

test("Handler changes and .trigger() order", function() {
expect(1);
expect(1);

var markup = jQuery(
'<div><div><p><span><b class="a">b</b></span></p></div></div>'
),
path = "";
var markup = jQuery(
'<div><div><p><span><b class="a">b</b></span></p></div></div>'
),
path = "";

markup
markup
.find( "*" ).andSelf().on( "click", function( e ) {
path += this.nodeName.toLowerCase() + " ";
})
Expand All @@ -53,11 +53,11 @@ test("Handler changes and .trigger() order", function() {
}
});

markup.find( "b" ).trigger( "click" );
markup.find( "b" ).trigger( "click" );

equals( path, "b p div div ", "Delivered all events" );
equals( path, "b p div div ", "Delivered all events" );

markup.remove();
markup.remove();
});

test("bind(), with data", function() {
Expand Down Expand Up @@ -118,16 +118,16 @@ test("bind(), five events at once", function() {
expect(1);

var count = 0,
handler = function(event) {
count++;
};
handler = function(event) {
count++;
};

jQuery("#firstp").bind("click mouseover foo bar baz", handler)
.trigger("click").trigger("mouseover")
.trigger("foo").trigger("bar")
.trigger("baz");
.trigger("click").trigger("mouseover")
.trigger("foo").trigger("bar")
.trigger("baz");

equals( count, 5, "bind() five events at once" );
equals( count, 5, "bind() five events at once" );
});

test("bind(), multiple events at once and namespaces", function() {
Expand Down Expand Up @@ -997,7 +997,7 @@ test("trigger(type, [data], [fn])", function() {

$elem.die('mouseleave');

// Triggers handlrs and native
// Triggers handlrs and native
// Trigger 5
$elem.bind("click", handler).trigger("click", [1, "2", "abc"]);

Expand Down Expand Up @@ -2477,12 +2477,13 @@ test("fixHooks extensions", function() {

// IE requires focusable elements to be visible, so append to body
var $fixture = jQuery( "<input type='text' id='hook-fixture' />" ).appendTo( "body" ),
saved = jQuery.event.fixHooks.click;
saved = jQuery.event.fixHooks.click;

// Ensure the property doesn't exist
$fixture.bind( "click", function( event ) {
ok( !("blurrinessLevel" in event), "event.blurrinessLevel does not exist" );
})[0].click();
});
fireNative( $fixture[0], 'click' );
$fixture.unbind( "click" );

jQuery.event.fixHooks.click = {
Expand All @@ -2495,7 +2496,8 @@ test("fixHooks extensions", function() {
// Trigger a native click and ensure the property is set
$fixture.bind( "click", function( event ) {
equals( event.blurrinessLevel, 42, "event.blurrinessLevel was set" );
})[0].click();
});
fireNative( $fixture[0], 'click' );

delete jQuery.event.fixHooks.click;
$fixture.unbind( "click" ).remove();
Expand Down

0 comments on commit 38f087b

Please sign in to comment.