mardi 30 décembre 2014

Should JavaScript Event Handlers Be Unit Tested

There are a lot of questions here about unit test event handlers in other languages, but I haven't been able to find a good answer when it comes to JavaScript. Specifically I'm talking about a case like:



// inside a "view" class definition:
handleCheckboxClick: function() {
this.relevantData.toggleSomeValue();
return false;
}

// later on:
$('#someCheckbox').on('click', view.handleCheckboxClick);


Clearly there is logic in the event handler (this.relevantData.toggleSomeValue()), but at the same time no other method will ever call this handler, so it's not like unit-testing it will catch some future refactoring-related bug. And in any given JavaScript codebase there are A LOT of these handlers, so it's a non-trivial amount of work to test them.


Plus, in many JS shops (certainly in ours) there are also feature-level tests being done with Selenium that would typically catch obvious UI issues (such as when an event handler breaks).


So, on the one hand I get that "unit testing of logic == good", and I don't want to shoot myself in the foot by not testing certain code if I will pay for it later. On the other hand this particular sub-set of code seems to have a high cost and low value when it comes to unit testing. Thus, my question for the SO community is, should JS developers unit test their event handling functions?


Aucun commentaire:

Enregistrer un commentaire