1

I have the following function called on Init() of SubmitCodePage:

 TrySubmit: function () {
            var $submitButton = $("#submitButton");

            if (!$submitButton.is(':disabled')) {
                $submitButton.submit();
            }
        }

I am trying to check that if the submit button is disabled, submit is not attempted. Here is my attempt at a QUnit test:

QUnit.test("TrySubmit - Submit button is disabled - submit not called", function (assert) {
    var $fixture = $("#qunit-fixture");

    // injects a disabled submit button: 
    $fixture.append(HTMLInjectorSubmitForm());

    submitMock = sinon.stub(document, 'getElementById').withArgs('submitButton').returns('submit');

    assert.ok(submitMock.notCalled, "Submit not called when button is disabled");
});

The test just runs and times out - I don't know how to check that the 'submit' is actually called?

3
  • What's HTMLInjectorSubmitForm? If it's not related, please remove it.
    – Lin Du
    Dec 22, 2020 at 11:15
  • @slideshowp2 it injects the element I need for the test: "<input id='submitButton' type='submit' class='disabled' disabled='disabled'>";
    – Jordan1993
    Dec 22, 2020 at 11:17
  • Well, you're mocking out getElementById... but I'm not sure why, it is not used in your TrySubmit function (I'm not sure that $('#foo') uses it, jQuery could be using document.querySelector() instead). Also, where are you actually calling your TrySubmit function in your test? If you don't call that outer function, then the test would never get to the point of calling submit on your button. Dec 22, 2020 at 14:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.