how to write a jasmine spec for triggering a on event in jquery

54 Views Asked by At

how to write a jasmine spec for triggering a on event in jquery ?

   $('#myid').on('keyup', function (event)
   {
       if(event.which === 13)
       {
           event.preventDefault();
           myfunc(id);
           }
   });

those lines are not covered by jasmine spec how to write jasmine specs for those lines. only triggering event didnt cover those lines so help me here

1

There are 1 best solutions below

0
lifetimeLearner007 On

I don't know how you wrote your spec but try the below code

var event = $.Event("keyup");
event.which = 13;
$("#myid").trigger("keyup");
// expectations follow

If it still doesn't work, try using a separate named function instead of anonymous function as follows:

$('#myid').on('keyup', keyupHandler.bind(this));
function keyupHandler() {
  if(event.which === 13){
    event.preventDefault();
    myfunc(id);
  }
}