Testing whether a form is submitted twice using QUnit

140 Views Asked by At

I am trying to understand the behavior of QUnit, using Chutzpah in Visual Studio. The following JQuery plugin should prevent double submissions of a form:

$.fn.preventDoubleSubmit = function () {
$(this).submit(function (e) {
    if (this.beenSubmitted) {
        return false;
    }
    else {
        this.beenSubmitted = true;
    }
});
return this;
};

It is used like this:

<script>
$(function () {
    jQuery('#someForm).preventDoubleSubmit();
});
</script>

Now I want to test it using QUnit, such that every time the form is submitted a counter is incremented, and hence we can test if the form is submitted. like so:

/// <reference path="../jquery-1.10.2.js" />
/// <reference path="extensions.js" />

(function ($) {

    var hasSubmitted = 0;
    this.$form.preventDoubleSubmit();

    this.$parent.on("submit", function () {
        hasSubmitted++;
    });

    this.$form.submit();
    equal(hasSubmitted, 1, 'It should have been submitted');

    this.$form.submit();
    equal(hasSubmitted, 1, 'It should not have been submitted');
});
})(jQuery);

However, the form appears to be submitted twice. Yet, outside of QUnit the code appears to work as expected (the form is only submitted once).

What is going on here and/or what is the correct way to test form submissions in QUnit and Chutzpah? Thanks!

p.s., tried using Phantom and Chrome engines, but the result is the same.

0

There are 0 best solutions below