QUnit async mockjax

by nickkell

HTML

<script src="http://code.jquery.com/qunit/qunit-1.9.0.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-1.9.0.css">
<script src="https://raw.github.com/appendto/jquery-mockjax/master/jquery.mockjax.js"></script>
<body>
    <h1 id="qunit-header">
        QUnit example</h1>
    <h2 id="qunit-banner">
    </h2>
    <div id="qunit-testrunner-toolbar">
    </div>
    <h2 id="qunit-userAgent">
    </h2>
    <ol id="qunit-tests">
    </ol>
    <div id="qunit-fixture">
        test markup, will be hidden</div>
</body>

JavaScript

$(document).ready(function() {

    function functionToTest(number) {
        return $.getJSON('/echo/json/', {
            json: JSON.stringify({
                order: number
            })
        });
    }

    $.mockjax({
        url: '/echo/json/',
        response: function(settings) {
            
            if (JSON.parse(settings.data.json).order === 1) {
                this.responseText = JSON.stringify({
                    hello: 'HEYO!'
                });
            } else {
                this.responseText = JSON.stringify({
                    hello: 'HELL NO!'
                });
            }
        }
    });


    module("first");

    test("first test", function() {

        stop();

        functionToTest(1).done(function(json) {
            ok(true, json.hello);
            start();
        });
    });


    test("second test", function() {

        stop();

        functionToTest(2).done(function(json) {
            ok(true, json.hello);
            start();
        });
    });
});