mercredi 24 juin 2015

What is the state-of-the-art approach to testing jQuery code?

I'm new to JavaScript. Imagine, I have following piece of code:

$(document).ready(function() {
  $('#nights').on('keyup', function() {
    var nights = +$(this).val();
    var dailyPrice = +$(this).closest(".tour").data("daily-price");
    $('#total').text(nights * dailyPrice);
    $('#nights-count').text($(this).val());
  });
});

How can I unit-test that the anonymous function

  1. retrieves the value of the current element,
  2. retrieves the data from +$(this).closest(".tour").data("daily-price") and
  3. then calls text(...) on $('#total') and $('#nights-count')

?

Note that I'm interested in unit tests (therefore creating a full-fledged a Selenium test suite, which types in something and then checks the value of the elements isn't suitable for me), which don't require me to add a new abstraction layer.

By abstraction layer I mean this: I could create class JQueryAbstraction and then 2 sub-classes - one, which calls real jQuery methods and another, which just counts the number of calls.

Aucun commentaire:

Enregistrer un commentaire