Saurabh Kumar
Saurabh Kumar

Reputation: 154

Jasmine.js: How to spyOn a dropdown list

I have a fromDate text field in my script as:

      var fromDate = document.getElementById('fromDate').value;

I know how to spyOn this textbox in jasmine.js:

    var params = {
    'fromDate': { value: '01/01/2010' },
         };

      beforeEach() {
      spyOn(document, 'getElementById').and.callFake(function (arg) {
            return params[arg];
        });
      }

Similarly I am also extracting value from a dropdown list in script as:

    var invId = $('#cboInv').find('option:selected').val();

Now how do I spy on this? Please help.

Upvotes: 1

Views: 1598

Answers (1)

Saurabh Kumar
Saurabh Kumar

Reputation: 154

I found the way to mock it.

Because it is a chained call, in the spyOn() function we need another method to mock val() also:

spyOn($.fn, 'find').and.callFake(function (arg) {
    var valInternal = function () {
        return 'myCustomId';
    };
    return {
        val: valInternal
    };
});

Upvotes: 1

Related Questions