Ad

Writing Unit Tests For Form Elements

I'm writing jasmine test cases for my validation function. This is the code for my function.

function validateCardNum(){
    var cardEntered = $('#cardNum').val();
    var cTypeVal = $('#cType').val();
    if(cTypeVal === 'visa'){
         var reg = /^4\d{15}$/; 
         if (!reg.test(cardEntered)) {
               showError('#cardNum',errorMsg);
                return false;    
          };
     }
}

And this is the code I'm writing to test it:

 describe('VISA Card Validations', function() {
    $('#cType').val() = 'visa';
    it('should not validate empty CC number', function() {
        $('#cardNum').val() = '';    
        expect(validateCardNum().toBe(false));
    });
  });

When I write this, I get this error:

 ReferenceError: Invalid left-hand side in assignment

My question is how can I assign testing values to my input fields to test different case scenarios.

Ad

Answer

I was using wrong syntax for changing value of input fields. I used this code, and it worked like charm!

describe('VISA Card Validations', function() {
    $('#cType').val('visa');
    it('should not validate empty CC number', function() {
        $('#cardNum').val('');    
        expect(validateCardNum()).toBe(false);
    });
});
Ad
source: stackoverflow.com
Ad