1

I have javascript code that looks like this:

this.ProgressBarUpdater = {
  poll: function() {
    setInterval(ProgressBarUpdater.request, 5000);
  },

  request: function() {
    $(".progress_bar_updater[data-url]").each(function(i, elem) {
      url = $(elem).data("url");

      $.getJSON(url, function(data) {
        if (isFinished(data)) {
          location.reload();
        };

        $.each(data, function(key, val) {
          updateProgressBar(key, val);
        });
      });
    });
  }
};

isFinished = function(obj) {
  var correct = true;
  for (key in obj) {
    var progress = typeof obj[key] == 'string' ? obj[key] : obj[key][1];
    if (progress != '100%') correct = false;
  }
  return correct;
}

updateProgressBar = function(key, val) {
  var progress_info_value = typeof val == 'string' ? val : val[0];
  var progress_bar_value = typeof val == 'string' ? val : val[1];
  $(key + ' .progress_info').html(progress_info_value);
  $(key + ' .progress-bar').width(progress_bar_value);
}

How can I test it with Jasmine? I can't find any good tutorials about this...

1 Answer 1

1

it is generally hard to test objects.

What you could do:

  1. create the elements that your functions are using
  2. put them into the DOM
  3. call your functions
  4. check the elements
  5. remove elements from DOM

show this as an incentive:

describe('Test ProgressBarUpdater', function() {
    var elements = [];

    beforeAll(function() {
        // create elements and put into array

        elements.forEach(function(elem) {
            document.body.appendChild(elem);
        });
    });  

    afterAll(function() {
        // remove all elements from DOM
    });  

    it('call functions...', function() {
        // call the functions to test

        // check the elements
    });
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.