Thursday, December 13, 2012

Javascript Unit Testing


The following posts will use QUnit functions for testing. There is no explicit marking of QUnit functoins. So, when in doubt search in QUnit API.


Testing if a button was clicked



The following code shows you how to unit test weather a button was clicked in javascript. I found need for this while trying to test the following scenario: When user presses enter while an input box has focus then fire a click event on the button next to the input box.
test(“button clicked”, function(){
$(“#button”).on(‘click’, function(event){
$(this).data(‘clicked’, true);
});

$(“#button”).click();
ok($(“#button”).data(‘clicked’), “button clicked”)
});
As you can see the sky is the limit to what can be tested with such a technique.

No comments:

Post a Comment