1

My JS code is mentioned below:

if (typeof IPL === "undefined") {
    IPL = {};
}

/// <summary>Declare Namespace IPL.Register</summary>
if (typeof IPL.Register === "undefined") {
    IPL.Register = {};
}

$(document).ready(function () {
    IPL.Register.Print.initialize();
});

/// <summary>Declare Namespace IPL.Register.Print</summary>
IPL.Register.Print =
{
    /// <summary>Function to call on initialize page.</summary>
    initialize: function () {
        window.onload = function () {
            window.print();
        };
    }
};

When I run Test case (Qunit.js and blanket.js) as mentioned below then Document.ready function is not getting called and Code coverage not cover that line. Below test case works fine but it only include last lines of code and initialize function on window load.

test("initialize test", 1, function () {
    var result = IPL.Register.Print.initialize();
    equal(undefined, result, "passed");
});

Someone please assist how to write Test case to execute function on document load?

Raj
  • 183
  • 3
  • 17
  • What kind of test do you want? A Unit test shouldn't depend on document.ready event. If you want an e2e test you could try something like [selenium](http://www.seleniumhq.org/) – Anticom Sep 16 '16 at 11:20
  • I Just want to improve code coverage percentage. – Raj Sep 16 '16 at 11:26

1 Answers1

0

The best way to do this is by not testing the load event at all. Just put the event handler into a named function and then test the behaviour of that function.

/// <summary>Declare Namespace IPL.Register.Print</summary>
IPL.Register.Print = {
  /// <summary>Function to call on initialize page.</summary>
  initialize: function() {
    window.onload = function() {
      window.print();
    };
  },
  print: function() {
    window.print();
  }
};

test("initialize test", 1, function() {
  var result = IPL.Register.Print.print();
  equal(undefined, result, "passed");
});

http://jsfiddle.net/Tintin37/vpqjsr8L/

The load event just runs your function, what do you want specifically achieve by testing the load event ?

Quentin Roger
  • 6,410
  • 2
  • 23
  • 36