Search code examples
javascriptjqueryjasminejasmine-jquery

Unit test case for html element


I am not able to get the html element value in unit test using Jasmine.

JavaScript code:

  $scope.close = function () {
    $scope.success = false;
    $('#current, #new').val(''); // Need to test this
  };

Jasmine code:

  it('close method', function() {
    scope.close();
    expect($('#current')).toContain(''); //unable to get the value ->return undefined
  });

Error:

TypeError: 'undefined' is not a function (evaluating 'haystack.indexOf(needle)')

Please help me to solve this.


Solution

  • You're trying to perform a .toContain test on a jQuery node collection, which is not an Array, so it doesn't have the .indexOf method your error is mentioning. If you're trying to test the value you've set using .val(), do this instead:

    expect($('#current').val()).toEqual('');