Asynchronous JavaScript Testing with QUnit

I’m implementing a feature that does some stuff when an IFRAME is finished loading.  Due to the inherent asynchronous nature of this functionality, it was hard to test.  I was trying to use QUnit, but having issues, until I found this post:

The summary?  Check out the stop() and start() functions.

Now, I can write my test like this:

test("addWindow should remove the loadingStauts div after the iframe is loaded", function() {
    var loadingStatus = $("<div></div>").attr("id", "loadingDiv");
    var stack = $("<div></div>")
                    .attr("id", "stackDiv")

    stack[0].addWindow("Blank_for_iframe_testing.htm", "");


    setTimeout(function() {
        equals(loadingStatus[0].parentNode, null, "loadingStatus DIV should be removed after the iframe is loaded");
    }, 500);
%d bloggers like this: