Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test a html5 drag and drop in qunit

I want to test a html 5 drag and drop what i though would work:

From test file

var event = new Event('drop',{
    'originalEvent':{
        'dataTransfer':{
            'getData': function(){
                return 'tr#0.sprint'
             }
         }
     },
     stopPropagation': function() {return;}
}
$('tr#0.sprint').trigger(event);

This so that the event is triggered and all the functions are set to return what i want tested.

actual script:

Backbone framework

myView = Backbone.View.extend({

    ...

    drop : function(event)
    {
        event.stopPropagation();
        var data = event.originalEvent.dataTransfer.getData('element');

        // some code

    },

    ...

};

Sadly this error's to Illigal function call

If i remove the dataTransfer object and try to set it myself i get a undefined error

like image 437
Funonly Avatar asked Nov 01 '22 21:11

Funonly


1 Answers

If you set it a little different it makes a world of difference:

var event = new Event('drop',{
     stopPropagation: function() {return;}
});
event['originalEvent'] ={
    'dataTransfer':{
        'getData': function(){
            return 'tr#0.sprint'
        }
    }
};
$('tr#0.sprint').trigger(event);

This is a more 'manual' way of setting attributes of an javascript object. The arguments added to the constructor of the event are checked by the event object's construct.

like image 193
Funonly Avatar answered Nov 08 '22 05:11

Funonly