Declare Namespace IPL.Register if (typeof IPL.Regi" /> Declare Namespace IPL.Register if (typeof IPL.Regi" /> Declare Namespace IPL.Register if (typeof IPL.Regi"/>

Test Case to call function on Document.ready

242 Views Asked by At

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?

1

There are 1 best solutions below

0
Quentin Roger On

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 ?