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:

http://markdotmeyer.blogspot.com/2008/07/javascript-unit-testing-with-qunit.html

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")
                    .appendTo("#stack")
                    .windowStack()
                    .withLoadingStatus(loadingStatus);

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

    stop();

    setTimeout(function() {
        equals(loadingStatus[0].parentNode, null, "loadingStatus DIV should be removed after the iframe is loaded");
        start();
    }, 500);
});

Related Articles:

Post Footer automatically generated by Add Post Footer Plugin for wordpress.

About Chad Myers

Chad Myers is the Director of Development for Dovetail Software, in Austin, TX, where he leads a premiere software team building complex enterprise software products. Chad is a .NET software developer specializing in enterprise software designs and architectures. He has over 12 years of software development experience and a proven track record of Agile, test-driven project leadership using both Microsoft and open source tools. He is a community leader who speaks at the Austin .NET User's Group, the ADNUG Code Camp, and participates in various development communities and open source projects.
This entry was posted in QUnit. Bookmark the permalink. Follow any comments here with the RSS feed for this post.