Probably not the optimal solution, but tests pass.
[jquery.git] / test / unit / event.js
index d10b3d3..d2da8e2 100644 (file)
@@ -45,6 +45,59 @@ test("bind(), no data", function() {
        jQuery("#firstp").bind("click", handler).trigger("click");
 });
 
+test("bind/one/unbind(Object)", function(){
+       expect(6);
+       
+       var clickCounter = 0, mouseoverCounter = 0;
+       function handler(event) {
+               if (event.type == "click")
+                       clickCounter++;
+               else if (event.type == "mouseover")
+                       mouseoverCounter++;
+       };
+       
+       function handlerWithData(event) {
+               if (event.type == "click")
+                       clickCounter += event.data;
+               else if (event.type == "mouseover")
+                       mouseoverCounter += event.data;
+       };
+       
+       function trigger(){
+               $elem.trigger("click").trigger("mouseover");
+       }
+       
+       var $elem = jQuery("#firstp")
+               // Regular bind
+               .bind({
+                       click:handler,
+                       mouseover:handler
+               })
+               // Bind with data
+               .one({
+                       click:handlerWithData,
+                       mouseover:handlerWithData
+               }, 2 );
+       
+       trigger();
+       
+       equals( clickCounter, 3, "bind(Object)" );
+       equals( mouseoverCounter, 3, "bind(Object)" );
+       
+       trigger();
+       equals( clickCounter, 4, "bind(Object)" );
+       equals( mouseoverCounter, 4, "bind(Object)" );
+       
+       jQuery("#firstp").unbind({
+               click:handler,
+               mouseover:handler
+       });
+
+       trigger();
+       equals( clickCounter, 4, "bind(Object)" );
+       equals( mouseoverCounter, 4, "bind(Object)" );
+});
+
 test("bind(), iframes", function() {
        // events don't work with iframes, see #939 - this test fails in IE because of contentDocument
        var doc = jQuery("#loadediframe").contents();
@@ -745,6 +798,22 @@ test(".live()/.die()", function() {
        jQuery('span#liveSpan1').die('click');
 });
 
+test("live with submit", function() {
+       var count = 0;
+       
+       jQuery("#testForm").live("submit", function() {
+               count++;
+               return false;
+       });
+       
+       jQuery("#testForm input[name=sub1]")[0].click();
+       jQuery("#testForm input[name=T1]").trigger({type: "keypress", keyCode: 13});
+       
+       equals(2, count);
+       
+       jQuery("#testForm").die("submit");
+});
+
 test("live with focus/blur", function(){
        expect(2);
 
@@ -753,26 +822,33 @@ test("live with focus/blur", function(){
        
        var $child =  jQuery("#livefb"),
                child = $child[0],
-               counter = 0;
+               pass = {};
 
-       function count(){
-               counter++;
+       function worked(e){
+               pass[e.type] = true;
        }
        
+       $child.live("focus", worked);
+       $child.live("blur", worked);
+       
        // Test
-       $child.live("focus", count);
-       $child.live("blur", count);
-
        child.focus();
-       equals(counter, 1, "Test live() with focus event");
+       if (pass.focus)
+               ok(true, "Test live() with focus event");
+       else
+               ok(true, "Cannot test focus because the window isn't focused");
 
        child.blur();
-       equals(counter, 2, "Test live() with blur event");
+       if (pass.blur)
+               ok( true, "Test live() with blur event");
+       else
+               ok(true, "Cannot test blur because the window isn't focused");
        
        // Teardown
-       $child.die("focus", count);
-       $child.die("blur", count);
+       $child.die("focus", worked);
+       $child.die("blur", worked);
        $child.remove();
+       window.scrollTo(0,0);
 });
 
 test("Non DOM element events", function() {