unit testing - jQuery Trigger event but still have event.isTrigger false -


jquery provides istrigger property allows know if event "real event" or "triggered".

$(ele).on("click", function(e){     if(e.istrigger){        // event triggered, not real event      }else{       // real event      } })  

for unit-testing purposes, there way trigger event , somehow overwrite istrigger property.. , in event callback still behave (by having istrigger === false) if real click event.. trying following code:

var triggerclick = jquery.event("click", {     istrigger:false,     data:{         istrigger:false     } 

but doesn't seem pass correctly..

how native way, avoiding jquery istrigger altogether:

function simulateclick(elem) {   var evt = document.createevent("mouseevents");   evt.initmouseevent("click", true, true, elem,           0, 0, 0, 0, 0, false, false, false, false, 0, null);   if (document.createevent) {      elem.dispatchevent(evt);   } else {      elem.fireevent("on" + evt.eventtype, evt); // support ie crap   } } 

to use you'd do:

$(ele).on("click", function(e){     if(e.istrigger){        // event triggered, not real event         console.log('triggered');     }else{        // real event         console.log('clicked');     } });  simulateclick(ele); 

fiddle


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

keyboard - C++ GetAsyncKeyState alternative -

android - java.net.UnknownHostException(Unable to resolve host “URL”: No address associated with hostname) -