Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test jquery and ajax calls using JsTestDriver?

I have a dynamic page built with jQuery. Html pieces are loaded from mustache templates. These templates are downloaded from a url, and I would like to unit test the overall html construction :

The JsTestDriver test is :

AppTest = TestCase("AppTest")

AppTest.prototype.test = function() {
    var actualHtml = "";

    getHtml({ "title": "title", "header": "header", "text": "text", "authors": [ {"firstname": "firstname", "lastname": "lastname"} ] }, function(html) {
        actualHtml = html;
    });

    assertEquals("expected html", actualHtml);
};

And the code :

function getHtml(json, resultFunc) {
   jQuery.ajax({
            url: "url/to/mustache/template",
            success: function(view) {
                    resultFunc(mergeArticleModelAndView(json, view));
            },
            error: function(jqXHR, textStatus, errorThrown) {
                    resultFunc(textStatus + " errorThrown: " + errorThrown);
            },
            dataType: 'text',
            async: false 
    });
}

Then I launch the tests and the result is :

$ java -jar JsTestDriver-1.3.2.jar --port 9876 --browser /usr/bin/firefox --tests all
F
Total 1 tests (Passed: 0; Fails: 1; Errors: 0) (8,00 ms)
  Firefox 5.0 Linux: Run 1 tests (Passed: 0; Fails: 1; Errors 0) (8,00 ms)
    AppTest.test failed (8,00 ms): AssertError: expected "expected html" but was "error errorThrown: [Exception... \"Component returned failure code: 0x80004005 (NS_ERROR_FAILURE)\"  nsresult: \"0x80004005 (NS_ERROR_FAILURE)\"  location: \"JS frame :: http://localhost:9876/test/main/js/jquery.min.js :: <TOP_LEVEL> :: line 16\"  data: no]"
  ()@http://localhost:9876/test/test/js/app_test.js:25

So the error callback has been called, and I don't understand why it breaks with JsTestDriver, and the code works when calling the application manually with a browser

Last thing, the jsTestDriver.conf :

server: http://localhost:9876

load:
  - test/js/app_test.js
  - main/js/jquery.min.js
  - main/js/jquery.mustache.js
  - main/js/app.js

Thank you for your advices. More generally, what unit test frameworks do you use for javascript and command line testing with DOM and jQuery ?

like image 372
Bruno Thomas Avatar asked Jul 16 '11 13:07

Bruno Thomas


1 Answers

An alternative would be to get JsTestDriver to serve your template and some test JSON data. The configuration below allows you to put static JSON test data (e.g. my_data.json) in src/test/webapp/json and Mustache templates in src/main/webapp/templates. You can change this around to match your source layout if this is too Maven-y for your tastes.

Note that JsTestDriver serves the assets with /test/ pre-pended to the URI specified in the serve attribute, so src/test/webapp/json/my_data.json is actually served from http:/ /localhost:9876/test/src/test/webapp/json/my_data.json.

server: http://localhost:9876

serve:
- src/main/webapp/templates/*.html
- src/test/webapp/json/*.json

load:
- src/main/webapp/js/*.js
- src/main/webapp/js/libs/*.js

test:
- src/test/webapp/js/*.js

Then, in a test case you can easily load a template and JSON data.

    testLoadTemplateAndData : function () {

        // Variables
        var onComplete, template, data, expected, actual, templateLoaded, dataLoaded;
        dataLoaded = false;
        templateLoaded = false;
        expected = "<h2>Your expected markup</h2>";

        // Completion action
        onComplete = function () {
            if (dataLoaded && templateLoaded) {

                // Render the template and data
                actual = Mustache.to_html(template, data);

                // Compare with expected
                assertEquals('Markup should match', expected, actual);

            }
        };

        // Load data with jQuery
        $.ajax({
            url : '/test/src/test/webapp/json/demo.json', 
            success : 
                function (result) {
                    data = result;
                    dataLoaded = true;
            },
            error : 
                function () {
                    fail("Data did not load.");
            },
            complete :
                function () {
                    onComplete();
                }
            }
        );

        // Load the template with jQuery
        $.get('/test/src/main/webapp/templates/demo.html',
            function(result) {
                template = result;
                templateLoaded = true;
            }
        )
        .error(function() { fail("Template did not load."); })
        .complete(function() {
            onComplete();
        });

    }

When both jQuery callbacks complete, Mustache should parse the template with the JSON data and render the expected output.

like image 68
pvankann Avatar answered Oct 24 '22 05:10

pvankann