Search code examples
javascripttestingqunit

Qunit testing with no passed Vars


I am trying to make a test of a function that does some pretty simple validation for a form. However I cannot figure out how to use qunit without passing vars to the function. here is an example of what I am talking about

  function validateForm(){
        var name = $('#name').val();
        var submit = true;
        //do some validation
        //if submit true
        form.submit();
   }

all the example of qunit I see in the docs do something like this

   ok(validateForm('hello'),'Hello is a valid name');

should I just modify my function or is there a way to work with such a setup.

//EDIT

The option I have chosen to pursue right now is setting up another function above the current one that simply parses the form inputs and then sends them as vars to the validateForm() function


Solution

  • What I ended up doing was modifying the function in a way that allowed me to pass the vars in the test.

    before the code would do something like this

           function validateForm(){
                   var value1 = $('#someinput').val();
                   //Do some validation based on these vars
                   $('#someform').submit();
            }
    

    Now When the submit button is pressed it goes to another function that parses the args out for me also added a 'test' arg so the form is not being submitted during the tests.

         function parseFormArgs(){
                 var value1 = $('#someinput').val();
                 var value2 = $('#otherinput').val();
                 var test = false;
    
                 validateForm(value1,value2,test);
         }
    
         function validateForm(value1,value2,test){
                   var submit = true;
                   //Do some validation
                   if( test != true && submit ){
                       $('#someform').submit();
                   }
    
                  return submit;
        }
    

    Then my qunit looks something like this

          var testFn = true;
          test('validateForm()', function(){
               equals(validateForm('ExampleVal1','ExampleVal2',testFn), false, 'Such and Such Values should not validate');
               ...
               ...
           });