Permalink
Browse files

Adding "fire" unit tests

  • Loading branch information...
1 parent 521afaa commit 7ec799b62daa4c88327efe743d0a98be9f9e799d Michael Helgeson committed Jan 27, 2011
Showing with 333 additions and 0 deletions.
  1. +5 −0 fire/jquery.fire.js
  2. +61 −0 fire/test/custom.js
  3. +50 −0 fire/test/dom.js
  4. +24 −0 fire/test/index.html
  5. +52 −0 fire/test/key.js
  6. +54 −0 fire/test/mouse.js
  7. +26 −0 fire/test/requirements.js
  8. +61 −0 fire/test/touch.js
View
@@ -74,6 +74,11 @@ $.fire.prototype = {
);
$.extend( event, {
view: opts.view,
+ detail: opts.detail,
+ screenX: opts.screenX,
+ screenY: opts.screenY,
+ clientX: opts.clientX,
+ clientY: opts.clientY,
ctrlKey: opts.ctrlKey,
altKey: opts.altKey,
shiftKey: opts.shiftKey,
View
@@ -0,0 +1,61 @@
+module("Custom Events");
+
+if ( !document.createEvent ){
+ test("Custom Event Simulation Not Supported",function(){
+ ok( true, 'This browser does not support "document.createEvent" and cannot simulate custom events.');
+ });
+}
+else {
+
+ // test each of the following events
+ $.each([
+ "snap","crackle","pop"
+ ],function( i, type ){
+ // test each event type
+ test( '"'+ type +'"', function(){
+ expect( 33 );
+
+ // custom event properties
+ var props = {
+ pageX: Math.round( Math.random() * 500 ),
+ pageY: Math.round( Math.random() * 500 ),
+ ctrlKey: Math.round( Math.random() ) ? true : false,
+ altKey: Math.round( Math.random() ) ? true : false,
+ shiftKey: Math.round( Math.random() ) ? true : false,
+ button: Math.round( Math.random() * 2 )
+ },
+ // new test element
+ $div = $('<div/>').appendTo( document.body );
+ // test the document too for bubbling
+ $div.add( document ).bind( type, function( ev ){
+
+ equals( ev.currentTarget, this, "event.currentTarget");
+ equals( ev.target, $div[0], "event.target" );
+ equals( ev.type, type, "event.type" );
+ equals( ev.pageX, props.pageX, "event.pageX" );
+ equals( ev.pageY, props.pageY, "event.pageY" );
+ equals( ev.ctrlKey, props.ctrlKey, "event.ctrlKey" );
+ equals( ev.altKey, props.altKey, "event.altKey" );
+ equals( ev.shiftKey, props.shiftKey, "event.shiftKey" );
+ equals( ev.metaKey, props.metaKey, "event.metaKey" );
+ equals( ev.button, props.button, "event.button" );
+ equals( ev.bubbles, props.bubbles, "event.bubbles" );
+ });
+
+ // make sure that metaKey and ctrlKey are equal
+ props.metaKey = props.ctrlKey;
+ // fire the event with bubbling
+ props.bubbles = true;
+ $div.fire( type, props );
+
+ // fire the event without bubbling
+ props.bubbles = false;
+ $div.fire( type, props );
+
+ // cleanup
+ $( document ).unbind( type );
+ $div.remove();
+ });
+ });
+
+}
View
@@ -0,0 +1,50 @@
+module("DOM Events");
+
+// test each of the following events
+$.each([
+ "focus","blur","change","scroll","resize",
+ "error","load","unload","submit","select"
+],function( i, type ){
+ // test each event type
+ test( '"'+ type +'"', function(){
+ expect( 0 );
+
+ // custom event properties
+ var props = {
+ //keyCode: Math.round( Math.random() * 256 ),
+ //charCode: Math.round( Math.random() * 256 ),
+ ctrlKey: Math.round( Math.random() ) ? true : false,
+ altKey: Math.round( Math.random() ) ? true : false,
+ shiftKey: Math.round( Math.random() ) ? true : false
+ },
+ // new test element
+ $div = $('<div/>').appendTo( document.body );
+ // test the document too for bubbling
+ $div.add( document ).bind( type, function( ev ){
+
+ equals( ev.currentTarget, this, "event.currentTarget");
+ equals( ev.target, $div[0], "event.target" );
+ equals( ev.type, type, "event.type" );
+
+ equals( ev.ctrlKey, props.ctrlKey, "event.ctrlKey" );
+ equals( ev.altKey, props.altKey, "event.altKey" );
+ equals( ev.shiftKey, props.shiftKey, "event.shiftKey" );
+ equals( ev.metaKey, props.metaKey, "event.metaKey" );
+ equals( ev.bubbles, props.bubbles, "event.bubbles" );
+ });
+
+ // make sure that metaKey and ctrlKey are equal
+ props.metaKey = props.ctrlKey;
+ // fire the event with bubbling
+ props.bubbles = true;
+ $div.fire( type, props );
+
+ // fire the event without bubbling
+ props.bubbles = false;
+ $div.fire( type, props );
+
+ // cleanup
+ $( document ).unbind( type );
+ $div.remove();
+ });
+});
View
@@ -0,0 +1,24 @@
+<!DOCTYPE html>
+<html xmlns="http://www.w3.org/1999/xhtml">
+ <head>
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+ <link href="../../img/favicon.ico" rel="shortcut icon" />
+ <link href="../../jquery/qunit.css" rel="stylesheet" />
+ <script src="../../jquery/jquery.js"></script>
+ <script src="../../jquery/qunit.js"></script>
+ <script src="../../fire/jquery.fire.js"></script>
+ <script src="./requirements.js"></script>
+ <script src="./mouse.js"></script>
+ <script src="./key.js"></script>
+ <script src="./dom.js"></script>
+ <script src="./touch.js"></script>
+ <script src="./custom.js"></script>
+ <title>ThreeDubMedia &middot; jQuery.fire.js</title>
+ </head>
+ <body>
+ <h1 id="qunit-header">jQuery.fire.js Unit Tests</h1>
+ <h2 id="qunit-banner"></h2>
+ <h2 id="qunit-userAgent"></h2>
+ <ol id="qunit-tests"></ol>
+ </body>
+</html>
View
@@ -0,0 +1,52 @@
+module("Key Events");
+
+// test each of the following events
+$.each([
+ "keydown","keyup","keypress"
+],function( i, type ){
+ // test each event type
+ test( '"'+ type +'"', function(){
+ expect( i < 2 ? 27 : 30 );
+
+ // custom event properties
+ var props = {
+ keyCode: Math.round( Math.random() * 256 ),
+ charCode: Math.round( Math.random() * 256 ),
+ ctrlKey: Math.round( Math.random() ) ? true : false,
+ altKey: Math.round( Math.random() ) ? true : false,
+ shiftKey: Math.round( Math.random() ) ? true : false
+ },
+ // new test element
+ $div = $('<div/>').appendTo( document.body );
+ // test the document too for bubbling
+ $div.add( document ).bind( type, function( ev ){
+
+ equals( ev.currentTarget, this, "event.currentTarget");
+ equals( ev.target, $div[0], "event.target" );
+ equals( ev.type, type, "event.type" );
+ equals( ev.keyCode, props.keyCode, "event.keyCode" );
+ if ( type == "keypress" ){
+ equals( ev.charCode, props.charCode, "event.charCode" );
+ }
+ equals( ev.ctrlKey, props.ctrlKey, "event.ctrlKey" );
+ equals( ev.altKey, props.altKey, "event.altKey" );
+ equals( ev.shiftKey, props.shiftKey, "event.shiftKey" );
+ equals( ev.metaKey, props.metaKey, "event.metaKey" );
+ equals( ev.bubbles, props.bubbles, "event.bubbles" );
+ });
+
+ // make sure that metaKey and ctrlKey are equal
+ props.metaKey = props.ctrlKey;
+ // fire the event with bubbling
+ props.bubbles = true;
+ $div.fire( type, props );
+
+ // fire the event without bubbling
+ props.bubbles = false;
+ $div.fire( type, props );
+
+ // cleanup
+ $( document ).unbind( type );
+ $div.remove();
+ });
+});
View
@@ -0,0 +1,54 @@
+module("Mouse Events");
+
+// test each of the following events
+$.each([
+ "click","dblclick",
+ "mouseover","mouseout",
+ "mousedown","mouseup","mousemove"
+],function( i, type ){
+ // test each event type
+ test( '"'+ type +'"', function(){
+ expect( 33 );
+
+ // custom event properties
+ var props = {
+ pageX: Math.round( Math.random() * 500 ),
+ pageY: Math.round( Math.random() * 500 ),
+ ctrlKey: Math.round( Math.random() ) ? true : false,
+ altKey: Math.round( Math.random() ) ? true : false,
+ shiftKey: Math.round( Math.random() ) ? true : false,
+ button: Math.round( Math.random() * 2 )
+ },
+ // new test element
+ $div = $('<div/>').appendTo( document.body );
+ // test the document too for bubbling
+ $div.add( document ).bind( type, function( ev ){
+
+ equals( ev.currentTarget, this, "event.currentTarget");
+ equals( ev.target, $div[0], "event.target" );
+ equals( ev.type, type, "event.type" );
+ equals( ev.pageX, props.pageX, "event.pageX" );
+ equals( ev.pageY, props.pageY, "event.pageY" );
+ equals( ev.ctrlKey, props.ctrlKey, "event.ctrlKey" );
+ equals( ev.altKey, props.altKey, "event.altKey" );
+ equals( ev.shiftKey, props.shiftKey, "event.shiftKey" );
+ equals( ev.metaKey, props.metaKey, "event.metaKey" );
+ equals( ev.button, props.button, "event.button" );
+ equals( ev.bubbles, props.bubbles, "event.bubbles" );
+ });
+
+ // make sure that metaKey and ctrlKey are equal
+ props.metaKey = props.ctrlKey;
+ // fire the event with bubbling
+ props.bubbles = true;
+ $div.fire( type, props );
+
+ // fire the event without bubbling
+ props.bubbles = false;
+ $div.fire( type, props );
+
+ // cleanup
+ $( document ).unbind( type );
+ $div.remove();
+ });
+});
View
@@ -0,0 +1,26 @@
+module("Requirements");
+
+test("jQuery",function(){
+
+ expect( 2 );
+
+ // make sure the right jquery is included
+ ok( window.jQuery, "jQuery exists" );
+ ok( parseFloat( jQuery([]).jquery ) >= 1.4, "jQuery version is 1.4 or greater" );
+
+});
+
+test("Installation",function(){
+
+ expect( 8 );
+
+ // make sure the plugin interface is complete
+ ok( jQuery.fn.fire, "FIRE method is defined" );
+ ok( jQuery.fire, "CONSTRUCTOR is defined" );
+ ok( jQuery.fire.defaults, "DEFAULTS are defined" );
+ ok( jQuery.fire.prototype.create, "CREATE method is defined" );
+ ok( jQuery.fire.prototype.event, "EVENT method is defined" );
+ ok( jQuery.fire.prototype.mouse, "MOUSE method is defined" );
+ ok( jQuery.fire.prototype.key, "KEY method is defined" );
+ ok( jQuery.fire.prototype.dispatch, "DISPATCH method is defined" );
+});
View
@@ -0,0 +1,61 @@
+module("Touch Events");
+
+if ( !document.createEvent ){
+ test("Touch Simulation Not Supported",function(){
+ ok( true, 'This browser does not support "document.createEvent" and cannot simulate touch events.');
+ });
+}
+else {
+
+ // test each of the following events
+ $.each([
+ "touchstart","touchmove","touchend"
+ ],function( i, type ){
+ // test each event type
+ test( '"'+ type +'"', function(){
+ expect( 33 );
+
+ // custom event properties
+ var props = {
+ pageX: Math.round( Math.random() * 500 ),
+ pageY: Math.round( Math.random() * 500 ),
+ ctrlKey: Math.round( Math.random() ) ? true : false,
+ altKey: Math.round( Math.random() ) ? true : false,
+ shiftKey: Math.round( Math.random() ) ? true : false,
+ button: Math.round( Math.random() * 2 )
+ },
+ // new test element
+ $div = $('<div/>').appendTo( document.body );
+ // test the document too for bubbling
+ $div.add( document ).bind( type, function( ev ){
+
+ equals( ev.currentTarget, this, "event.currentTarget");
+ equals( ev.target, $div[0], "event.target" );
+ equals( ev.type, type, "event.type" );
+ equals( ev.pageX, props.pageX, "event.pageX" );
+ equals( ev.pageY, props.pageY, "event.pageY" );
+ equals( ev.ctrlKey, props.ctrlKey, "event.ctrlKey" );
+ equals( ev.altKey, props.altKey, "event.altKey" );
+ equals( ev.shiftKey, props.shiftKey, "event.shiftKey" );
+ equals( ev.metaKey, props.metaKey, "event.metaKey" );
+ equals( ev.button, props.button, "event.button" );
+ equals( ev.bubbles, props.bubbles, "event.bubbles" );
+ });
+
+ // make sure that metaKey and ctrlKey are equal
+ props.metaKey = props.ctrlKey;
+ // fire the event with bubbling
+ props.bubbles = true;
+ $div.fire( type, props );
+
+ // fire the event without bubbling
+ props.bubbles = false;
+ $div.fire( type, props );
+
+ // cleanup
+ $( document ).unbind( type );
+ $div.remove();
+ });
+ });
+
+}

0 comments on commit 7ec799b

Please sign in to comment.