0

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.

Dr. Thomas C. King
  • 993
  • 2
  • 15
  • 28
  • They event handlers are being added to two different submit events, one is on the child, and the other is on the parent. If the child returns false, should the submit event not be prevented from bubbling up to the parent? – Dr. Thomas C. King Feb 04 '19 at 15:40
  • Good call, didn't notice that as it wasn't clear what `this` was given the incomplete code provided (brackets don't match up, so assuming something missing). – freedomn-m Feb 04 '19 at 15:46
  • Yeah, I think you need to stop propagation in addition to `return false`. Check out [this question/answer on double submit event handlers](https://stackoverflow.com/questions/4507956/two-submit-event-handlers-on-a-form-one-must-stop-the-other). – Jordan Kasper Feb 05 '19 at 19:05

0 Answers0