Permalink
Browse files

Adding "drop" unit tests

  • Loading branch information...
1 parent 218d43e commit 15447786198d1543bda644464ebb323c072d66cc @threedubmedia committed Jun 8, 2010
View
@@ -0,0 +1,30 @@
+test("Event Binding",function(){
+
+ expect( 36 );
+
+ $.each(['dropinit','dropstart','drop','dropend'],function( i, type ){
+
+ // make sure the event handler gets bound to the element
+ var $elem = $('<div />'),
+ elem = $elem[0],
+ count = 0,
+ fn = function(){
+ count += 1;
+ };
+
+ ok( $elem.bind( type, fn )[0] == elem, ".bind('"+ type +"', fn )" );
+ ok( $.data( elem, $.event.special.drop.datakey ), "drop data exists" );
+ ok( $.data( elem, "events" ), "event data exists" );
+ ok( $.data( elem, "events" )[ type ][0], type +" event handler added" );
+
+ ok( $elem.trigger( type )[0] == elem, ".trigger('"+ type +"')" );
+ ok( count == 1, "handler was triggered");
+
+ ok( $elem.unbind( type )[0] == elem, ".unbind('"+ type +"')" );
+ ok( !$.data( elem, "events" ), "event data removed" );
+ ok( !$.data( elem, $.event.special.drop.datakey ), "drop data removed" );
+
+ $elem.remove();
+
+ });
+});
View
@@ -0,0 +1,116 @@
+test("Event Handlers",function(){
+
+ expect( 24 );
+
+ // create the markup for the test
+ var $drag = $('<div class="drag"/>')
+ .appendTo( document.body )
+ .css({
+ position: 'absolute',
+ top: 0,
+ left: 0,
+ height: 100,
+ width: 100
+ })
+ .bind("dragend",{ drop:'.drop' },function( event, dd ){
+ same( dd.drop, dragend, "drop (dragend)" );
+ }),
+ $drop = $('<div class="drop"/><div class="extra"/>')
+ .appendTo( document.body )
+ .css({
+ position: 'absolute',
+ top: 0,
+ left: 0,
+ height: 100,
+ width: 100
+ })
+ .bind("dropinit dropstart drop dropend",function( event, dd ){
+ counts[ event.type ] += 1;
+ return returned[ event.type ];
+ }),
+ counts, returned, dragend,
+ reset = function(){
+ $.drop({ mode:'overlap', multi:false });
+ counts = { dropinit:0, dropstart:0, drop:0, dropend:0 };
+ returned = {};
+ };
+
+ // test DROPINIT FALSE
+ ok( true, 'DROPINIT returns false...' );
+ // test prep
+ reset();
+ returned.dropinit = false;
+ dragend = [];
+ // simulate a partial drag
+ $drag
+ .fire("mousedown",{ clientX:50, clientY:50 })
+ .fire("mousemove",{ clientX:51, clientY:51 })
+ .fire("mouseup",{ clientX:51, clientY:51 })
+ .trigger("click");
+ // check counts
+ equals( counts.dropinit, 1, "dropinit");
+ equals( counts.dropstart, 0, "dropstart");
+ equals( counts.drop, 0, "drop");
+ equals( counts.dropend, 0, "dropend");
+
+ // test DROPSTART FALSE
+ ok( true, 'DROPSTART returns false...' );
+ // test prep
+ reset();
+ returned.dropstart = false;
+ dragend = [];
+ // simulate a partial drag
+ $drag
+ .fire("mousedown",{ clientX:50, clientY:50 })
+ .fire("mousemove",{ clientX:51, clientY:51 })
+ .fire("mouseup",{ clientX:51, clientY:51 })
+ .trigger("click");
+ // check counts
+ equals( counts.dropinit, 1, "dropinit");
+ equals( counts.dropstart, 1, "dropstart");
+ equals( counts.drop, 0, "drop");
+ equals( counts.dropend, 0, "dropend");
+
+ // test DROP FALSE
+ ok( true, 'DROP returns false...' );
+ // test prep
+ reset();
+ returned.drop = false;
+ dragend = [];
+ // simulate a partial drag
+ $drag
+ .fire("mousedown",{ clientX:50, clientY:50 })
+ .fire("mousemove",{ clientX:51, clientY:51 })
+ .fire("mouseup",{ clientX:51, clientY:51 })
+ .trigger("click");
+ // check counts
+ equals( counts.dropinit, 1, "dropinit");
+ equals( counts.dropstart, 1, "dropstart");
+ equals( counts.drop, 1, "drop");
+ equals( counts.dropend, 0, "dropend");
+
+ // test DROPINIT returns elements
+ ok( true, 'DROPINIT returns elements...' );
+ // test prep
+ reset();
+ returned.dropinit = $drop.eq(1);
+ dragend = [ $drop[1] ];
+ // simulate a partial drag
+ $drag
+ .fire("mousedown",{ clientX:50, clientY:50 })
+ .fire("mousemove",{ clientX:51, clientY:51 })
+ .fire("mouseup",{ clientX:51, clientY:51 })
+ .trigger("click");
+ // check counts
+ equals( counts.dropinit, 1, "dropinit");
+ equals( counts.dropstart, 1, "dropstart");
+ equals( counts.drop, 1, "drop");
+ equals( counts.dropend, 1, "dropend");
+
+ // test clean-up
+ setTimeout(function(){
+ $drag.remove();
+ $drop.remove();
+ }, 20 );
+
+});
View
@@ -0,0 +1,27 @@
+<!DOCTYPE html>
+<html xmlns="http://www.w3.org/1999/xhtml">
+<head>
+<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+<link href="./inc/qunit.css" rel="stylesheet" />
+<script src="./inc/jquery-1.4.2.js"></script>
+<script src="./inc/qunit.js"></script>
+<script src="./inc/jquery.fire.js"></script>
+<script src="./inc/jquery.event.drag.js"></script>
+<script src="./inc/jquery.event.drop.js"></script>
+<script src="./requirements.js"></script>
+<script src="./binding.js"></script>
+<script src="./method.js"></script>
+<script src="./tolerance.js"></script>
+<script src="./interaction.js"></script>
+<script src="./properties.js"></script>
+<script src="./handlers.js"></script>
+<script src="./live.js"></script>
+<title>ThreeDubMedia &middot; jquery.event.drop.js</title>
+</head>
+<body>
+<h1 id="qunit-header">jquery.event.drop.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,144 @@
+test("Interaction Options",function(){
+
+ expect( 36 );
+
+ // create the markup for the test
+ var $drag = $('<div />')
+ .css({
+ position: 'absolute',
+ top: 0,
+ left: 0,
+ height: 100,
+ width: 100
+ })
+ .appendTo( document.body )
+ .bind("draginit dragstart drag dragend click", function( event ){
+ counts[ event.type ] += 1;
+ }),
+ $drop = $('<div />')
+ .css({
+ position: 'absolute',
+ top: 0,
+ left: 0,
+ height: 100,
+ width: 100
+ })
+ .appendTo( document.body )
+ .bind("dropinit dropstart drop dropend",function( event ){
+ counts[ event.type ] += 1;
+ }),
+ counts,
+ reset = function( opts ){
+ counts = {
+ draginit:0, dragstart:0, drag:0, dragend:0,
+ dropinit:0, dropstart:0, drop:0, dropend:0,
+ click:0
+ };
+ $.extend( $drag.data( $.event.special.drag.datakey ), $.event.special.drag.defaults, opts );
+ };
+
+
+ setTimeout(function(){
+ ok( true, "Drag and Drop defaults...");
+ // prep DEFAULT interaction
+ reset();
+ // simulate DEFAULT interaction
+ $drag
+ .fire("mousedown",{ clientX:50, clientY:50 })
+ .fire("mousemove",{ clientX:51, clientY:51 })
+ .fire("mouseup",{ clientX:51, clientY:51 })
+ .trigger("click");
+ // inspect results
+ equals( counts.draginit, 1, "draginit");
+ equals( counts.dragstart, 1, "dragstart");
+ equals( counts.drag, 1, "drag");
+ equals( counts.dragend, 1, "dragend");
+ equals( counts.dropinit, 1, "dropinit");
+ equals( counts.dropstart, 1, "dropstart");
+ equals( counts.drop, 1, "drop");
+ equals( counts.dropend, 1, "dropend");
+ // continue
+ start();
+ }, 20 );
+ stop();
+
+ setTimeout(function(){
+ ok( true, 'Drag "drop" option (false)...');
+ // prep interaction
+ reset({ drop:false });
+ // simulate drag
+ $drag
+ .fire("mousedown",{ clientX:50, clientY:50 })
+ .fire("mousemove",{ clientX:51, clientY:51 })
+ .fire("mouseup",{ clientX:51, clientY:51 })
+ .trigger("click");
+ // inspect results
+ equals( counts.draginit, 1, "draginit");
+ equals( counts.dragstart, 1, "dragstart");
+ equals( counts.drag, 1, "drag");
+ equals( counts.dragend, 1, "dragend");
+ equals( counts.dropinit, 0, "dropinit");
+ equals( counts.dropstart, 0, "dropstart");
+ equals( counts.drop, 0, "drop");
+ equals( counts.dropend, 0, "dropend");
+ // continue
+ start();
+ }, 20 );
+ stop();
+
+ setTimeout(function(){
+ ok( true, 'Drag "drop" option (unmatched)...');
+ // prep interaction
+ reset({ drop:'body' });
+ // simulate drag
+ $drag
+ .fire("mousedown",{ clientX:50, clientY:50 })
+ .fire("mousemove",{ clientX:51, clientY:51 })
+ .fire("mouseup",{ clientX:51, clientY:51 })
+ .trigger("click");
+ // inspect results
+ equals( counts.draginit, 1, "draginit");
+ equals( counts.dragstart, 1, "dragstart");
+ equals( counts.drag, 1, "drag");
+ equals( counts.dragend, 1, "dragend");
+ equals( counts.dropinit, 0, "dropinit");
+ equals( counts.dropstart, 0, "dropstart");
+ equals( counts.drop, 0, "drop");
+ equals( counts.dropend, 0, "dropend");
+ // continue
+ start();
+ }, 20 );
+ stop();
+
+ setTimeout(function(){
+ ok( true, 'Drag "drop" option (matched)...');
+ // prep interaction
+ reset({ drop:'div' });
+ // simulate drag
+ $drag
+ .fire("mousedown",{ clientX:50, clientY:50 })
+ .fire("mousemove",{ clientX:51, clientY:51 })
+ .fire("mouseup",{ clientX:51, clientY:51 })
+ .trigger("click");
+ // inspect results
+ equals( counts.draginit, 1, "draginit");
+ equals( counts.dragstart, 1, "dragstart");
+ equals( counts.drag, 1, "drag");
+ equals( counts.dragend, 1, "dragend");
+ equals( counts.dropinit, 1, "dropinit");
+ equals( counts.dropstart, 1, "dropstart");
+ equals( counts.drop, 1, "drop");
+ equals( counts.dropend, 1, "dropend");
+ // continue
+ start();
+ }, 20 );
+ stop();
+
+ // test clean-up
+ setTimeout(function(){
+ $drag.remove();
+ $drop.remove();
+ }, 20 );
+
+});
+
View
@@ -0,0 +1,48 @@
+test("Live Delegation",function(){
+
+ expect( 0 );
+
+ // set up the delegation
+ $('.drop-live').live("dropinit dropstart drop dropend",function( event ){
+ counts[ event.type ] += 1;
+ equals( this, $drop[0], event.type+" target" );
+ });
+ // local refs
+ var counts = { dropinit:0, dropstart:0, drop:0, dropend:0 },
+ // add a div to test the delegation
+ $drop = $('<div class="drop-live" />')
+ .css({
+ position: 'absolute',
+ top: 0, left: 0,
+ height: 100, width: 100
+ })
+ .appendTo( document.body ),
+ // add a dragger
+ $drag = $('<div class="drag-live" />')
+ .css({
+ position: 'absolute',
+ top: 0, left: 0,
+ height: 100, width: 100
+ })
+ .appendTo( document.body )
+ .drag(function(){ },{ drop:'.drop-live' });
+
+ // check triggering of the event handlers
+ $.each(["dropend","drop","dropstart","dropinit"],function( i, type ){
+ $drop.trigger( type );
+ equals( counts[ type ], 1, "triggered "+ type );
+ counts[ type ] = 0;
+ });
+
+ // simulate a complete drag
+ $drag
+ .fire("mousedown",{ clientX:50, clientY:50 })
+ .fire("mousemove",{ clientX:51, clientY:51 })
+ .fire("mouseup",{ clientX:51, clientY:51 })
+ .trigger("click");
+
+ // check the event handler counts
+ $.each(["dropend","drop","dropstart","dropinit"],function( i, type ){
+ equals( counts[ type ], 1, "delegated "+ type );
+ });
+});
Oops, something went wrong.

0 comments on commit 1544778

Please sign in to comment.